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

Add a func converting go-kit log to slog #7969

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ require (
github.com/onsi/gomega v1.34.0
github.com/prometheus-community/prom-label-proxy v0.8.1-0.20240127162815-c1195f9aabc0
github.com/seiflotfy/cuckoofilter v0.0.0-20240715131351-a2f2c23f1771
github.com/tjhop/slog-gokit v0.1.2
go.opentelemetry.io/contrib/propagators/autoprop v0.54.0
go4.org/intern v0.0.0-20230525184215-6c62f75575cb
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,8 @@ github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab h1:7ZR3hmisBWw
github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab/go.mod h1:eheTFp954zcWZXCU8d0AT76ftsQOTo4DTqkN/h3k1MY=
github.com/tinylib/msgp v1.1.5 h1:2gXmtWueD2HefZHQe1QOy9HVzmFrLOVvsXwXBQ0ayy0=
github.com/tinylib/msgp v1.1.5/go.mod h1:eQsjooMTnV42mHu917E26IogZ2930nFyBQdofk10Udg=
github.com/tjhop/slog-gokit v0.1.2 h1:pmQI4SvU9h4gA0vIQsdhJQSqQg4mOmsPykG2/PM3j1I=
github.com/tjhop/slog-gokit v0.1.2/go.mod h1:8fhlcp8C8ELbg3GCyKv06tgt4B5sDq2P1r2DQAu1HuM=
github.com/tklauser/go-sysconf v0.3.4/go.mod h1:Cl2c8ZRWfHD5IrfHo9VN+FX9kCFjIOyVklgXycLB6ek=
github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw=
github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk=
Expand Down
10 changes: 9 additions & 1 deletion pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const (
LogFormatJSON = "json"
)

type LevelLogger struct {
log.Logger
LogLevel string
}

// NewLogger returns a log.Logger that prints in the provided format at the
// provided level with a UTC timestamp and the caller of the log entry. If non
// empty, the debug name is also appended as a field to all log lines. Panics
Expand Down Expand Up @@ -55,5 +60,8 @@ func NewLogger(logLevel, logFormat, debugName string) log.Logger {
logger = log.With(logger, "name", debugName)
}

return logger
return LevelLogger{
Logger: logger,
LogLevel: logLevel,
}
}
33 changes: 33 additions & 0 deletions pkg/logutil/logutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package logutil

import (
"log/slog"

"github.com/go-kit/log"
"github.com/thanos-io/thanos/pkg/logging"
sloggk "github.com/tjhop/slog-gokit"
)

// GoKitLogToSlog convert go-kit/log to slog.
func GoKitLogToSlog(logger log.Logger) *slog.Logger {
levelVar := slog.LevelVar{}
levelLogger, ok := logger.(logging.LevelLogger)
if !ok {
levelVar.Set(slog.LevelDebug)
} else {
switch levelLogger.LogLevel {
case "debug":
levelVar.Set(slog.LevelDebug)
case "info":
levelVar.Set(slog.LevelInfo)
case "warn":
levelVar.Set(slog.LevelWarn)
case "error":
levelVar.Set(slog.LevelError)
}
}
return slog.New(sloggk.NewGoKitHandler(logger, &levelVar))
}
52 changes: 52 additions & 0 deletions pkg/logutil/logutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package logutil

import (
"context"
"log/slog"
"testing"

"github.com/go-kit/log/level"
"github.com/stretchr/testify/require"
"github.com/thanos-io/thanos/pkg/logging"
)

func Test_GoKitLogToSlog(t *testing.T) {
ctx := context.Background()
logLevels := []string{"debug", "info", "warn", "error"}
slogLevels := []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError}

for _, logFormat := range []string{"logfmt", "json"} {
for i, lv := range logLevels {
logger := logging.NewLogger(lv, logFormat, "test")

slog := GoKitLogToSlog(logger)
for j, slogLv := range slogLevels {
if i <= j {
t.Logf("[logFormat: %v, go-kit log level: %v, slog level: %v] slog should be enabled", logFormat, lv, slogLv)
require.True(t, slog.Enabled(ctx, slogLv))
} else {
t.Logf("[logFormat: %v, go-kit log level: %v, slog level: %v] slog should be disabled", logFormat, lv, slogLv)
require.False(t, slog.Enabled(ctx, slogLv))
}

switch lv {
case "debug":
level.Debug(logger).Log("msg", "message", "debug", lv)
slog.Debug("message", "debug", lv)
case "info":
level.Info(logger).Log("msg", "message", "info", lv)
slog.Info("message", "info", lv)
case "warn":
level.Warn(logger).Log("msg", "message", "warn", lv)
slog.Warn("message", "warn", lv)
case "error":
level.Error(logger).Log("msg", "message", "error", lv)
slog.Error("message", "error", lv)
}
}
}
}
}
Loading