diff options
Diffstat (limited to 'plugins/logger')
-rw-r--r-- | plugins/logger/zap_adapter.go | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/plugins/logger/zap_adapter.go b/plugins/logger/zap_adapter.go index fab59844..1c68cf25 100644 --- a/plugins/logger/zap_adapter.go +++ b/plugins/logger/zap_adapter.go @@ -4,6 +4,7 @@ import ( "fmt" "go.uber.org/zap" + core "go.uber.org/zap/zapcore" ) type ZapAdapter struct { @@ -17,13 +18,33 @@ func NewZapAdapter(zapLogger *zap.Logger) *ZapAdapter { } } +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) + 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 { @@ -31,6 +52,8 @@ func (log *ZapAdapter) fields(keyvals []interface{}) []zap.Field { } fields = append(fields, zap.Any(key, keyvals[i+1])) } + // add all the fields + fields = append(fields, zapFields...) return fields } |