You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to figure out how to use zap across multiple files. It seems I'm not the only one with this confusion. In that link though, the OP chooses to replace globals, but the FAQ says to avoid when possible. I'm am not migrating, so I don't think that's the right solution for me.
In all the examples that are provided in the docs, zap is used only in main.go, but I've yet to find any examples on the docs (or elsewhere), with multiple files. This is how I am attempting to use zap:
In pkg/util/logger.go, I have
type ZapLogger struct {
ZLog *zap.Logger
}
func ZapLog() *ZapLogger {
lgr, err := zap.NewProduction()
if err != nil {
log.Fatalf("Status: Failed to initialize zap logger: %v", err)
}
lgr.Info("Zap log is now initialized")
return &ZapLogger{ZLog: lgr}
}
func (z *ZapLogger) DebugLog(msg string, fields ...zap.Field) {
if debugEnabled {
z.ZLog.Debug(msg, zap.Field...)
}
}
Is there any way to register the *zap.Logger instance as a global, so that I will only set the format in main.go and use it in any other files in this runtime? Just like Python's logging or loguru.
I'm trying to figure out how to use zap across multiple files. It seems I'm not the only one with this confusion. In that link though, the OP chooses to replace globals, but the FAQ says to avoid when possible. I'm am not migrating, so I don't think that's the right solution for me.
In all the examples that are provided in the docs, zap is used only in
main.go
, but I've yet to find any examples on the docs (or elsewhere), with multiple files. This is how I am attempting to use zap:In
pkg/util/logger.go
, I haveThen in
cmd/server/server.go
, I have:Then in other packages where I need to log, I import
pkg/util
, and callutil.DebugLog("asdf", zap.String("qwerty")
Would this be the correct or an acceptable way of using zap?
The text was updated successfully, but these errors were encountered: