[W.I.P] refactor(logger): logger fields changed to map[string]interface{} (#1198)

* support unix daemon socket

* refactor(logger): logger fields changed to map[string]interface{}

* improvement(logger): adding string to Level Parser

* improvement(logger): rename ParseLevel to GetLevel
This commit is contained in:
Sumanth Chinthagunta 2020-02-15 10:19:28 -08:00 committed by GitHub
parent 964b7dee3f
commit fc5339a368
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 44 deletions

View File

@ -1,43 +0,0 @@
package logger
type FieldType uint8
type Encode func(*Field) string
type Field struct {
Key string
Type FieldType
Value interface{}
Encode Encode
}
func (f *Field) GetValue() interface{} {
if f.Encode != nil {
return f.Encode(f)
}
return f.Value
}
// preset common types for choosing encoder faster
const (
UnknownType FieldType = iota
BoolType
DurationType
Float64Type
Float32Type
Int64Type
Int32Type
Int16Type
Int8Type
Uint64Type
Uint32Type
Uint16Type
Uint8Type
StringType
TimeType
)
func Bool(key string, val bool) Field {
return Field{Key: key, Type: BoolType, Value: val}
}

View File

@ -11,3 +11,23 @@ const (
PanicLevel
FatalLevel
)
func (l Level) String() string {
switch l {
case TraceLevel:
return "trace"
case DebugLevel:
return "debug"
case InfoLevel:
return "info"
case WarnLevel:
return "warn"
case ErrorLevel:
return "error"
case FatalLevel:
return "fatal"
case PanicLevel:
return "panic"
}
return ""
}

View File

@ -20,7 +20,9 @@ type Logger interface {
// fmt.Printf.
Logf(level Level, format string, v ...interface{})
// Fields set fields to always be logged
Fields(fields ...Field) Logger
Fields(fields map[string]interface{}) Logger
// Error set `error` field to be logged
Error(err error) Logger
// SetLevel updates the logging level.
SetLevel(Level)
// String returns the name of logger
@ -47,3 +49,25 @@ func GetLogger(name string) (Logger, error) {
return l, nil
}
// GetLevel converts a level string into a logger Level value.
// returns an error if the input string does not match known values.
func GetLevel(levelStr string) (Level, error) {
switch levelStr {
case TraceLevel.String():
return TraceLevel, nil
case DebugLevel.String():
return DebugLevel, nil
case InfoLevel.String():
return InfoLevel, nil
case WarnLevel.String():
return WarnLevel, nil
case ErrorLevel.String():
return ErrorLevel, nil
case FatalLevel.String():
return FatalLevel, nil
case PanicLevel.String():
return PanicLevel, nil
}
return InfoLevel, fmt.Errorf("Unknown Level String: '%s', defaulting to NoLevel", levelStr)
}