diff --git a/interceptors/logging/examples/kit/example.go b/interceptors/logging/examples/kit/example.go deleted file mode 100644 index ccae658aa..000000000 --- a/interceptors/logging/examples/kit/example.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) The go-grpc-middleware Authors. -// Licensed under the Apache License 2.0. - -package examplekit - -import ( - "context" - "fmt" - - "github.com/go-kit/log" - "github.com/go-kit/log/level" - "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" -) - -// InterceptorLogger adapts go-kit logger to interceptor logger. -// This code is simple enough to be copied and not imported. -func InterceptorLogger(l log.Logger) logging.Logger { - return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { - largs := append([]any{"msg", msg}, fields...) - switch lvl { - case logging.LevelDebug: - _ = level.Debug(l).Log(largs...) - case logging.LevelInfo: - _ = level.Info(l).Log(largs...) - case logging.LevelWarn: - _ = level.Warn(l).Log(largs...) - case logging.LevelError: - _ = level.Error(l).Log(largs...) - default: - panic(fmt.Sprintf("unknown level %v", lvl)) - } - }) -} diff --git a/interceptors/logging/examples/kit/example_test.go b/interceptors/logging/examples/kit/example_test.go index 06d938629..5c2cfae69 100644 --- a/interceptors/logging/examples/kit/example_test.go +++ b/interceptors/logging/examples/kit/example_test.go @@ -1,17 +1,18 @@ // Copyright (c) The go-grpc-middleware Authors. // Licensed under the Apache License 2.0. -package examplekit_test +package kit_test import ( "bytes" "context" + "fmt" "runtime" "strings" "testing" "github.com/go-kit/log" - examplekit "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/kit" + "github.com/go-kit/log/level" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" "github.com/stretchr/testify/assert" @@ -20,6 +21,26 @@ import ( "google.golang.org/grpc" ) +// InterceptorLogger adapts go-kit logger to interceptor logger. +// This code is simple enough to be copied and not imported. +func InterceptorLogger(l log.Logger) logging.Logger { + return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { + largs := append([]any{"msg", msg}, fields...) + switch lvl { + case logging.LevelDebug: + _ = level.Debug(l).Log(largs...) + case logging.LevelInfo: + _ = level.Info(l).Log(largs...) + case logging.LevelWarn: + _ = level.Warn(l).Log(largs...) + case logging.LevelError: + _ = level.Error(l).Log(largs...) + default: + panic(fmt.Sprintf("unknown level %v", lvl)) + } + }) +} + func ExampleInterceptorLogger() { logger := log.NewNopLogger() @@ -31,11 +52,11 @@ func ExampleInterceptorLogger() { // You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished. _ = grpc.NewServer( grpc.ChainUnaryInterceptor( - logging.UnaryServerInterceptor(examplekit.InterceptorLogger(logger), opts...), + logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.ChainStreamInterceptor( - logging.StreamServerInterceptor(examplekit.InterceptorLogger(logger), opts...), + logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -45,11 +66,11 @@ func ExampleInterceptorLogger() { _, _ = grpc.Dial( "some-target", grpc.WithChainUnaryInterceptor( - logging.UnaryClientInterceptor(examplekit.InterceptorLogger(logger), opts...), + logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.WithChainStreamInterceptor( - logging.StreamClientInterceptor(examplekit.InterceptorLogger(logger), opts...), + logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -68,7 +89,7 @@ func TestSuite(t *testing.T) { } buffer := &bytes.Buffer{} - logger := examplekit.InterceptorLogger(log.NewLogfmtLogger(buffer)) + logger := InterceptorLogger(log.NewLogfmtLogger(buffer)) s := &kitExampleTestSuite{ InterceptorTestSuite: &testpb.InterceptorTestSuite{ diff --git a/interceptors/logging/examples/log/example.go b/interceptors/logging/examples/log/example.go deleted file mode 100644 index 604929e7a..000000000 --- a/interceptors/logging/examples/log/example.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) The go-grpc-middleware Authors. -// Licensed under the Apache License 2.0. - -package examplelog - -import ( - "context" - "fmt" - "log" - - "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" -) - -// InterceptorLogger adapts standard Go logger to interceptor logger. -// This code is simple enough to be copied and not imported. -func InterceptorLogger(l *log.Logger) logging.Logger { - return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { - switch lvl { - case logging.LevelDebug: - msg = fmt.Sprintf("DEBUG :%v", msg) - case logging.LevelInfo: - msg = fmt.Sprintf("INFO :%v", msg) - case logging.LevelWarn: - msg = fmt.Sprintf("WARN :%v", msg) - case logging.LevelError: - msg = fmt.Sprintf("ERROR :%v", msg) - default: - panic(fmt.Sprintf("unknown level %v", lvl)) - } - l.Println(append([]any{"msg", msg}, fields...)) - }) -} diff --git a/interceptors/logging/examples/log/example_test.go b/interceptors/logging/examples/log/example_test.go index 381262a15..73b6db78e 100644 --- a/interceptors/logging/examples/log/example_test.go +++ b/interceptors/logging/examples/log/example_test.go @@ -1,18 +1,18 @@ // Copyright (c) The go-grpc-middleware Authors. // Licensed under the Apache License 2.0. -package examplelog_test +package log_test import ( "bytes" "context" + "fmt" "log" "os" "runtime" "strings" "testing" - examplelog "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/log" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" "github.com/stretchr/testify/assert" @@ -21,6 +21,26 @@ import ( "google.golang.org/grpc" ) +// InterceptorLogger adapts standard Go logger to interceptor logger. +// This code is simple enough to be copied and not imported. +func InterceptorLogger(l *log.Logger) logging.Logger { + return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { + switch lvl { + case logging.LevelDebug: + msg = fmt.Sprintf("DEBUG :%v", msg) + case logging.LevelInfo: + msg = fmt.Sprintf("INFO :%v", msg) + case logging.LevelWarn: + msg = fmt.Sprintf("WARN :%v", msg) + case logging.LevelError: + msg = fmt.Sprintf("ERROR :%v", msg) + default: + panic(fmt.Sprintf("unknown level %v", lvl)) + } + l.Println(append([]any{"msg", msg}, fields...)) + }) +} + func ExampleInterceptorLogger() { logger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile) @@ -32,11 +52,11 @@ func ExampleInterceptorLogger() { // You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished. _ = grpc.NewServer( grpc.ChainUnaryInterceptor( - logging.UnaryServerInterceptor(examplelog.InterceptorLogger(logger), opts...), + logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.ChainStreamInterceptor( - logging.StreamServerInterceptor(examplelog.InterceptorLogger(logger), opts...), + logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -46,11 +66,11 @@ func ExampleInterceptorLogger() { _, _ = grpc.Dial( "some-target", grpc.WithChainUnaryInterceptor( - logging.UnaryClientInterceptor(examplelog.InterceptorLogger(logger), opts...), + logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.WithChainStreamInterceptor( - logging.StreamClientInterceptor(examplelog.InterceptorLogger(logger), opts...), + logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -68,7 +88,7 @@ func TestSuite(t *testing.T) { return } buffer := &bytes.Buffer{} - logger := examplelog.InterceptorLogger(log.New(buffer, "", 0)) + logger := InterceptorLogger(log.New(buffer, "", 0)) s := &logExampleTestSuite{ InterceptorTestSuite: &testpb.InterceptorTestSuite{ diff --git a/interceptors/logging/examples/logr/example.go b/interceptors/logging/examples/logr/example.go deleted file mode 100644 index ab2edaa11..000000000 --- a/interceptors/logging/examples/logr/example.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) The go-grpc-middleware Authors. -// Licensed under the Apache License 2.0. - -package examplelogr - -import ( - "context" - "fmt" - - "github.com/go-logr/logr" - "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" -) - -// verbosity https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use -const ( - debugVerbosity = 4 - infoVerbosity = 2 - warnVerbosity = 1 - errorVerbosity = 0 -) - -// InterceptorLogger adapts logr logger to interceptor logger. -// This code is simple enough to be copied and not imported. -func InterceptorLogger(l logr.Logger) logging.Logger { - return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { - l = l.WithValues(fields...) - switch lvl { - case logging.LevelDebug: - l.V(debugVerbosity).Info(msg) - case logging.LevelInfo: - l.V(infoVerbosity).Info(msg) - case logging.LevelWarn: - l.V(warnVerbosity).Info(msg) - case logging.LevelError: - l.V(errorVerbosity).Info(msg) - default: - panic(fmt.Sprintf("unknown level %v", lvl)) - } - }) -} diff --git a/interceptors/logging/examples/logr/example_test.go b/interceptors/logging/examples/logr/example_test.go index ab99a462f..44846a67f 100644 --- a/interceptors/logging/examples/logr/example_test.go +++ b/interceptors/logging/examples/logr/example_test.go @@ -1,15 +1,16 @@ // Copyright (c) The go-grpc-middleware Authors. // Licensed under the Apache License 2.0. -package examplelogr_test +package logr_test import ( "context" + "fmt" "runtime" "strings" "testing" - examplelogr "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/logr" + "github.com/go-logr/logr" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" "github.com/stretchr/testify/assert" @@ -20,6 +21,34 @@ import ( "k8s.io/klog/v2/ktesting" ) +// verbosity https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use +const ( + debugVerbosity = 4 + infoVerbosity = 2 + warnVerbosity = 1 + errorVerbosity = 0 +) + +// InterceptorLogger adapts logr logger to interceptor logger. +// This code is simple enough to be copied and not imported. +func InterceptorLogger(l logr.Logger) logging.Logger { + return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { + l = l.WithValues(fields...) + switch lvl { + case logging.LevelDebug: + l.V(debugVerbosity).Info(msg) + case logging.LevelInfo: + l.V(infoVerbosity).Info(msg) + case logging.LevelWarn: + l.V(warnVerbosity).Info(msg) + case logging.LevelError: + l.V(errorVerbosity).Info(msg) + default: + panic(fmt.Sprintf("unknown level %v", lvl)) + } + }) +} + func ExampleInterceptorLogger() { logger := klog.NewKlogr() @@ -31,11 +60,11 @@ func ExampleInterceptorLogger() { // You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished. _ = grpc.NewServer( grpc.ChainUnaryInterceptor( - logging.UnaryServerInterceptor(examplelogr.InterceptorLogger(logger), opts...), + logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.ChainStreamInterceptor( - logging.StreamServerInterceptor(examplelogr.InterceptorLogger(logger), opts...), + logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -45,11 +74,11 @@ func ExampleInterceptorLogger() { _, _ = grpc.Dial( "some-target", grpc.WithChainUnaryInterceptor( - logging.UnaryClientInterceptor(examplelogr.InterceptorLogger(logger), opts...), + logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.WithChainStreamInterceptor( - logging.StreamClientInterceptor(examplelogr.InterceptorLogger(logger), opts...), + logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -69,7 +98,7 @@ func TestSuite(t *testing.T) { buffer := &ktesting.BufferTL{} cfg := ktesting.NewConfig() - logger := examplelogr.InterceptorLogger(ktesting.NewLogger(buffer, cfg)) + logger := InterceptorLogger(ktesting.NewLogger(buffer, cfg)) s := &logrExampleTestSuite{ InterceptorTestSuite: &testpb.InterceptorTestSuite{ diff --git a/interceptors/logging/examples/logrus/example.go b/interceptors/logging/examples/logrus/example.go deleted file mode 100644 index 64d6884a1..000000000 --- a/interceptors/logging/examples/logrus/example.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) The go-grpc-middleware Authors. -// Licensed under the Apache License 2.0. - -package examplelogrus - -import ( - "context" - "fmt" - - "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" - "github.com/sirupsen/logrus" -) - -// InterceptorLogger adapts logrus logger to interceptor logger. -// This code is simple enough to be copied and not imported. -func InterceptorLogger(l logrus.FieldLogger) logging.Logger { - return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { - f := make(map[string]any, len(fields)/2) - i := logging.Fields(fields).Iterator() - for i.Next() { - k, v := i.At() - f[k] = v - } - l = l.WithFields(f) - - switch lvl { - case logging.LevelDebug: - l.Debug(msg) - case logging.LevelInfo: - l.Info(msg) - case logging.LevelWarn: - l.Warn(msg) - case logging.LevelError: - l.Error(msg) - default: - panic(fmt.Sprintf("unknown level %v", lvl)) - } - }) -} diff --git a/interceptors/logging/examples/logrus/example_test.go b/interceptors/logging/examples/logrus/example_test.go index 26da699fe..22fd63f6f 100644 --- a/interceptors/logging/examples/logrus/example_test.go +++ b/interceptors/logging/examples/logrus/example_test.go @@ -1,17 +1,17 @@ // Copyright (c) The go-grpc-middleware Authors. // Licensed under the Apache License 2.0. -package examplelogrus_test +package logrus_test import ( "bytes" "context" "encoding/json" + "fmt" "runtime" "strings" "testing" - examplelogrus "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/logrus" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" "github.com/sirupsen/logrus" @@ -21,6 +21,33 @@ import ( "google.golang.org/grpc" ) +// InterceptorLogger adapts logrus logger to interceptor logger. +// This code is simple enough to be copied and not imported. +func InterceptorLogger(l logrus.FieldLogger) logging.Logger { + return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { + f := make(map[string]any, len(fields)/2) + i := logging.Fields(fields).Iterator() + for i.Next() { + k, v := i.At() + f[k] = v + } + l = l.WithFields(f) + + switch lvl { + case logging.LevelDebug: + l.Debug(msg) + case logging.LevelInfo: + l.Info(msg) + case logging.LevelWarn: + l.Warn(msg) + case logging.LevelError: + l.Error(msg) + default: + panic(fmt.Sprintf("unknown level %v", lvl)) + } + }) +} + func ExampleInterceptorLogger() { logger := logrus.New() @@ -32,11 +59,11 @@ func ExampleInterceptorLogger() { // You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished. _ = grpc.NewServer( grpc.ChainUnaryInterceptor( - logging.UnaryServerInterceptor(examplelogrus.InterceptorLogger(logger), opts...), + logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.ChainStreamInterceptor( - logging.StreamServerInterceptor(examplelogrus.InterceptorLogger(logger), opts...), + logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -46,11 +73,11 @@ func ExampleInterceptorLogger() { _, _ = grpc.Dial( "some-target", grpc.WithChainUnaryInterceptor( - logging.UnaryClientInterceptor(examplelogrus.InterceptorLogger(logger), opts...), + logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.WithChainStreamInterceptor( - logging.StreamClientInterceptor(examplelogrus.InterceptorLogger(logger), opts...), + logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -72,7 +99,7 @@ func TestSuite(t *testing.T) { testLogger := logrus.New() testLogger.Out = buffer testLogger.SetFormatter(&logrus.JSONFormatter{}) - logger := examplelogrus.InterceptorLogger(testLogger) + logger := InterceptorLogger(testLogger) s := &logrusExampleTestSuite{ InterceptorTestSuite: &testpb.InterceptorTestSuite{ diff --git a/interceptors/logging/examples/slog/example.go b/interceptors/logging/examples/slog/example.go deleted file mode 100644 index 74c4bf58f..000000000 --- a/interceptors/logging/examples/slog/example.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) The go-grpc-middleware Authors. -// Licensed under the Apache License 2.0. - -package exampleslog - -import ( - "context" - - "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" - "golang.org/x/exp/slog" -) - -// InterceptorLogger adapts slog logger to interceptor logger. -// This code is simple enough to be copied and not imported. -func InterceptorLogger(l *slog.Logger) logging.Logger { - return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) { - l.Log(ctx, slog.Level(lvl), msg, fields...) - }) -} diff --git a/interceptors/logging/examples/slog/example_test.go b/interceptors/logging/examples/slog/example_test.go index 26299c82c..fda478a90 100644 --- a/interceptors/logging/examples/slog/example_test.go +++ b/interceptors/logging/examples/slog/example_test.go @@ -1,7 +1,7 @@ // Copyright (c) The go-grpc-middleware Authors. // Licensed under the Apache License 2.0. -package exampleslog_test +package slog_test import ( "bytes" @@ -11,7 +11,6 @@ import ( "strings" "testing" - exampleslog "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/slog" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" "github.com/stretchr/testify/assert" @@ -21,6 +20,14 @@ import ( "google.golang.org/grpc" ) +// InterceptorLogger adapts slog logger to interceptor logger. +// This code is simple enough to be copied and not imported. +func InterceptorLogger(l *slog.Logger) logging.Logger { + return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) { + l.Log(ctx, slog.Level(lvl), msg, fields...) + }) +} + func ExampleInterceptorLogger() { logger := slog.New(slog.NewTextHandler(os.Stderr)) @@ -32,11 +39,11 @@ func ExampleInterceptorLogger() { // You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished. _ = grpc.NewServer( grpc.ChainUnaryInterceptor( - logging.UnaryServerInterceptor(exampleslog.InterceptorLogger(logger), opts...), + logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.ChainStreamInterceptor( - logging.StreamServerInterceptor(exampleslog.InterceptorLogger(logger), opts...), + logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -46,11 +53,11 @@ func ExampleInterceptorLogger() { _, _ = grpc.Dial( "some-target", grpc.WithChainUnaryInterceptor( - logging.UnaryClientInterceptor(exampleslog.InterceptorLogger(logger), opts...), + logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.WithChainStreamInterceptor( - logging.StreamClientInterceptor(exampleslog.InterceptorLogger(logger), opts...), + logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -68,7 +75,7 @@ func TestSuite(t *testing.T) { return } buffer := &bytes.Buffer{} - logger := exampleslog.InterceptorLogger(slog.New(slog.NewTextHandler(buffer))) + logger := InterceptorLogger(slog.New(slog.NewTextHandler(buffer))) s := &slogExampleTestSuite{ InterceptorTestSuite: &testpb.InterceptorTestSuite{ TestService: &testpb.TestPingService{}, diff --git a/interceptors/logging/examples/zap/example.go b/interceptors/logging/examples/zap/example.go deleted file mode 100644 index b4b285db3..000000000 --- a/interceptors/logging/examples/zap/example.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) The go-grpc-middleware Authors. -// Licensed under the Apache License 2.0. - -package examplezap - -import ( - "context" - "fmt" - - "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" - "go.uber.org/zap" -) - -// InterceptorLogger adapts zap logger to interceptor logger. -// This code is simple enough to be copied and not imported. -func InterceptorLogger(l *zap.Logger) logging.Logger { - return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) { - f := make([]zap.Field, 0, len(fields)/2) - iter := logging.Fields(fields).Iterator() - for iter.Next() { - k, v := iter.At() - f = append(f, zap.Any(k, v)) - } - l = l.WithOptions(zap.AddCallerSkip(1)).With(f...) - - switch lvl { - case logging.LevelDebug: - l.Debug(msg) - case logging.LevelInfo: - l.Info(msg) - case logging.LevelWarn: - l.Warn(msg) - case logging.LevelError: - l.Error(msg) - default: - panic(fmt.Sprintf("unknown level %v", lvl)) - } - }) -} diff --git a/interceptors/logging/examples/zap/example_test.go b/interceptors/logging/examples/zap/example_test.go index 76a4093ef..90ddb3f89 100644 --- a/interceptors/logging/examples/zap/example_test.go +++ b/interceptors/logging/examples/zap/example_test.go @@ -1,15 +1,15 @@ // Copyright (c) The go-grpc-middleware Authors. // Licensed under the Apache License 2.0. -package examplezap_test +package zap_test import ( "context" + "fmt" "runtime" "strings" "testing" - examplezap "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/zap" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" "github.com/stretchr/testify/assert" @@ -20,6 +20,33 @@ import ( "google.golang.org/grpc" ) +// InterceptorLogger adapts zap logger to interceptor logger. +// This code is simple enough to be copied and not imported. +func InterceptorLogger(l *zap.Logger) logging.Logger { + return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) { + f := make([]zap.Field, 0, len(fields)/2) + iter := logging.Fields(fields).Iterator() + for iter.Next() { + k, v := iter.At() + f = append(f, zap.Any(k, v)) + } + l = l.WithOptions(zap.AddCallerSkip(1)).With(f...) + + switch lvl { + case logging.LevelDebug: + l.Debug(msg) + case logging.LevelInfo: + l.Info(msg) + case logging.LevelWarn: + l.Warn(msg) + case logging.LevelError: + l.Error(msg) + default: + panic(fmt.Sprintf("unknown level %v", lvl)) + } + }) +} + func ExampleInterceptorLogger() { logger := zap.NewExample() @@ -31,11 +58,11 @@ func ExampleInterceptorLogger() { // You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished. _ = grpc.NewServer( grpc.ChainUnaryInterceptor( - logging.UnaryServerInterceptor(examplezap.InterceptorLogger(logger), opts...), + logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.ChainStreamInterceptor( - logging.StreamServerInterceptor(examplezap.InterceptorLogger(logger), opts...), + logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -45,11 +72,11 @@ func ExampleInterceptorLogger() { _, _ = grpc.Dial( "some-target", grpc.WithChainUnaryInterceptor( - logging.UnaryClientInterceptor(examplezap.InterceptorLogger(logger), opts...), + logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.WithChainStreamInterceptor( - logging.StreamClientInterceptor(examplezap.InterceptorLogger(logger), opts...), + logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -67,7 +94,7 @@ func TestSuite(t *testing.T) { return } observedZapCore, observedLogs := observer.New(zap.DebugLevel) - logger := examplezap.InterceptorLogger(zap.New(observedZapCore)) + logger := InterceptorLogger(zap.New(observedZapCore)) s := &zapExampleTestSuite{ InterceptorTestSuite: &testpb.InterceptorTestSuite{ TestService: &testpb.TestPingService{}, diff --git a/interceptors/logging/examples/zerolog/example.go b/interceptors/logging/examples/zerolog/example.go deleted file mode 100644 index 080a2e322..000000000 --- a/interceptors/logging/examples/zerolog/example.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) The go-grpc-middleware Authors. -// Licensed under the Apache License 2.0. - -package examplezerolog - -import ( - "context" - "fmt" - - "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" - "github.com/rs/zerolog" -) - -// InterceptorLogger adapts zerolog logger to interceptor logger. -// This code is simple enough to be copied and not imported. -func InterceptorLogger(l zerolog.Logger) logging.Logger { - return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) { - l := l.With().Fields(fields).Logger() - - switch lvl { - case logging.LevelDebug: - l.Debug().Msg(msg) - case logging.LevelInfo: - l.Info().Msg(msg) - case logging.LevelWarn: - l.Warn().Msg(msg) - case logging.LevelError: - l.Error().Msg(msg) - default: - panic(fmt.Sprintf("unknown level %v", lvl)) - } - }) -} diff --git a/interceptors/logging/examples/zerolog/example_test.go b/interceptors/logging/examples/zerolog/example_test.go index da6144da6..5056ed8b5 100644 --- a/interceptors/logging/examples/zerolog/example_test.go +++ b/interceptors/logging/examples/zerolog/example_test.go @@ -1,18 +1,18 @@ // Copyright (c) The go-grpc-middleware Authors. // Licensed under the Apache License 2.0. -package examplezerolog_test +package zerolog_test import ( "bytes" "context" "encoding/json" + "fmt" "os" "runtime" "strings" "testing" - examplezerolog "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/zerolog" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" "github.com/rs/zerolog" @@ -22,6 +22,27 @@ import ( "google.golang.org/grpc" ) +// InterceptorLogger adapts zerolog logger to interceptor logger. +// This code is simple enough to be copied and not imported. +func InterceptorLogger(l zerolog.Logger) logging.Logger { + return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) { + l := l.With().Fields(fields).Logger() + + switch lvl { + case logging.LevelDebug: + l.Debug().Msg(msg) + case logging.LevelInfo: + l.Info().Msg(msg) + case logging.LevelWarn: + l.Warn().Msg(msg) + case logging.LevelError: + l.Error().Msg(msg) + default: + panic(fmt.Sprintf("unknown level %v", lvl)) + } + }) +} + func ExampleInterceptorLogger() { logger := zerolog.New(os.Stderr) @@ -33,11 +54,11 @@ func ExampleInterceptorLogger() { // You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished. _ = grpc.NewServer( grpc.ChainUnaryInterceptor( - logging.UnaryServerInterceptor(examplezerolog.InterceptorLogger(logger), opts...), + logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.ChainStreamInterceptor( - logging.StreamServerInterceptor(examplezerolog.InterceptorLogger(logger), opts...), + logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -47,11 +68,11 @@ func ExampleInterceptorLogger() { _, _ = grpc.Dial( "some-target", grpc.WithChainUnaryInterceptor( - logging.UnaryClientInterceptor(examplezerolog.InterceptorLogger(logger), opts...), + logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), grpc.WithChainStreamInterceptor( - logging.StreamClientInterceptor(examplezerolog.InterceptorLogger(logger), opts...), + logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), // Add any other interceptor you want. ), ) @@ -69,7 +90,7 @@ func TestSuite(t *testing.T) { return } buffer := &bytes.Buffer{} - logger := examplezerolog.InterceptorLogger(zerolog.New(buffer)) + logger := InterceptorLogger(zerolog.New(buffer)) s := &zerologExampleTestSuite{ InterceptorTestSuite: &testpb.InterceptorTestSuite{ TestService: &testpb.TestPingService{},