charmbracelet / log

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

feat(logger): implement slog.Handler #13

Closed aymanbagabas closed 1 year ago

aymanbagabas commented 1 year ago

Uses x/exp/slog for go1.19 and go1.20 and log/slog for >go1.21. Upgrades the minimum go version to go1.19

Example:

package main

import (
    "fmt"
    "time"

    "github.com/charmbracelet/log"
    "golang.org/x/exp/slog"
)

func main() {
    log.SetTimeFormat(time.Kitchen)
    log.SetLevel(log.DebugLevel)
    log.SetReportCaller(true)
    logger := slog.New(log.Default())
    logger.Debug("This is a debug message")
    logger.Info("This is an info message with attributes", "key", "value")
    sub := logger.With("attr", "value")
    sub.Warn("This is a warning message from a sublogger")
    group := sub.WithGroup("subgroup")
    group.Error("This is an error message from a group", fmt.Errorf("this is an error"))
}

Output:

12:54PM DEBU <app/main.go:16> This is a debug message
12:54PM INFO <app/main.go:17> This is an info message with attributes key=value
12:54PM WARN <app/main.go:19> This is a warning message from a sublogger attr=value
12:54PM ERRO <app/main.go:21> subgroup: This is an error message from a subgroup attr=value err="this is an error"

Fixes: https://github.com/charmbracelet/log/issues/8

codecov[bot] commented 1 year ago

Welcome to Codecov :tada:

Once merged to your default branch, Codecov will compare your coverage reports and display the results in this comment.

Thanks for integrating Codecov - We've got you covered :open_umbrella:

owenthereal commented 1 year ago

👋 Was looking for bridging with slog. Any update?

bashbunni commented 1 year ago

@owenthereal I believe we're just waiting for slog to be merged into the language as the API may change in the meantime. https://github.com/golang/go/issues/56345 (I think that's the right issue iirc)

Will be quick to merge once that's all up to date!

EwenQuim commented 1 year ago

Now that 1.21 released log/slog, is it possible to make this lib compatible ? It looks very pretty, better than the default slog TextHandler

jamietanna commented 1 year ago

Is there anything I can do to help get #74 merged in with this / with this PR released?

aymanbagabas commented 1 year ago

Is there anything I can do to help get #74 merged in with this / with this PR released?

Hi @jamietanna,

Thanks for working on #74. It would be awesome to make it use exp/slog on versions prior to go1.21. You could do so with the //go:build !go1.21 build tag

jamietanna commented 1 year ago

Ooh interesting!

Sure thing, I was thinking of only supporting as early as Go 1.20, as it's the earliest version of Go that the Go team support, but happy to add exp/slog, that's a good idea if we want to support old versions :raised_hands:

jamietanna commented 1 year ago

Thanks for carrying on with this I'd remembered yesterday I needed to get back to my PR, but appreciate you doing it 🙌🏽

aymanbagabas commented 1 year ago

It looks like it might be a breaking change though, as the JSON keys for some fields changed.

Yes, that's correct, there will be breaking changes in the next release