Skip to content

Setting channel name cross multiple loggers #108

Closed
@nhi-vanye

Description

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...

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

enhancementNew feature or requestquestionFurther information is requestedresolved

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions