eclipse / paho.golang

Go libraries
Other
341 stars 93 forks source link

Use log/slog for logging #169

Open vishnureddy17 opened 1 year ago

vishnureddy17 commented 1 year ago

Go now has a nicer logging package in the standard library. It might be better to use that if the Go ecosystem is going to converge on that.

MattBrittan commented 11 months ago

Our current Logger is:

Logger interface {
        Println(v ...interface{})
        Printf(format string, v ...interface{})
    }

This is a subset of log.Logger so can be used with slog pretty easily:

h := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})

cliCfg := autopaho.ClientConfig{
    Errors:         slog.NewLogLogger(h.WithAttrs([]slog.Attr{slog.String("source", "autoErrors")}), slog.LevelError),
    Debug:          slog.NewLogLogger(h.WithAttrs([]slog.Attr{slog.String("source", "autoDebug")}), slog.LevelDebug),
    PahoErrors:     slog.NewLogLogger(h.WithAttrs([]slog.Attr{slog.String("source", "pahoErrors")}), slog.LevelError),
    PahoDebug:      slog.NewLogLogger(h.WithAttrs([]slog.Attr{slog.String("source", "pahoDebug")}), slog.LevelDebug),
...

Of course this does not enable the library itself to output structured messages.

Advantages of using slog:

Disadvantages of using slog:

I'm on the fence with this one; the above was really me trying to put my thoughts in order! Either way the logging could do with some work...