charmbracelet / log

A minimal, colorful Go logging library 🪵
MIT License
2.4k stars 67 forks source link

Sublogger with prefix? #36

Closed tmm1 closed 1 year ago

tmm1 commented 1 year ago

I would like to be able to set some options on the default logger (like timestamp format, loglevel), then create sub-loggers which inherit those settings but have their own prefixes.

Right now it seems the only way to create a sub-logger is With() which adds key/value pairs but in my case I don't have key/value pairs to add I just want a new prefix.

WDYT about adding a new WithPrefix()?

aymanbagabas commented 1 year ago

You could use SetPrefix() on the sub-logger.

sub := log.With()
sub.SetPrefix("myapp")
...
sub.Info("hello world")
tmm1 commented 1 year ago

Yep that works, thanks!

I would still like to see a convenience wrapper to make it a one liner..

log.WithPrefix("myapp").Info("hello world")