From a606813fdf8b0b4f78fb4448cf3513656ff37beb Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 14 Aug 2019 02:18:22 +0300 Subject: [PATCH 1/2] export log levels and reverse log level order Signed-off-by: Vasiliy Tolstov --- util/log/log.go | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/util/log/log.go b/util/log/log.go index 4374aa1b..b533f39d 100644 --- a/util/log/log.go +++ b/util/log/log.go @@ -12,30 +12,30 @@ import ( type Level int const ( - trace Level = iota - debug - info - fatal + LevelFatal Level = iota + LevelInfo + LevelDebug + LevelTrace ) var ( // the local logger logger log.Logger = golog.New() - // default log level is debug - level = info + // default log level is info + level = LevelInfo ) func init() { switch os.Getenv("MICRO_LOG_LEVEL") { case "debug": - level = debug + level = LevelDebug case "info": - level = info + level = LevelInfo case "trace": - level = trace + level = LevelTrace case "fatal": - level = fatal + level = LevelFatal } } @@ -51,7 +51,7 @@ func Logf(format string, v ...interface{}) { // WithLevel logs with the level specified func WithLevel(l Level, v ...interface{}) { - if l < level { + if l > level { return } Log(v...) @@ -59,7 +59,7 @@ func WithLevel(l Level, v ...interface{}) { // WithLevel logs with the level specified func WithLevelf(l Level, format string, v ...interface{}) { - if l < level { + if l > level { return } Logf(format, v...) @@ -67,43 +67,43 @@ func WithLevelf(l Level, format string, v ...interface{}) { // Trace provides trace level logging func Trace(v ...interface{}) { - WithLevel(trace, v...) + WithLevel(LevelTrace, v...) } // Tracef provides trace level logging func Tracef(format string, v ...interface{}) { - WithLevelf(trace, format, v...) + WithLevelf(LevelTrace, format, v...) } // Debug provides debug level logging func Debug(v ...interface{}) { - WithLevel(debug, v...) + WithLevel(LevelDebug, v...) } // Debugf provides debug level logging func Debugf(format string, v ...interface{}) { - WithLevelf(debug, format, v...) + WithLevelf(LevelDebug, format, v...) } // Info provides info level logging func Info(v ...interface{}) { - WithLevel(info, v...) + WithLevel(LevelInfo, v...) } // Infof provides info level logging func Infof(format string, v ...interface{}) { - WithLevelf(info, format, v...) + WithLevelf(LevelInfo, format, v...) } // Fatal logs with Log and then exits with os.Exit(1) func Fatal(v ...interface{}) { - WithLevel(fatal, v...) + WithLevel(LevelFatal, v...) os.Exit(1) } // Fatalf logs with Logf and then exits with os.Exit(1) func Fatalf(format string, v ...interface{}) { - WithLevelf(fatal, format, v...) + WithLevelf(LevelFatal, format, v...) os.Exit(1) } From 70d0029658da6118cb83b6f721407fd395517211 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 14 Aug 2019 02:26:51 +0300 Subject: [PATCH 2/2] add warn log level Signed-off-by: Vasiliy Tolstov --- util/log/log.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/util/log/log.go b/util/log/log.go index b533f39d..0b911aae 100644 --- a/util/log/log.go +++ b/util/log/log.go @@ -14,6 +14,7 @@ type Level int const ( LevelFatal Level = iota LevelInfo + LevelWarn LevelDebug LevelTrace ) @@ -36,6 +37,8 @@ func init() { level = LevelTrace case "fatal": level = LevelFatal + case "warn": + level = LevelWarn } } @@ -95,6 +98,16 @@ func Infof(format string, v ...interface{}) { WithLevelf(LevelInfo, format, v...) } +// Warn provides warn level logging +func Warn(v ...interface{}) { + WithLevel(LevelWarn, v...) +} + +// Warnf provides warn level logging +func Warnf(format string, v ...interface{}) { + WithLevelf(LevelWarn, format, v...) +} + // Fatal logs with Log and then exits with os.Exit(1) func Fatal(v ...interface{}) { WithLevel(LevelFatal, v...)