-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a func to convert go-kit log to slog
Signed-off-by: kade.lee <[email protected]>
- Loading branch information
1 parent
d0d93db
commit 281c11e
Showing
5 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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{"json", "logfmt"} { | ||
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) | ||
} | ||
} | ||
} | ||
} | ||
} |