From 40c4bfa2f9975cedacd86fe80ab63a857edbb4a4 Mon Sep 17 00:00:00 2001 From: Sumanth Chinthagunta Date: Fri, 14 Feb 2020 22:56:46 -0800 Subject: [PATCH] refactor(logger): logger fields changed to map[string]interface{} --- go.mod | 2 ++ go.sum | 14 ++----------- options.go | 10 ++-------- stackdriver_hook.go | 34 -------------------------------- zerolog.go | 41 ++++++++++---------------------------- zerolog_test.go | 48 ++++++++++++++++++--------------------------- 6 files changed, 35 insertions(+), 114 deletions(-) delete mode 100644 stackdriver_hook.go diff --git a/go.mod b/go.mod index 3243de7..35bb462 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,8 @@ module github.com/micro/go-plugins/logger/zap/v2 go 1.13 +replace github.com/micro/go-micro/v2 => github.com/xmlking/go-micro/v2 v2.0.0-20200215060542-c8f6d351d692 + require ( github.com/micro/go-micro/v2 v2.0.1-0.20200212105717-d76baf59de2e github.com/rs/zerolog v1.18.0 diff --git a/go.sum b/go.sum index f8c589a..f355d69 100644 --- a/go.sum +++ b/go.sum @@ -70,21 +70,16 @@ github.com/containerd/go-runc v0.0.0-20180907222934-5a6d9f37cfa3/go.mod h1:IV7qH github.com/containerd/ttrpc v0.0.0-20190828154514-0e0f228740de/go.mod h1:PvCDdDGpgqzQIzDW1TphrGLssLDZp2GuS+X5DkEJB8o= github.com/containerd/typeurl v0.0.0-20180627222232-a93fcdb778cd/go.mod h1:Cm3kwCdlkCfMSHURc+r6fwoGH6/F1hH3S4sg0rLFWPc= github.com/coreos/bbolt v1.3.3/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= -github.com/coreos/etcd v3.3.18+incompatible h1:Zz1aXgDrFFi1nadh58tA9ktt06cmPTwNNP3dXwIq1lE= github.com/coreos/etcd v3.3.18+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f h1:JOrtw2xFKzlg+cbHpyrpLDmnN1HqhBfnX7WDiW7eG2c= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f h1:lBNOc5arjvs8E5mO2tbpBpLoyyu8B6e44T7hJy6potg= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpu/goacmedns v0.0.1/go.mod h1:sesf/pNnCYwUevQEQfEwY0Y3DydlQWSGZbaMElOWxok= -github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8= github.com/dnaeon/go-vcr v0.0.0-20180814043457-aafff18a5cc2/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E= @@ -123,7 +118,6 @@ github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6 github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= -github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -134,7 +128,6 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= @@ -148,7 +141,6 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= -github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= @@ -182,7 +174,6 @@ github.com/joncalhoun/qson v0.0.0-20170526102502-8a9cab3a62b1/go.mod h1:DFXrEwSR github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= @@ -215,10 +206,7 @@ github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp github.com/mattn/go-tty v0.0.0-20180219170247-931426f7535a/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mholt/certmagic v0.9.1/go.mod h1:nu8jbsbtwK4205EDH/ZUMTKsfYpJA1Q7MKXHfgTihNw= -github.com/micro/cli/v2 v2.1.2-0.20200203150404-894195727d9c h1:oohy8v2QQeXfDe9/BaM0b+5wETzoMiemOs3fhPhnFTg= github.com/micro/cli/v2 v2.1.2-0.20200203150404-894195727d9c/go.mod h1:EguNh6DAoWKm9nmk+k/Rg0H3lQnDxqzu5x5srOtGtYg= -github.com/micro/go-micro/v2 v2.0.1-0.20200212105717-d76baf59de2e h1:EXYgiOLVc7zkUCIsAc++GQiOvPRh/gaGl++fqY1807g= -github.com/micro/go-micro/v2 v2.0.1-0.20200212105717-d76baf59de2e/go.mod h1:CDPVByZzOp1RNrJfNxEGgNOJ11wEw8NoHfADo8M3+LM= github.com/micro/mdns v0.3.0/go.mod h1:KJ0dW7KmicXU2BV++qkLlmHYcVv7/hHnbtguSWt9Aoc= github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.15/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= @@ -327,6 +315,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1: github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs= github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +github.com/xmlking/go-micro/v2 v2.0.0-20200215060542-c8f6d351d692 h1:ob2te89eNFfGpsishbeWyQAKpXwNMBAzhlpjv5J34rc= +github.com/xmlking/go-micro/v2 v2.0.0-20200215060542-c8f6d351d692/go.mod h1:CDPVByZzOp1RNrJfNxEGgNOJ11wEw8NoHfADo8M3+LM= github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk= diff --git a/options.go b/options.go index 14280d0..67659d8 100644 --- a/options.go +++ b/options.go @@ -1,4 +1,4 @@ -package zero +package zerolog import ( "context" @@ -36,12 +36,6 @@ func WithProductionMode() logger.Option { return setOption(productionModeKey{}, true) } -type gcpModeKey struct{} - -func WithGCPMode() logger.Option { - return setOption(gcpModeKey{}, true) -} - type outKey struct{} func WithOut(out io.Writer) logger.Option { @@ -50,7 +44,7 @@ func WithOut(out io.Writer) logger.Option { type fieldsKey struct{} -func WithFields(fields ...logger.Field) logger.Option { +func WithFields(fields map[string]interface{}) logger.Option { return setOption(fieldsKey{}, fields) } diff --git a/stackdriver_hook.go b/stackdriver_hook.go deleted file mode 100644 index 29caaac..0000000 --- a/stackdriver_hook.go +++ /dev/null @@ -1,34 +0,0 @@ -package zero - -import ( - "github.com/rs/zerolog" -) - -type stackdriverSeverityHook struct{} - -func (h stackdriverSeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) { - e.Str("severity", LevelToSeverity(level)) -} - -// LevelToSeverity converts a zerolog level to the stackdriver severity -// Stackdriver has more levels than zerolog so we skip some severities. -// By default we set info when no level is provided. -var LevelToSeverity = func(level zerolog.Level) string { - switch level { - case zerolog.NoLevel: - return "Default" - case zerolog.DebugLevel: - return "Debug" - // Let info falls into the default - case zerolog.WarnLevel: - return "Warning" - case zerolog.ErrorLevel: - return "Error" - case zerolog.FatalLevel: - return "Alert" - case zerolog.PanicLevel: - return "Emergency" - default: - return "Info" - } -} diff --git a/zerolog.go b/zerolog.go index 0ca9f97..ea92ce6 100644 --- a/zerolog.go +++ b/zerolog.go @@ -1,4 +1,4 @@ -package zero +package zerolog import ( "context" @@ -6,7 +6,6 @@ import ( "io" "os" "runtime/debug" - "time" "github.com/micro/go-micro/v2/logger" "github.com/rs/zerolog" @@ -19,7 +18,6 @@ type Mode uint8 const ( Production Mode = iota + 1 Development - GCP ) var ( @@ -34,7 +32,7 @@ var ( // The logging level the logger should log at. // This defaults to 100 means not explicitly set by user level logger.Level = 100 - fields []logger.Field + fields map[string]interface{} hooks []zerolog.Hook timeFormat string // default Production (1) @@ -45,12 +43,8 @@ type zeroLogger struct { nativelogger zerolog.Logger } -func (l *zeroLogger) Fields(fields ...logger.Field) logger.Logger { - data := make(map[string]interface{}, len(fields)) - for _, f := range fields { - data[f.Key] = f.GetValue() - } - return &zeroLogger{l.nativelogger.With().Fields(data).Logger()} +func (l *zeroLogger) Fields(fields map[string]interface{}) logger.Logger { + return &zeroLogger{l.nativelogger.With().Fields(fields).Logger()} } func (l *zeroLogger) Error(err error) logger.Logger { @@ -72,7 +66,7 @@ func (l *zeroLogger) Init(opts ...logger.Option) error { if hs, ok := options.Context.Value(hooksKey{}).([]zerolog.Hook); ok { hooks = hs } - if flds, ok := options.Context.Value(fieldsKey{}).([]logger.Field); ok { + if flds, ok := options.Context.Value(fieldsKey{}).(map[string]interface{}); ok { fields = flds } if lvl, ok := options.Context.Value(levelKey{}).(logger.Level); ok { @@ -96,9 +90,6 @@ func (l *zeroLogger) Init(opts ...logger.Option) error { if prodMode, ok := options.Context.Value(productionModeKey{}).(bool); ok && prodMode { mode = Production } - if gcpMode, ok := options.Context.Value(gcpModeKey{}).(bool); ok && gcpMode { - mode = GCP - } switch mode { case Development: @@ -108,7 +99,9 @@ func (l *zeroLogger) Init(opts ...logger.Option) error { } consOut := zerolog.NewConsoleWriter( func(w *zerolog.ConsoleWriter) { - w.TimeFormat = time.RFC3339 + if len(timeFormat) > 0 { + w.TimeFormat = timeFormat + } w.Out = out w.NoColor = false }, @@ -117,16 +110,6 @@ func (l *zeroLogger) Init(opts ...logger.Option) error { l.nativelogger = zerolog.New(consOut). Level(zerolog.DebugLevel). With().Timestamp().Stack().Logger() - case GCP: - zerolog.LevelFieldName = "severity" - zerolog.TimestampFieldName = "timestamp" - timeFormat = time.RFC3339Nano - // Adds stackdriver severity hook - hooks = append(hooks, stackdriverSeverityHook{}) - - l.nativelogger = zerolog.New(out). - Level(zerolog.InfoLevel). - With().Timestamp().Stack().Logger() default: // Production zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack l.nativelogger = zerolog.New(out). @@ -160,14 +143,10 @@ func (l *zeroLogger) Init(opts ...logger.Option) error { // Adding seed fields if exist if fields != nil { - data := make(map[string]interface{}, len(fields)) - for _, f := range fields { - data[f.Key] = f.GetValue() - } - l.nativelogger = l.nativelogger.With().Fields(data).Logger() + l.nativelogger = l.nativelogger.With().Fields(fields).Logger() } - // Also set it as Default zerolog logger + // Also set it as zerolog's Default logger if useAsDefault { zlog.Logger = l.nativelogger } diff --git a/zerolog_test.go b/zerolog_test.go index f19d8f1..bf6f12b 100644 --- a/zerolog_test.go +++ b/zerolog_test.go @@ -1,8 +1,10 @@ -package zero +package zerolog import ( + "errors" "os" "testing" + "time" "github.com/micro/go-micro/v2/logger" "github.com/rs/zerolog" @@ -24,7 +26,7 @@ func TestName(t *testing.T) { // l.Logf(logger.InfoLevel, "testing: %s", "logf") // // Output: -// // {"level":"info","time":"2020-02-13T20:55:24-08:00","message":"testing: logf"} +// // {"level":"info","time":"2020-02-14T22:15:36-08:00","message":"testing: logf"} // } func TestSetLevel(t *testing.T) { @@ -42,45 +44,33 @@ func TestWithReportCaller(t *testing.T) { l.Logf(logger.InfoLevel, "testing: %s", "WithReportCaller") } + func TestWithOut(t *testing.T) { l := NewLogger(WithOut(os.Stdout)) l.Logf(logger.InfoLevel, "testing: %s", "WithOut") } -func TestWithPretty(t *testing.T) { - l := NewLogger(WithDevelopmentMode()) +func TestWithDevelopmentMode(t *testing.T) { + l := NewLogger(WithDevelopmentMode(), WithTimeFormat(time.Kitchen)) - l.Logf(logger.InfoLevel, "testing: %s", "WithPretty") -} -func TestWithLevelFieldName(t *testing.T) { - l := NewLogger(WithGCPMode()) - - l.Logf(logger.InfoLevel, "testing: %s", "WithLevelFieldName") - // reset `LevelFieldName` to make other tests pass. - NewLogger(WithProductionMode()) + l.Logf(logger.InfoLevel, "testing: %s", "DevelopmentMode") } func TestWithFields(t *testing.T) { l := NewLogger() - l.Fields([]logger.Field{ - { - Key: "sumo", - Type: logger.StringType, - Value: "demo", - }, - { - Key: "human", - Type: logger.BoolType, - Value: true, - }, - { - Key: "age", - Type: logger.Int32Type, - Value: 99, - }, - }...).Logf(logger.InfoLevel, "testing: %s", "WithFields") + l.Fields(map[string]interface{}{ + "sumo": "demo", + "human": true, + "age": 99, + }).Logf(logger.InfoLevel, "testing: %s", "WithFields") +} + +func TestWithError(t *testing.T) { + l := NewLogger() + + l.Error(errors.New("I am Error")).Logf(logger.ErrorLevel, "testing: %s", "WithError") } func TestWithHooks(t *testing.T) {