add comments

This commit is contained in:
Wenkai Yin 2016-03-14 14:53:28 +08:00
parent 7de0adbff0
commit 1e21f43dd7
5 changed files with 46 additions and 19 deletions

View File

@ -15,6 +15,7 @@
package log package log
// Formatter formats records in different ways: text, json, etc.
type Formatter interface { type Formatter interface {
Format(*Record) ([]byte, error) Format(*Record) ([]byte, error)
} }

View File

@ -15,13 +15,19 @@
package log package log
// Level ...
type Level int type Level int
const ( const (
// DebugLevel debug
DebugLevel Level = iota DebugLevel Level = iota
// InfoLevel info
InfoLevel InfoLevel
// WarningLevel warning
WarningLevel WarningLevel
// ErrorLevel error
ErrorLevel ErrorLevel
// FatalLevel fatal
FatalLevel FatalLevel
) )
@ -41,5 +47,3 @@ func (l Level) string() string {
return "unknown" return "unknown"
} }
//DEBUG, INFO, WARNING, ERROR, and FATAL

View File

@ -34,6 +34,7 @@ type Logger struct {
mu sync.Mutex mu sync.Mutex
} }
// New returns a customized Logger
func New(out io.Writer, fmtter Formatter, lvl Level) *Logger { func New(out io.Writer, fmtter Formatter, lvl Level) *Logger {
return &Logger{ return &Logger{
out: out, out: out,
@ -42,6 +43,7 @@ func New(out io.Writer, fmtter Formatter, lvl Level) *Logger {
} }
} }
//SetOutput sets the output of Logger l
func (l *Logger) SetOutput(out io.Writer) { func (l *Logger) SetOutput(out io.Writer) {
l.mu.Lock() l.mu.Lock()
defer l.mu.Unlock() defer l.mu.Unlock()
@ -49,6 +51,7 @@ func (l *Logger) SetOutput(out io.Writer) {
l.out = out l.out = out
} }
//SetFormatter sets the formatter of Logger l
func (l *Logger) SetFormatter(fmtter Formatter) { func (l *Logger) SetFormatter(fmtter Formatter) {
l.mu.Lock() l.mu.Lock()
defer l.mu.Unlock() defer l.mu.Unlock()
@ -56,6 +59,7 @@ func (l *Logger) SetFormatter(fmtter Formatter) {
l.fmtter = fmtter l.fmtter = fmtter
} }
//SetLevel sets the level of Logger l
func (l *Logger) SetLevel(lvl Level) { func (l *Logger) SetLevel(lvl Level) {
l.mu.Lock() l.mu.Lock()
defer l.mu.Unlock() defer l.mu.Unlock()
@ -63,25 +67,19 @@ func (l *Logger) SetLevel(lvl Level) {
l.lvl = lvl l.lvl = lvl
} }
//SetOutput sets the output of default Logger
func SetOutput(out io.Writer) { func SetOutput(out io.Writer) {
logger.mu.Lock() logger.SetOutput(out)
defer logger.mu.Unlock()
logger.out = out
} }
//SetFormatter sets the formatter of default Logger
func SetFormatter(fmtter Formatter) { func SetFormatter(fmtter Formatter) {
logger.mu.Lock() logger.SetFormatter(fmtter)
defer logger.mu.Unlock()
logger.fmtter = fmtter
} }
//SetLevel sets the level of default Logger
func SetLevel(lvl Level) { func SetLevel(lvl Level) {
logger.mu.Lock() logger.SetLevel(lvl)
defer logger.mu.Unlock()
logger.lvl = lvl
} }
func (l *Logger) output(record *Record) (err error) { func (l *Logger) output(record *Record) (err error) {
@ -98,6 +96,7 @@ func (l *Logger) output(record *Record) (err error) {
return return
} }
// Debug ...
func (l *Logger) Debug(v ...interface{}) { func (l *Logger) Debug(v ...interface{}) {
if l.lvl <= DebugLevel { if l.lvl <= DebugLevel {
line := line(2) line := line(2)
@ -106,6 +105,7 @@ func (l *Logger) Debug(v ...interface{}) {
} }
} }
// Debugf ...
func (l *Logger) Debugf(format string, v ...interface{}) { func (l *Logger) Debugf(format string, v ...interface{}) {
if l.lvl <= DebugLevel { if l.lvl <= DebugLevel {
line := line(2) line := line(2)
@ -114,6 +114,7 @@ func (l *Logger) Debugf(format string, v ...interface{}) {
} }
} }
// Info ...
func (l *Logger) Info(v ...interface{}) { func (l *Logger) Info(v ...interface{}) {
if l.lvl <= InfoLevel { if l.lvl <= InfoLevel {
record := NewRecord(time.Now(), fmt.Sprint(v...), "", InfoLevel) record := NewRecord(time.Now(), fmt.Sprint(v...), "", InfoLevel)
@ -121,6 +122,7 @@ func (l *Logger) Info(v ...interface{}) {
} }
} }
// Infof ...
func (l *Logger) Infof(format string, v ...interface{}) { func (l *Logger) Infof(format string, v ...interface{}) {
if l.lvl <= InfoLevel { if l.lvl <= InfoLevel {
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", InfoLevel) record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", InfoLevel)
@ -128,6 +130,7 @@ func (l *Logger) Infof(format string, v ...interface{}) {
} }
} }
// Warning ...
func (l *Logger) Warning(v ...interface{}) { func (l *Logger) Warning(v ...interface{}) {
if l.lvl <= WarningLevel { if l.lvl <= WarningLevel {
record := NewRecord(time.Now(), fmt.Sprint(v...), "", WarningLevel) record := NewRecord(time.Now(), fmt.Sprint(v...), "", WarningLevel)
@ -135,6 +138,7 @@ func (l *Logger) Warning(v ...interface{}) {
} }
} }
// Warningf ...
func (l *Logger) Warningf(format string, v ...interface{}) { func (l *Logger) Warningf(format string, v ...interface{}) {
if l.lvl <= WarningLevel { if l.lvl <= WarningLevel {
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", WarningLevel) record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", WarningLevel)
@ -142,6 +146,7 @@ func (l *Logger) Warningf(format string, v ...interface{}) {
} }
} }
// Error ...
func (l *Logger) Error(v ...interface{}) { func (l *Logger) Error(v ...interface{}) {
if l.lvl <= ErrorLevel { if l.lvl <= ErrorLevel {
line := line(2) line := line(2)
@ -150,6 +155,7 @@ func (l *Logger) Error(v ...interface{}) {
} }
} }
// Errorf ...
func (l *Logger) Errorf(format string, v ...interface{}) { func (l *Logger) Errorf(format string, v ...interface{}) {
if l.lvl <= ErrorLevel { if l.lvl <= ErrorLevel {
line := line(2) line := line(2)
@ -158,6 +164,7 @@ func (l *Logger) Errorf(format string, v ...interface{}) {
} }
} }
// Fatal ...
func (l *Logger) Fatal(v ...interface{}) { func (l *Logger) Fatal(v ...interface{}) {
if l.lvl <= FatalLevel { if l.lvl <= FatalLevel {
line := line(2) line := line(2)
@ -167,6 +174,7 @@ func (l *Logger) Fatal(v ...interface{}) {
os.Exit(1) os.Exit(1)
} }
// Fatalf ...
func (l *Logger) Fatalf(format string, v ...interface{}) { func (l *Logger) Fatalf(format string, v ...interface{}) {
if l.lvl <= FatalLevel { if l.lvl <= FatalLevel {
line := line(2) line := line(2)
@ -176,6 +184,7 @@ func (l *Logger) Fatalf(format string, v ...interface{}) {
os.Exit(1) os.Exit(1)
} }
// Debug ...
func Debug(v ...interface{}) { func Debug(v ...interface{}) {
if logger.lvl <= DebugLevel { if logger.lvl <= DebugLevel {
line := line(2) line := line(2)
@ -184,6 +193,7 @@ func Debug(v ...interface{}) {
} }
} }
// Debugf ...
func Debugf(format string, v ...interface{}) { func Debugf(format string, v ...interface{}) {
if logger.lvl <= DebugLevel { if logger.lvl <= DebugLevel {
line := line(2) line := line(2)
@ -192,6 +202,7 @@ func Debugf(format string, v ...interface{}) {
} }
} }
// Info ...
func Info(v ...interface{}) { func Info(v ...interface{}) {
if logger.lvl <= InfoLevel { if logger.lvl <= InfoLevel {
record := NewRecord(time.Now(), fmt.Sprint(v...), "", InfoLevel) record := NewRecord(time.Now(), fmt.Sprint(v...), "", InfoLevel)
@ -199,6 +210,7 @@ func Info(v ...interface{}) {
} }
} }
// Infof ...
func Infof(format string, v ...interface{}) { func Infof(format string, v ...interface{}) {
if logger.lvl <= InfoLevel { if logger.lvl <= InfoLevel {
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", InfoLevel) record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", InfoLevel)
@ -206,6 +218,7 @@ func Infof(format string, v ...interface{}) {
} }
} }
// Warning ...
func Warning(v ...interface{}) { func Warning(v ...interface{}) {
if logger.lvl <= WarningLevel { if logger.lvl <= WarningLevel {
record := NewRecord(time.Now(), fmt.Sprint(v...), "", WarningLevel) record := NewRecord(time.Now(), fmt.Sprint(v...), "", WarningLevel)
@ -213,6 +226,7 @@ func Warning(v ...interface{}) {
} }
} }
// Warningf ...
func Warningf(format string, v ...interface{}) { func Warningf(format string, v ...interface{}) {
if logger.lvl <= WarningLevel { if logger.lvl <= WarningLevel {
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", WarningLevel) record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", WarningLevel)
@ -220,6 +234,7 @@ func Warningf(format string, v ...interface{}) {
} }
} }
// Error ...
func Error(v ...interface{}) { func Error(v ...interface{}) {
if logger.lvl <= ErrorLevel { if logger.lvl <= ErrorLevel {
line := line(2) line := line(2)
@ -228,6 +243,7 @@ func Error(v ...interface{}) {
} }
} }
// Errorf ...
func Errorf(format string, v ...interface{}) { func Errorf(format string, v ...interface{}) {
if logger.lvl <= ErrorLevel { if logger.lvl <= ErrorLevel {
line := line(2) line := line(2)
@ -236,6 +252,7 @@ func Errorf(format string, v ...interface{}) {
} }
} }
// Fatal ...
func Fatal(v ...interface{}) { func Fatal(v ...interface{}) {
if logger.lvl <= FatalLevel { if logger.lvl <= FatalLevel {
line := line(2) line := line(2)
@ -245,6 +262,7 @@ func Fatal(v ...interface{}) {
os.Exit(1) os.Exit(1)
} }
// Fatalf ...
func Fatalf(format string, v ...interface{}) { func Fatalf(format string, v ...interface{}) {
if logger.lvl <= FatalLevel { if logger.lvl <= FatalLevel {
line := line(2) line := line(2)

View File

@ -19,13 +19,15 @@ import (
"time" "time"
) )
// Record holds information about log
type Record struct { type Record struct {
Time time.Time Time time.Time // time when the log produced
Msg string Msg string // content of the log
Line string Line string // in which file and line that the log produced
Lvl Level Lvl Level // level of the log
} }
// NewRecord creates a record according to the arguments provided and returns it
func NewRecord(time time.Time, msg, line string, lvl Level) *Record { func NewRecord(time time.Time, msg, line string, lvl Level) *Record {
return &Record{ return &Record{
Time: time, Time: time,

View File

@ -20,12 +20,14 @@ import (
"time" "time"
) )
var defaultTimeFormat string = time.RFC3339 var defaultTimeFormat = time.RFC3339 // 2006-01-02T15:04:05Z07:00
// TextFormatter represents a kind of formatter that formats the logs as plain text
type TextFormatter struct { type TextFormatter struct {
timeFormat string timeFormat string
} }
// NewTextFormatter returns a customized TextFormatter
func NewTextFormatter(timeFormat string) *TextFormatter { func NewTextFormatter(timeFormat string) *TextFormatter {
if len(timeFormat) == 0 { if len(timeFormat) == 0 {
timeFormat = defaultTimeFormat timeFormat = defaultTimeFormat