zerolog for fx dependency injection library.
import "github.com/worldline-go/logz/logfx"
Simple usage with default logger
fx.WithLogger(logfx.New)
With custom logger
fx.WithLogger(func() fxevent.Logger {
return logfx.New(logfx.WithLogger(log.Logger.With().Str("component", "test").Logger()))
})
Detailed version with config loader.
type Config struct {
LogLevel string
}
func LoadConfig() (*Config, error) {
cfg := &Config{
LogLevel: "info",
}
// set loglevel again
logz.SetLogLevel(cfg.LogLevel)
return cfg, nil
}
func SetLogger() fxevent.Logger {
logz.InitializeLog(logz.WithServiceInfo(ServiceName, ServiceVersion))
logz.SetLogLevel(DefaultLogLevel)
return logfx.New()
}
func main() {
fx.New(
fx.WithLogger(SetLogger),
fx.Provide(
LoadConfig,
// ...
),
// ...
).Run()
}