From a1b768438258bfd6c33573e156708f002c693ba2 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Wed, 22 Sep 2021 19:47:27 +0200 Subject: add debug log level --- logger/logger.go | 38 +++++++++++++++++++++++++++++++++----- logger/logger_test.go | 12 ++++++------ 2 files changed, 39 insertions(+), 11 deletions(-) (limited to 'logger') diff --git a/logger/logger.go b/logger/logger.go index bd8b937..9613e17 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -20,15 +20,18 @@ type logger interface { // Severity levels. const ( - sInfo severity = iota + sDebug severity = iota + sInfo sWarning sError sFatal + sCount ) // Severity tags. const ( - tagInfo = "\033[0m[INFO] " + tagDebug = "\033[0m[DEBUG] " + tagInfo = "\033[97m[INFO] " tagWarning = "\033[33m[WARN] " tagError = "\033[31m[ERROR] " tagFatal = "\033[1;31m[FATAL] " @@ -44,8 +47,9 @@ var ( defaultLogger *Logger ) -func newLoggers() [4]logger { - return [4]logger{ +func newLoggers() [sCount]logger { + return [sCount]logger{ + log.New(os.Stderr, tagDebug, flags), log.New(os.Stderr, tagInfo, flags), log.New(os.Stderr, tagWarning, flags), log.New(os.Stderr, tagError, flags), @@ -90,7 +94,7 @@ 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 [4]logger + loggers [sCount]logger minSeverity severity initialized bool } @@ -129,6 +133,18 @@ func (l *Logger) SetFlags(flag int) { } } +// Debug logs with the Info severity. +// Arguments are handled in the manner of fmt.Print. +func (l *Logger) Debug(v ...interface{}) { + l.output(sDebug, v...) +} + +// Debugf logs with the Info severity. +// Arguments are handled in the manner of fmt.Printf. +func (l *Logger) Debugf(format string, v ...interface{}) { + l.outputf(sDebug, format, v...) +} + // Info logs with the Info severity. // Arguments are handled in the manner of fmt.Print. func (l *Logger) Info(v ...interface{}) { @@ -209,6 +225,18 @@ func SetFlags(flag int) { defaultLogger.SetFlags(flag) } +// Debug uses the default logger and logs with the Info severity. +// Arguments are handled in the manner of fmt.Print. +func Debug(v ...interface{}) { + defaultLogger.output(sDebug, v...) +} + +// Debugf uses the default logger and logs with the Info severity. +// Arguments are handled in the manner of fmt.Printf. +func Debugf(format string, v ...interface{}) { + defaultLogger.outputf(sDebug, format, v...) +} + // Info uses the default logger and logs with the Info severity. // Arguments are handled in the manner of fmt.Print. func Info(v ...interface{}) { diff --git a/logger/logger_test.go b/logger/logger_test.go index 2352856..2e1ff25 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -116,8 +116,8 @@ func TestFlags(t *testing.T) { 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) + if !strings.Contains(s, "info 1") { + t.Errorf("log output %q should contain: info 1", s) } path := "logger/logger_test.go:117" if !strings.Contains(s, path) { @@ -133,13 +133,13 @@ func TestFlags(t *testing.T) { 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, "warning 2") { + t.Errorf("log output %q should contain: warning 2", 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) + if !strings.Contains(s, "error 3") { + t.Errorf("log output %q should contain: error 3", s) } } -- cgit v1.2.3