Skip to content

Commit

Permalink
Add slogAdapter
Browse files Browse the repository at this point in the history
Signed-off-by: SungJin1212 <[email protected]>
  • Loading branch information
SungJin1212 committed Dec 6, 2024
1 parent 7191ecb commit 39a284b
Show file tree
Hide file tree
Showing 45 changed files with 5,477 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0
github.com/google/go-cmp v0.6.0
github.com/sercand/kuberesolver/v5 v5.1.1
github.com/tjhop/slog-gokit v0.1.2
go.opentelemetry.io/collector/pdata v1.21.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
google.golang.org/protobuf v1.35.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,8 @@ github.com/thanos-io/promql-engine v0.0.0-20241203103240-2f49f80c7c68 h1:cChM/Fb
github.com/thanos-io/promql-engine v0.0.0-20241203103240-2f49f80c7c68/go.mod h1:wx0JlRZtsB2S10JYUgeg5GqLfMxw31SzArP+28yyE00=
github.com/thanos-io/thanos v0.37.1 h1:PuTMql3S/i5UWlBT0WbCDwwL6Kc6Jf7DLHt2rdj4ivY=
github.com/thanos-io/thanos v0.37.1/go.mod h1:5Ni7Uc1Bc8UCGOYmZ/2f/LVvDkZKNDdqDJZqjDFG+rI=
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/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o=
github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func InitLogger(cfg *server.Config) {
// PrometheusLogger exposes Prometheus counters for each of go-kit's log levels.
type PrometheusLogger struct {
logger log.Logger
lv logging.Level
}

// NewPrometheusLogger creates a new instance of PrometheusLogger which exposes
Expand Down Expand Up @@ -93,6 +94,7 @@ func newPrometheusLoggerFrom(logger log.Logger, logLevel logging.Level, keyvals
}
return &PrometheusLogger{
logger: logger,
lv: logLevel,
}
}

Expand Down
30 changes: 30 additions & 0 deletions pkg/util/log/slog_adapter.go
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: util_log.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.lv.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))
}
33 changes: 33 additions & 0 deletions pkg/util/log/slog_adapter_test.go
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))
}
}
}
}
66 changes: 66 additions & 0 deletions vendor/cloud.google.com/go/auth/internal/compute/compute.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/cloud.google.com/go/auth/internal/compute/manufacturer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 39a284b

Please sign in to comment.