diff options
author | Valery Piashchynski <[email protected]> | 2021-09-16 17:12:37 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-09-16 17:12:37 +0300 |
commit | f3491c089b4da77fd8d2bc942a88b6b8d117a8a5 (patch) | |
tree | 32bfffb1f24eeee7b909747cc00a6a6b9fd3ee83 /plugins/logger/zap_adapter.go | |
parent | 5d2cd55ab522d4f1e65a833f91146444465a32ac (diff) |
Move plugins to a separate repository
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'plugins/logger/zap_adapter.go')
-rw-r--r-- | plugins/logger/zap_adapter.go | 79 |
1 files changed, 0 insertions, 79 deletions
diff --git a/plugins/logger/zap_adapter.go b/plugins/logger/zap_adapter.go deleted file mode 100644 index 1c68cf25..00000000 --- a/plugins/logger/zap_adapter.go +++ /dev/null @@ -1,79 +0,0 @@ -package logger - -import ( - "fmt" - - "go.uber.org/zap" - core "go.uber.org/zap/zapcore" -) - -type ZapAdapter struct { - zl *zap.Logger -} - -// NewZapAdapter ... which uses general log interface -func NewZapAdapter(zapLogger *zap.Logger) *ZapAdapter { - return &ZapAdapter{ - zl: zapLogger.WithOptions(zap.AddCallerSkip(1)), - } -} - -func separateFields(keyVals []interface{}) ([]zap.Field, []interface{}) { - var fields []zap.Field - var pairedKeyVals []interface{} - - for key := range keyVals { - switch value := keyVals[key].(type) { - case zap.Field: - fields = append(fields, value) - case core.ObjectMarshaler: - fields = append(fields, zap.Inline(value)) - default: - pairedKeyVals = append(pairedKeyVals, value) - } - } - return fields, pairedKeyVals -} - -func (log *ZapAdapter) fields(keyvals []interface{}) []zap.Field { - // separate any zap fields from other structs - zapFields, keyvals := separateFields(keyvals) - - // we should have even number of keys and values - if len(keyvals)%2 != 0 { - return []zap.Field{zap.Error(fmt.Errorf("odd number of keyvals pairs: %v", keyvals))} - } - - fields := make([]zap.Field, 0, len(keyvals)/2+len(zapFields)) - for i := 0; i < len(keyvals); i += 2 { - key, ok := keyvals[i].(string) - if !ok { - key = fmt.Sprintf("%v", keyvals[i]) - } - fields = append(fields, zap.Any(key, keyvals[i+1])) - } - // add all the fields - fields = append(fields, zapFields...) - - return fields -} - -func (log *ZapAdapter) Debug(msg string, keyvals ...interface{}) { - log.zl.Debug(msg, log.fields(keyvals)...) -} - -func (log *ZapAdapter) Info(msg string, keyvals ...interface{}) { - log.zl.Info(msg, log.fields(keyvals)...) -} - -func (log *ZapAdapter) Warn(msg string, keyvals ...interface{}) { - log.zl.Warn(msg, log.fields(keyvals)...) -} - -func (log *ZapAdapter) Error(msg string, keyvals ...interface{}) { - log.zl.Error(msg, log.fields(keyvals)...) -} - -func (log *ZapAdapter) With(keyvals ...interface{}) Logger { - return NewZapAdapter(log.zl.With(log.fields(keyvals)...)) -} |