rs / zerolog

Zero Allocation JSON Logger
MIT License
10.39k stars 566 forks source link

Creating sublogger from global log does not propagate global level #624

Closed wilsonehusin closed 9 months ago

wilsonehusin commented 9 months ago

Play: https://go.dev/play/p/uuTKVfAzWke

zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Info().Int8("level", int8(zerolog.GlobalLevel())).Send() // 1 = INFO

sub := log.With().Bool("sub", true).Logger()
sub.Info().Int8("level", int8(sub.GetLevel())).Send() // -1 = TRACE

This took me by surprise and wondering if it was intentional? Happy to send the patch, but for now my workaround is to do:

- sub := log.With().Bool("sub", true).Logger()
+ sub := log.Level(zerolog.GlobalLevel()).With().Bool("sub", true).Logger()
rs commented 9 months ago

The default logger does not have the global level set either. A local level on a logger can be set to a higher level than the global level to further restrict what is logged with this particular logger.

wilsonehusin commented 9 months ago

Oh interesting! I see why that makes sense now. Thanks!