Skip to content

slog support #6

Description

@earthboundkid

It would be nice to support log/slog. Maybe it should be a new library though.

Activity

  1. earthboundkid commented on Dec 17, 2023

    @earthboundkid
    Author

    API could be like what.Handler(myLogger); what.Slog("a", 1, "b", 2). Handler sets the slog Handler, and Slog sends messages to it.

  2. christophberger commented on Dec 18, 2023

    @christophberger
    Contributor

    Hi Carlana, that's an interesting idea.

    From my point of view, what and logging serve different purposes.

    • logging supports observability in production while also helping with debugging
    • what is a pure ad-hoc “information dumping” tool that is meant to be active during development only (and be non-existent in production code).

    Hence I don't see the advantage of “marrying” both concepts (but I am happy to learn).
    What problem would what.Slog() solve?

  3. earthboundkid commented on Dec 18, 2023

    @earthboundkid
    Author

    I find the output of slog easier to read because I have it colorized in dev. That would be the big advantage to me. That said, I’m not sure if it would make sense to retrofit an existing package with a clear scope of work.

  4. christophberger commented on Dec 19, 2023

    @christophberger
    Contributor

    I see, you want to use slog to get nicely colored what output. I like the idea.

    What colorizer would you use?

    I tried lmittmann/tint, but most of the colorization is about log levels (plus some grey for the attribute names and the time), so I would have to add what.Info(), what.Warn(), what.Error(), and what.Debug() functions to get a decent colorization. I feel that's a bit of an overkill. If leveled logging is required, I'd use slog directly. I would rather want to implement your minimal suggestion using just what.Slog(), with a default handler that does some nice auto-colorization based on ... ? attribute names? attribute position?

    My current attempt using tint:

    var logger = slog.New(
    	// set lmittmann/tint as the global logger's handler
    	// colorable enables color output on Windows
    	tint.NewHandler(colorable.NewColorableStderr(), &tint.Options{
    		Level:      slog.LevelInfo,
    		TimeFormat: time.Kitchen,
    	}),
    )
    
    func SlogHandler(h slog.Handler) {
    	logger = slog.New(h)
    }
    func Slog(msg string, args ...any) {
    	logger.Info(msg, args...)
    }
    Screenshot 2023-12-19 at 11 54 57
  5. earthboundkid commented on Dec 20, 2023

    @earthboundkid
    Author
  6. christophberger commented on Jan 6, 2024

    @christophberger
    Contributor

    Lost track of this over the holiday season...

    I modified what.Happens() to take Slog-like arguments if the format string is one of "DEBUG", "INFO", "WARN", or "ERROR". Example:

    what.Happens("INFO", "something happened", "count", 3, "sum", 10)

    I took your colorize.go as a starting point, @carlmjohnson, and implemented something similar but without involving slog. The changes have not been tagged yet; you can test it at commit b5d6749 if you like.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions