Skip to content

Latest commit

 

History

History
60 lines (45 loc) · 879 Bytes

README.md

File metadata and controls

60 lines (45 loc) · 879 Bytes

logfx

zerolog for fx dependency injection library.

import "github.com/worldline-go/logz/logfx"

Usage

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()
}