Skip to content

feat(logger): implement slog.Handler - #13

Merged
aymanbagabas merged 11 commits into
mainfrom
slog
Nov 7, 2023
Merged

aymanbagabas merged 11 commits into
mainfrom
slog

Conversation

@aymanbagabas

@aymanbagabas aymanbagabas commented Feb 22, 2023 •

Copy link
Copy Markdown
Contributor

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

codecov Bot commented Feb 22, 2023 •

Copy link
Copy Markdown

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 force-pushed the slog branch 2 times, most recently from 4b37215 to ffbea2a Compare March 21, 2023 16:19
@aymanbagabas aymanbagabas changed the title POC: feat(slog): implement slog.Handler feat(logger): implement slog.Handler Mar 21, 2023
@aymanbagabas
aymanbagabas force-pushed the slog branch 2 times, most recently from 0a54f15 to 6391d1a Compare March 21, 2023 16:27
@owenthereal

Copy link
Copy Markdown

👋 Was looking for bridging with slog. Any update?

@bashbunni

Copy link
Copy Markdown
Contributor

@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
Copy Markdown

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

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

@aymanbagabas

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

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 🙌

@aymanbagabas
aymanbagabas marked this pull request as ready for review October 31, 2023 22:57
@jamietanna

Copy link
Copy Markdown

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

@caarlos0 caarlos0 left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor 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
@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

⌂ 换网址