diff options
Diffstat (limited to 'logger')
-rw-r--r-- | logger/logger.go | 50 | ||||
-rw-r--r-- | logger/logger_test.go | 59 |
2 files changed, 77 insertions, 32 deletions
diff --git a/logger/logger.go b/logger/logger.go index 9cd4b33..bc2b3c8 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -29,8 +29,8 @@ const ( // Severity tags. const ( tagInfo = "\033[0m[INFO] " - tagWarning = "\033[1;33m[WARN] " - tagError = "\033[1;31m[ERROR] " + tagWarning = "\033[33m[WARN] " + tagError = "\033[31m[ERROR] " tagFatal = "\033[1;31m[FATAL] " ) @@ -46,13 +46,13 @@ var ( // initialize resets defaultLogger. Which allows tests to reset environment. func initialize() { defaultLogger = &Logger{ - loggers: []logger{ + loggers: [4]logger{ log.New(os.Stderr, tagInfo, flags), log.New(os.Stderr, tagWarning, flags), log.New(os.Stderr, tagError, flags), log.New(os.Stderr, tagFatal, flags), }, - level: 3, + minSeverity: 0, } } @@ -67,14 +67,16 @@ func init() { // generated logger, subsequent calls to Init will only return the generated // logger. func Init(level int) *Logger { - - loggers := []logger{ - log.New(os.Stderr, tagInfo, flags), - log.New(os.Stderr, tagWarning, flags), - log.New(os.Stderr, tagError, flags), - log.New(os.Stderr, tagFatal, flags), + l := Logger{ + loggers: [4]logger{ + log.New(os.Stderr, tagInfo, flags), + log.New(os.Stderr, tagWarning, flags), + log.New(os.Stderr, tagError, flags), + log.New(os.Stderr, tagFatal, flags), + }, + minSeverity: sFatal - severity(level), + initialized: true, } - l := Logger{loggers: loggers, level: level, initialized: true} logLock.Lock() defer logLock.Unlock() @@ -88,13 +90,13 @@ func Init(level int) *Logger { // A Logger represents an active logging object. Multiple loggers can be used // simultaneously even if they are using the same writers. type Logger struct { - loggers []logger - level int + loggers [4]logger + minSeverity severity initialized bool } func (l *Logger) output(s severity, depth int, txt string) { - if s < sFatal-severity(l.level) { + if s < l.minSeverity { return } logLock.Lock() @@ -125,12 +127,6 @@ func (l *Logger) Info(v ...interface{}) { l.output(sInfo, 0, fmt.Sprint(v...)) } -// InfoDepth acts as Info but uses depth to determine which call frame to log. -// InfoDepth(0, "msg") is the same as Info("msg"). -func (l *Logger) InfoDepth(depth int, v ...interface{}) { - l.output(sInfo, depth, fmt.Sprint(v...)) -} - // Infof logs with the Info severity. // Arguments are handled in the manner of fmt.Printf. func (l *Logger) Infof(format string, v ...interface{}) { @@ -143,12 +139,6 @@ func (l *Logger) Warning(v ...interface{}) { l.output(sWarning, 0, fmt.Sprint(v...)) } -// WarningDepth acts as Warning but uses depth to determine which call frame to log. -// WarningDepth(0, "msg") is the same as Warning("msg"). -func (l *Logger) WarningDepth(depth int, v ...interface{}) { - l.output(sWarning, depth, fmt.Sprint(v...)) -} - // Warningf logs with the Warning severity. // Arguments are handled in the manner of fmt.Printf. func (l *Logger) Warningf(format string, v ...interface{}) { @@ -161,12 +151,6 @@ func (l *Logger) Error(v ...interface{}) { l.output(sError, 0, fmt.Sprint(v...)) } -// ErrorDepth acts as Error but uses depth to determine which call frame to log. -// ErrorDepth(0, "msg") is the same as Error("msg"). -func (l *Logger) ErrorDepth(depth int, v ...interface{}) { - l.output(sError, depth, fmt.Sprint(v...)) -} - // Errorf logs with the Error severity. // Arguments are handled in the manner of fmt.Printf. func (l *Logger) Errorf(format string, v ...interface{}) { @@ -193,6 +177,7 @@ func (l *Logger) Panicf(format string, v ...interface{}) { // Arguments are handled in the manner of fmt.Print. func (l *Logger) Fatal(v ...interface{}) { l.output(sFatal, 0, fmt.Sprint(v...)) + debug.PrintStack() os.Exit(1) } @@ -200,6 +185,7 @@ func (l *Logger) Fatal(v ...interface{}) { // Arguments are handled in the manner of fmt.Printf. func (l *Logger) Fatalf(format string, v ...interface{}) { l.output(sFatal, 0, fmt.Sprintf(format, v...)) + debug.PrintStack() os.Exit(1) } diff --git a/logger/logger_test.go b/logger/logger_test.go index ce708bb..4d07d8c 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -3,7 +3,9 @@ package logger import ( "bufio" "bytes" + "log" "os" + "path/filepath" "reflect" "strings" "testing" @@ -85,3 +87,60 @@ func TestInit(t *testing.T) { } } } + +func TestLevel(t *testing.T) { + initialize() + var buf bytes.Buffer + Init(1) + SetOutput(&buf) + + Infof("info %d", sInfo) + Warningf("warning %d", sWarning) + Errorf("error %d", sError) + s := buf.String() + if strings.Contains(s, "info") { + t.Errorf("log output %q should not contain: info", s) + } + if strings.Contains(s, "warning") { + t.Errorf("log output %q should not contain: warning", s) + } + if !strings.Contains(s, "error") { + t.Errorf("log output %q should contain: error", s) + } +} + +func TestFlags(t *testing.T) { + initialize() + l := Init(3) + SetFlags(log.Llongfile) + var buf bytes.Buffer + SetOutput(&buf) + l.Infof("info %d", sInfo) + s := buf.String() + if !strings.Contains(s, "info 0") { + t.Errorf("log output %q should contain: info 0", s) + } + path := filepath.Join("logger", "logger_test.go") + if !strings.Contains(s, path) { + t.Errorf("log output %q should contain: %s", s, path) + } + + // bonus for coverage + l.Warning("warning") + l.Warningf("warning %d", sWarning) + l.Error("error") + l.Errorf("error %d", sError) + s = buf.String() + if !strings.Contains(s, "warning") { + t.Errorf("log output %q should contain: warning", s) + } + if !strings.Contains(s, "warning 1") { + t.Errorf("log output %q should contain: warning 1", s) + } + if !strings.Contains(s, "error") { + t.Errorf("log output %q should contain: error", s) + } + if !strings.Contains(s, "error 2") { + t.Errorf("log output %q should contain: error 2", s) + } +} |