-
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.
Signed-off-by: SungJin1212 <[email protected]>
- Loading branch information
1 parent
7191ecb
commit 6e8e031
Showing
13 changed files
with
534 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package log | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/go-kit/log" | ||
sloggk "github.com/tjhop/slog-gokit" | ||
"github.com/weaveworks/common/logging" | ||
) | ||
|
||
type slogAdapter struct { | ||
logging.Level | ||
} | ||
|
||
func newSlogAdapter(level logging.Level) slogAdapter { | ||
return slogAdapter{level} | ||
} | ||
|
||
func (s *slogAdapter) GoKitLogToSlog(logger log.Logger) *slog.Logger { | ||
levelVar := slog.LevelVar{} | ||
switch s.Level.String() { | ||
case "info": | ||
levelVar.Set(slog.LevelInfo) | ||
case "warn": | ||
levelVar.Set(slog.LevelWarn) | ||
case "error": | ||
levelVar.Set(slog.LevelError) | ||
case "debug": | ||
levelVar.Set(slog.LevelDebug) | ||
} | ||
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 := SlogAdapter.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.