summaryrefslogtreecommitdiff
path: root/plugins/logger
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-11-18 18:15:09 +0300
committerValery Piashchynski <[email protected]>2020-11-18 18:15:09 +0300
commit8fab090abc369237d5f9be2ee676005b24c2a470 (patch)
tree9f7fc358b66357f0218b8256d8073616995b91db /plugins/logger
parent4ccd58fc363264d24f642ab7e0ccfe6538a0b91c (diff)
Handler test
Diffstat (limited to 'plugins/logger')
-rw-r--r--plugins/logger/plugin.go6
-rw-r--r--plugins/logger/zap_adapter.go57
2 files changed, 60 insertions, 3 deletions
diff --git a/plugins/logger/plugin.go b/plugins/logger/plugin.go
index 0a8485d9..2937056c 100644
--- a/plugins/logger/plugin.go
+++ b/plugins/logger/plugin.go
@@ -35,7 +35,7 @@ func (z *ZapLogger) Init(cfg config.Configurer) error {
// DefaultLogger returns default logger.
func (z *ZapLogger) DefaultLogger() (log.Logger, error) {
- return log.NewZapAdapter(z.base), nil
+ return NewZapAdapter(z.base), nil
}
// NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params.
@@ -45,10 +45,10 @@ func (z *ZapLogger) NamedLogger(name string) (log.Logger, error) {
if err != nil {
return nil, err
}
- return log.NewZapAdapter(l), nil
+ return NewZapAdapter(l), nil
}
- return log.NewZapAdapter(z.base.Named(name)), nil
+ return NewZapAdapter(z.base.Named(name)), nil
}
// NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params.
diff --git a/plugins/logger/zap_adapter.go b/plugins/logger/zap_adapter.go
new file mode 100644
index 00000000..41e6d8f9
--- /dev/null
+++ b/plugins/logger/zap_adapter.go
@@ -0,0 +1,57 @@
+package logger
+
+import (
+ "fmt"
+
+ "github.com/spiral/roadrunner/v2/interfaces/log"
+ "go.uber.org/zap"
+)
+
+type ZapAdapter struct {
+ zl *zap.Logger
+}
+
+// Create NewZapAdapter which uses general log interface
+func NewZapAdapter(zapLogger *zap.Logger) *ZapAdapter {
+ return &ZapAdapter{
+ zl: zapLogger.WithOptions(zap.AddCallerSkip(1)),
+ }
+}
+
+func (log *ZapAdapter) fields(keyvals []interface{}) []zap.Field {
+ // 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))}
+ }
+
+ var fields []zap.Field
+ 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]))
+ }
+
+ 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{}) log.Logger {
+ return NewZapAdapter(log.zl.With(log.fields(keyvals)...))
+}