logger: breaking changes to log level parsing

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-08-27 18:55:08 +03:00
parent e98a93d530
commit 35d3e4b332
3 changed files with 23 additions and 33 deletions

View File

@ -11,15 +11,6 @@ import (
"time" "time"
) )
func init() {
lvl, err := GetLevel(os.Getenv("MICRO_LOG_LEVEL"))
if err != nil {
lvl = InfoLevel
}
DefaultLogger = NewLogger(WithLevel(lvl))
}
type defaultLogger struct { type defaultLogger struct {
enc *json.Encoder enc *json.Encoder
opts Options opts Options

View File

@ -1,24 +1,20 @@
package logger package logger
import (
"fmt"
)
// Level means logger level // Level means logger level
type Level int8 type Level int8
const ( const (
// TraceLevel level. Designates finer-grained informational events than the Debug. // TraceLevel level usually used to find bugs, very verbose
TraceLevel Level = iota - 2 TraceLevel Level = iota - 2
// DebugLevel level. Usually only enabled when debugging. Very verbose logging. // DebugLevel level used only when enabled debugging
DebugLevel DebugLevel
// InfoLevel level. General operational entries about what's going on inside the application. // InfoLevel level used for general info about what's going on inside the application
InfoLevel InfoLevel
// WarnLevel level. Non-critical entries that deserve eyes. // WarnLevel level used for non-critical entries
WarnLevel WarnLevel
// ErrorLevel level. Used for errors that should definitely be noted. // ErrorLevel level used for errors that should definitely be noted
ErrorLevel ErrorLevel
// FatalLevel level. Logs and then calls `os.Exit(1)`. highest level of severity. // FatalLevel level used for critical errors and then calls `os.Exit(1)`
FatalLevel FatalLevel
) )
@ -38,7 +34,7 @@ func (l Level) String() string {
case FatalLevel: case FatalLevel:
return "fatal" return "fatal"
} }
return "" return "info"
} }
// Enabled returns true if the given level is at or above this level. // Enabled returns true if the given level is at or above this level.
@ -46,22 +42,22 @@ func (l Level) Enabled(lvl Level) bool {
return lvl >= l return lvl >= l
} }
// GetLevel converts a level string into a logger Level value. // ParseLevel converts a level string into a logger Level value.
// returns an error if the input string does not match known values. // returns an InfoLevel if the input string does not match known values.
func GetLevel(levelStr string) (Level, error) { func ParseLevel(lvl string) Level {
switch levelStr { switch lvl {
case TraceLevel.String(): case TraceLevel.String():
return TraceLevel, nil return TraceLevel
case DebugLevel.String(): case DebugLevel.String():
return DebugLevel, nil return DebugLevel
case InfoLevel.String(): case InfoLevel.String():
return InfoLevel, nil return InfoLevel
case WarnLevel.String(): case WarnLevel.String():
return WarnLevel, nil return WarnLevel
case ErrorLevel.String(): case ErrorLevel.String():
return ErrorLevel, nil return ErrorLevel
case FatalLevel.String(): case FatalLevel.String():
return FatalLevel, nil return FatalLevel
} }
return InfoLevel, fmt.Errorf("unknown Level String: '%s', use InfoLevel", levelStr) return InfoLevel
} }

View File

@ -1,11 +1,14 @@
// Package logger provides a log interface // Package logger provides a log interface
package logger package logger
import "context" import (
"context"
"os"
)
var ( var (
// DefaultLogger variable // DefaultLogger variable
DefaultLogger Logger = NewLogger() DefaultLogger Logger = NewLogger(WithLevel(ParseLevel(os.Getenv("MICRO_LOG_LEVEL"))))
// DefaultLevel used by logger // DefaultLevel used by logger
DefaultLevel Level = InfoLevel DefaultLevel Level = InfoLevel
// DefaultCallerSkipCount used by logger // DefaultCallerSkipCount used by logger