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

feat(logger): implement slog.Handler #13

Merged
merged 11 commits into from Nov 7, 2023
Merged

feat(logger): implement slog.Handler #13

merged 11 commits into from Nov 7, 2023

Conversation

aymanbagabas
Copy link
Member

@aymanbagabas aymanbagabas commented Feb 22, 2023

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: #8

@codecov
Copy link

codecov bot commented Feb 22, 2023

Welcome to Codecov 🎉

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 ☂️

@aymanbagabas aymanbagabas changed the title POC: feat(slog): implement slog.Handler feat(logger): implement slog.Handler Mar 21, 2023
@owenthereal
Copy link

👋 Was looking for bridging with slog. Any update?

@bashbunni
Copy link
Member

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

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

@EwenQuim
Copy link

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 jamietanna mentioned this pull request Sep 12, 2023
@jamietanna
Copy link

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

@aymanbagabas
Copy link
Member Author

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
Copy link

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 🙌

@jamietanna
Copy link

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

Copy link
Member

@caarlos0 caarlos0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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

@aymanbagabas
Copy link
Member Author

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

@aymanbagabas aymanbagabas merged commit de79c17 into main Nov 7, 2023
16 checks passed
@aymanbagabas aymanbagabas deleted the slog branch November 7, 2023 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support x/exp/slog
7 participants