Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting channel name cross multiple loggers #108

Closed
nhi-vanye opened this issue Jul 1, 2023 · 3 comments
Closed

Setting channel name cross multiple loggers #108

nhi-vanye opened this issue Jul 1, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request question Further information is requested resolved

Comments

@nhi-vanye
Copy link

What is the best approach to tracking which "application subsystem" a message is coming from?

Normally I would create one logger per subsystem and configure its "name" or "channel".

But it seems that channel is only referenced in the record and its using the global DefaultCannelName

in probe.go I have

var probeLogger = slog.NewWithName("probe")

func init() {

    slog.DefaultChannelName = "probe"
    slog.DefaultTimeFormat = "2006-01-02 15:04:05.000"

    h1 := handler.NewConsoleHandler(slog.AllLevels)
    f := slog.AsTextFormatter(h1.Formatter())
    f.SetTemplate(utils.LogTemplate)
    probeLogger.AddHandlers(h1)

    probeLogger.Infof("Probing %s", viper.GetString("read"))
...

where as in root.go I have

var rootLogger = slog.NewWithName("root")

func init() {

    slog.DefaultChannelName = "root"
    slog.DefaultTimeFormat = "2006-01-02 15:04:05.000"

    h1 := handler.NewConsoleHandler(slog.AllLevels)
    f := slog.AsTextFormatter(h1.Formatter())
    f.SetTemplate(utils.LogTemplate)
    rootLogger.AddHandlers(h1)

All of probe.go's messages are using root

I have worked around it by using a subsystem specific template that hardcodes the name, but that seems unclean...

@inhere inhere added the question Further information is requested label Jul 1, 2023
@inhere
Copy link
Member

inhere commented Jul 1, 2023

👍 Thanks for your feedback, I will add logger.ChannelName option setting in next version.

var rootLogger = slog.NewWithName("root", func(l *slog.Logger) {
   // l.ChannelName = "root"
   // or 
   l.ChannelName = l.Name()
})

if not sets, will use slog. DefaultChannelName.


The time format can be sets by:

f := slog.AsTextFormatter(h1. Formatter())
f.TimeFormat = "2006-01-02 15:04:05.000"

@inhere inhere added the enhancement New feature or request label Jul 2, 2023
@nhi-vanye
Copy link
Author

Super-big thumbs up...

@inhere
Copy link
Member

inhere commented Jul 12, 2023

hi @nhi-vanye released on the v0.5.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested resolved
Projects
None yet
Development

No branches or pull requests

2 participants