Skip to content

Commit

Permalink
feat: improve log logic (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq authored Sep 26, 2022
1 parent e6d5e37 commit b9c0b11
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/xlog/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@

package xlog

import "go.uber.org/zap"
import (
"context"

"go.uber.org/zap"
)

// L returns the standard logger.
func L(ctx context.Context) *Logger {
return FromContext(ctx)
}

// Jupiter returns framework logger
func Jupiter() *Logger {
Expand All @@ -33,6 +42,8 @@ func Default() *Logger {
func SetDefault(logger *Logger) {
defaultLogger = logger
stdLogger = defaultLogger.WithOptions(zap.AddCallerSkip(1))
zap.ReplaceGlobals(defaultLogger)
zap.RedirectStdLog(defaultLogger)
}

// Debug logs a message at DebugLevel. The message includes any fields passed
Expand Down
4 changes: 4 additions & 0 deletions pkg/xlog/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func NewContext(ctx context.Context, l *Logger) context.Context {
}

func FromContext(ctx context.Context) *Logger {
if ctx == nil {
return defaultLogger
}

l, ok := ctx.Value(loggerKey{}).(*Logger)
if !ok {
return defaultLogger // default logger
Expand Down

0 comments on commit b9c0b11

Please sign in to comment.