-
Notifications
You must be signed in to change notification settings - Fork 806
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add slogAdapter Signed-off-by: SungJin1212 <[email protected]> * Fix test race condition due to EnableExperimentalFunctions Signed-off-by: SungJin1212 <[email protected]> --------- Signed-off-by: SungJin1212 <[email protected]>
- Loading branch information
1 parent
c9e3d5e
commit 7e444e7
Showing
14 changed files
with
532 additions
and
5 deletions.
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
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 log | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/go-kit/log" | ||
sloggk "github.com/tjhop/slog-gokit" | ||
) | ||
|
||
// GoKitLogToSlog convert go-kit/log to slog | ||
// usage: logutil.GoKitLogToSlog(gokitLogger) | ||
func GoKitLogToSlog(logger log.Logger) *slog.Logger { | ||
levelVar := slog.LevelVar{} | ||
promLogger, ok := logger.(*PrometheusLogger) | ||
if !ok { | ||
levelVar.Set(slog.LevelDebug) | ||
} else { | ||
switch promLogger.logLevel.String() { | ||
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,33 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/weaveworks/common/server" | ||
) | ||
|
||
func Test_SlogAdapter_LogLevel(t *testing.T) { | ||
ctx := context.Background() | ||
logLevels := []string{"debug", "info", "warn", "error"} | ||
slogLevels := []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError} | ||
|
||
for i, lv := range logLevels { | ||
cfg := &server.Config{} | ||
require.NoError(t, cfg.LogLevel.Set(lv)) | ||
InitLogger(cfg) | ||
|
||
slog := GoKitLogToSlog(Logger) | ||
for j, slogLv := range slogLevels { | ||
if i <= j { | ||
t.Logf("[go-kit log level: %v, slog level: %v] slog should be enabled", lv, slogLv) | ||
require.True(t, slog.Enabled(ctx, slogLv)) | ||
} else { | ||
t.Logf("[go-kit log level: %v, slog level: %v] slog should be disabled", lv, slogLv) | ||
require.False(t, slog.Enabled(ctx, slogLv)) | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.