Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add AddCallerSkip for zap to show real caller #178

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions logging/zap_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,32 @@ type ZapLogger struct {

var _ Logger = (*ZapLogger)(nil)

// NewZapLogger creates a new logger wrapped the zap.Logger
func NewZapLogger(env LogLevel) (Logger, error) {
var config zap.Config

if env == Production {
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
return &ZapLogger{
logger: logger,
}, nil
config = zap.NewProductionConfig()
} else if env == Development {
logger, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
return &ZapLogger{
logger: logger,
}, nil
config = zap.NewDevelopmentConfig()
} else {
panic(fmt.Sprintf("Unknown environment. Expected %s or %s. Received %s.", Development, Production, env))
}

return NewZapLoggerByConfig(config, zap.AddCallerSkip(1))
}

// NewZapLoggerByConfig creates a logger wrapped the zap.Logger
// Note if the logger need to show the caller, need use `zap.AddCallerSkip(1)` ad options
func NewZapLoggerByConfig(config zap.Config, options ...zap.Option) (Logger, error) {
logger, err := config.Build(options...)
if err != nil {
panic(err)
}

return &ZapLogger{
logger: logger,
}, nil
}

func (z *ZapLogger) Debug(msg string, tags ...any) {
Expand Down
Loading