diff options
-rw-r--r-- | plugins/logger/zap_adapter.go | 22 | ||||
-rw-r--r-- | tests/plugins/logger/plugin.go | 24 |
2 files changed, 45 insertions, 1 deletions
diff --git a/plugins/logger/zap_adapter.go b/plugins/logger/zap_adapter.go index fab59844..0649bb4d 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,30 @@ func NewZapAdapter(zapLogger *zap.Logger) *ZapAdapter { } } +func separateFields(keyVals []interface{}) (fields []zap.Field, individualKeyVals []interface{}) { + for _, value := range keyVals { + switch value := value.(type) { + case zap.Field: + fields = append(fields, value) + case core.ObjectMarshaler: + fields = append(fields, zap.Inline(value)) + default: + individualKeyVals = append(individualKeyVals, value) + } + } + return +} + 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 +49,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 } diff --git a/tests/plugins/logger/plugin.go b/tests/plugins/logger/plugin.go index aa62f2b3..e2227ec9 100644 --- a/tests/plugins/logger/plugin.go +++ b/tests/plugins/logger/plugin.go @@ -6,6 +6,8 @@ import ( "github.com/spiral/errors" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/logger" + "go.uber.org/zap" + core "go.uber.org/zap/zapcore" ) type Plugin struct { @@ -13,6 +15,14 @@ type Plugin struct { log logger.Logger } +type Loggable struct { +} + +func (l *Loggable) MarshalLogObject(encoder core.ObjectEncoder) error { + encoder.AddString("error", "Example marshaller error") + return nil +} + func (p1 *Plugin) Init(cfg config.Configurer, log logger.Logger) error { p1.config = cfg p1.log = log @@ -26,6 +36,20 @@ func (p1 *Plugin) Serve() chan error { p1.log.Debug("error", "test", errors.E(errors.Str("test"))) p1.log.Warn("error", "test", errors.E(errors.Str("test"))) + field := zap.String("error", "Example field error") + + p1.log.Error("error", field) + p1.log.Info("error", field) + p1.log.Debug("error", field) + p1.log.Warn("error", field) + + marshalledObject := Loggable{} + + p1.log.Error("error", marshalledObject) + p1.log.Info("error", marshalledObject) + p1.log.Debug("error", marshalledObject) + p1.log.Warn("error", marshalledObject) + p1.log.Error("error", "test") p1.log.Info("error", "test") p1.log.Debug("error", "test") |