Allow to store and retrieve logger in `context.Context` object
cardil opened this issue · 0 comments
cardil commented
The standard Golang context.Context
allows storing and retrieve objects using WithValue
and Value
func.
It would be a good idea to create a common API to configure slf4go using context.Context
API, apart from existing usage of global variable. The current approach of using global variables (globalLogger
, and globalDriver
) are not always acceptable in some projects, as it's susceptible to race conditions to occur.
An example of a new API:
// configure logger: driver, level, etc. withing context
d := slogzap.Driver{/* ... */}
ctx = slog.WithLogger(ctx, slog.Config{Driver: d})
// usage
func someUserFunc(ctx context.Context) {
log := slog.FromContext(ctx)
log.Info("Example msg")
}