Skip to content

Commit

Permalink
[ZAP] Implement new option interface and add caller
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Feb 6, 2021
1 parent 95d9c8b commit cacb5e3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
56 changes: 50 additions & 6 deletions zap/adapter.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,76 @@
package zapadapter

import (
"runtime"

"github.com/graphmetrics/logger-go"
"github.com/graphmetrics/logger-go/options"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type zapAdapter struct {
parent *zap.Logger
parent *zap.Logger
callerSkipOffset int
}

func New(parent *zap.Logger) logger.Logger {
return &zapAdapter{parent: parent}
return &zapAdapter{parent: parent, callerSkipOffset: 0}
}

func (z *zapAdapter) Debug(msg string, metadata map[string]interface{}) {
z.parent.Debug(msg, prepareFields(metadata)...)
if ce := z.parent.Check(zap.DebugLevel, msg); ce != nil {
ce.Caller = z.caller()
ce.Write(prepareFields(metadata)...)
}
}

func (z *zapAdapter) Info(msg string, metadata map[string]interface{}) {
z.parent.Info(msg, prepareFields(metadata)...)
if ce := z.parent.Check(zap.InfoLevel, msg); ce != nil {
ce.Caller = z.caller()
ce.Write(prepareFields(metadata)...)
}
}

func (z *zapAdapter) Warn(msg string, metadata map[string]interface{}) {
z.parent.Warn(msg, prepareFields(metadata)...)
if ce := z.parent.Check(zap.WarnLevel, msg); ce != nil {
ce.Caller = z.caller()
ce.Write(prepareFields(metadata)...)
}
}

func (z *zapAdapter) Error(msg string, metadata map[string]interface{}) {
z.parent.Error(msg, prepareFields(metadata)...)
if ce := z.parent.Check(zap.ErrorLevel, msg); ce != nil {
ce.Caller = z.caller()
ce.Write(prepareFields(metadata)...)
}
}

func (z *zapAdapter) WithOptions(opts ...options.LoggerOption) logger.Logger {
adapter := z
for _, o := range opts {
switch o.Parameter() {
case "CallerSkipOffset":
adapter = &zapAdapter{
parent: adapter.parent,
callerSkipOffset: adapter.callerSkipOffset + o.Value().(int),
}
break
case "Named":
adapter = &zapAdapter{
parent: adapter.parent.Named(o.Value().(string)),
callerSkipOffset: adapter.callerSkipOffset,
}
break
default:
break
}
}
return adapter
}

func (z *zapAdapter) caller() zapcore.EntryCaller {
return zapcore.NewEntryCaller(runtime.Caller(z.callerSkipOffset + 2))
}

func prepareFields(metadata map[string]interface{}) []zap.Field {
Expand Down
2 changes: 1 addition & 1 deletion zap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/graphmetrics/logger-go/zap
go 1.15

require (
github.com/graphmetrics/logger-go v0.1.0
github.com/graphmetrics/logger-go v0.2.0
go.uber.org/zap v1.16.0
)
4 changes: 2 additions & 2 deletions zap/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/graphmetrics/logger-go v0.1.0 h1:sLZzQBa1YLJofcaPtbzZRmvnqFhZSU7+qjKx22OWQSQ=
github.com/graphmetrics/logger-go v0.1.0/go.mod h1:T98PXH1RF/nRghhhnKr8S7N96H5A7beuXXwzJaBl0dw=
github.com/graphmetrics/logger-go v0.2.0 h1:vzxwoe3wtvXHqh7urQ4MYtrOjlwDGDxEOsmEy9NH+o8=
github.com/graphmetrics/logger-go v0.2.0/go.mod h1:T98PXH1RF/nRghhhnKr8S7N96H5A7beuXXwzJaBl0dw=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down

0 comments on commit cacb5e3

Please sign in to comment.