From 56fd172eaff88bfc97553626794d5fb61c3a47c7 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 30 Aug 2017 14:00:22 +0900 Subject: [PATCH] ing --- glide.yaml | 1 - logging.go | 23 +++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/glide.yaml b/glide.yaml index deb7ac2..b968be4 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,5 +1,4 @@ package: git.loafle.net/commons_go/logging import: -- package: github.com/uber-go/zap - package: go.uber.org/zap version: v1.5.0 \ No newline at end of file diff --git a/logging.go b/logging.go index c2032f6..20ae409 100644 --- a/logging.go +++ b/logging.go @@ -5,34 +5,37 @@ import ( "os" "path" - "github.com/uber-go/zap" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) type loggerKeyType int const loggerKey loggerKeyType = iota -var defaultLogger zap.Logger +var defaultLogger *zap.Logger func init() { - defaultLogger = zap.New( - zap.NewJSONEncoder(zap.TimeFormatter(TimestampField)), - zap.Fields(zap.Int("pid", os.Getpid())), + l, _ := zap.NewDevelopment() + defaultLogger = l.With( + zap.Int("pid", os.Getpid()), zap.String("exe", path.Base(os.Args[0])), ) } -func NewContext(ctx context.Context, fields ...zap.Field) context.Context { +func NewContext(ctx context.Context, logger *zap.Logger, fields ...zapcore.Field) context.Context { + if nil != logger { + return context.WithValue(ctx, loggerKey, logger.With(fields...)) + } return context.WithValue(ctx, loggerKey, WithContext(ctx).With(fields...)) } -func WithContext(ctx context.Context) zap.Logger { +func WithContext(ctx context.Context) *zap.Logger { if ctx == nil { return defaultLogger } - if ctxLogger, ok := ctx.Value(loggerKey).(zap.Logger); ok { + if ctxLogger, ok := ctx.Value(loggerKey).(*zap.Logger); ok { return ctxLogger - } else { - return defaultLogger } + return defaultLogger }