diff --git a/interceptors/logging/examples/kit/example_test.go b/interceptors/logging/examples/kit/example_test.go index b79f6b999..06d938629 100644 --- a/interceptors/logging/examples/kit/example_test.go +++ b/interceptors/logging/examples/kit/example_test.go @@ -20,6 +20,42 @@ import ( "google.golang.org/grpc" ) +func ExampleInterceptorLogger() { + logger := log.NewNopLogger() + + opts := []logging.Option{ + logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), + // Add any other option (check functions starting with logging.With). + } + + // 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...), + // Add any other interceptor you want. + ), + grpc.ChainStreamInterceptor( + logging.StreamServerInterceptor(examplekit.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // ...user server. + + // Similarly you can create client that will log for the unary and stream client started or finished calls. + _, _ = grpc.Dial( + "some-target", + grpc.WithChainUnaryInterceptor( + logging.UnaryClientInterceptor(examplekit.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + grpc.WithChainStreamInterceptor( + logging.StreamClientInterceptor(examplekit.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // Output: +} + type kitExampleTestSuite struct { *testpb.InterceptorTestSuite logBuffer *bytes.Buffer diff --git a/interceptors/logging/examples/log/example_test.go b/interceptors/logging/examples/log/example_test.go index 3eebe6355..381262a15 100644 --- a/interceptors/logging/examples/log/example_test.go +++ b/interceptors/logging/examples/log/example_test.go @@ -7,6 +7,7 @@ import ( "bytes" "context" "log" + "os" "runtime" "strings" "testing" @@ -20,6 +21,42 @@ import ( "google.golang.org/grpc" ) +func ExampleInterceptorLogger() { + logger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile) + + opts := []logging.Option{ + logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), + // Add any other option (check functions starting with logging.With). + } + + // 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...), + // Add any other interceptor you want. + ), + grpc.ChainStreamInterceptor( + logging.StreamServerInterceptor(examplelog.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // ...user server. + + // Similarly you can create client that will log for the unary and stream client started or finished calls. + _, _ = grpc.Dial( + "some-target", + grpc.WithChainUnaryInterceptor( + logging.UnaryClientInterceptor(examplelog.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + grpc.WithChainStreamInterceptor( + logging.StreamClientInterceptor(examplelog.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // Output: +} + type logExampleTestSuite struct { *testpb.InterceptorTestSuite logBuffer *bytes.Buffer diff --git a/interceptors/logging/examples/logr/example_test.go b/interceptors/logging/examples/logr/example_test.go index ca278229d..ab99a462f 100644 --- a/interceptors/logging/examples/logr/example_test.go +++ b/interceptors/logging/examples/logr/example_test.go @@ -16,9 +16,46 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "google.golang.org/grpc" + "k8s.io/klog/v2" "k8s.io/klog/v2/ktesting" ) +func ExampleInterceptorLogger() { + logger := klog.NewKlogr() + + opts := []logging.Option{ + logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), + // Add any other option (check functions starting with logging.With). + } + + // 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...), + // Add any other interceptor you want. + ), + grpc.ChainStreamInterceptor( + logging.StreamServerInterceptor(examplelogr.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // ...user server. + + // Similarly you can create client that will log for the unary and stream client started or finished calls. + _, _ = grpc.Dial( + "some-target", + grpc.WithChainUnaryInterceptor( + logging.UnaryClientInterceptor(examplelogr.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + grpc.WithChainStreamInterceptor( + logging.StreamClientInterceptor(examplelogr.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // Output: +} + type logrExampleTestSuite struct { *testpb.InterceptorTestSuite logBuffer *ktesting.BufferTL diff --git a/interceptors/logging/examples/logrus/example_test.go b/interceptors/logging/examples/logrus/example_test.go index f0d1f5f8e..26da699fe 100644 --- a/interceptors/logging/examples/logrus/example_test.go +++ b/interceptors/logging/examples/logrus/example_test.go @@ -21,6 +21,42 @@ import ( "google.golang.org/grpc" ) +func ExampleInterceptorLogger() { + logger := logrus.New() + + opts := []logging.Option{ + logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), + // Add any other option (check functions starting with logging.With). + } + + // 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...), + // Add any other interceptor you want. + ), + grpc.ChainStreamInterceptor( + logging.StreamServerInterceptor(examplelogrus.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // ...user server. + + // Similarly you can create client that will log for the unary and stream client started or finished calls. + _, _ = grpc.Dial( + "some-target", + grpc.WithChainUnaryInterceptor( + logging.UnaryClientInterceptor(examplelogrus.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + grpc.WithChainStreamInterceptor( + logging.StreamClientInterceptor(examplelogrus.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // Output: +} + type logrusExampleTestSuite struct { *testpb.InterceptorTestSuite logBuffer *bytes.Buffer diff --git a/interceptors/logging/examples/slog/example_test.go b/interceptors/logging/examples/slog/example_test.go index 9022879f8..26299c82c 100644 --- a/interceptors/logging/examples/slog/example_test.go +++ b/interceptors/logging/examples/slog/example_test.go @@ -6,6 +6,7 @@ package exampleslog_test import ( "bytes" "context" + "os" "runtime" "strings" "testing" @@ -20,6 +21,42 @@ import ( "google.golang.org/grpc" ) +func ExampleInterceptorLogger() { + logger := slog.New(slog.NewTextHandler(os.Stderr)) + + opts := []logging.Option{ + logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), + // Add any other option (check functions starting with logging.With). + } + + // 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...), + // Add any other interceptor you want. + ), + grpc.ChainStreamInterceptor( + logging.StreamServerInterceptor(exampleslog.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // ...user server. + + // Similarly you can create client that will log for the unary and stream client started or finished calls. + _, _ = grpc.Dial( + "some-target", + grpc.WithChainUnaryInterceptor( + logging.UnaryClientInterceptor(exampleslog.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + grpc.WithChainStreamInterceptor( + logging.StreamClientInterceptor(exampleslog.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // Output: +} + type slogExampleTestSuite struct { *testpb.InterceptorTestSuite logBuffer *bytes.Buffer diff --git a/interceptors/logging/examples/zap/example.go b/interceptors/logging/examples/zap/example.go index b4f417e2e..b4b285db3 100644 --- a/interceptors/logging/examples/zap/example.go +++ b/interceptors/logging/examples/zap/example.go @@ -11,6 +11,8 @@ import ( "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) diff --git a/interceptors/logging/examples/zap/example_test.go b/interceptors/logging/examples/zap/example_test.go index 3aa0377a4..76a4093ef 100644 --- a/interceptors/logging/examples/zap/example_test.go +++ b/interceptors/logging/examples/zap/example_test.go @@ -20,6 +20,42 @@ import ( "google.golang.org/grpc" ) +func ExampleInterceptorLogger() { + logger := zap.NewExample() + + opts := []logging.Option{ + logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), + // Add any other option (check functions starting with logging.With). + } + + // 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...), + // Add any other interceptor you want. + ), + grpc.ChainStreamInterceptor( + logging.StreamServerInterceptor(examplezap.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // ...user server. + + // Similarly you can create client that will log for the unary and stream client started or finished calls. + _, _ = grpc.Dial( + "some-target", + grpc.WithChainUnaryInterceptor( + logging.UnaryClientInterceptor(examplezap.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + grpc.WithChainStreamInterceptor( + logging.StreamClientInterceptor(examplezap.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // Output: +} + type zapExampleTestSuite struct { *testpb.InterceptorTestSuite observedLogs *observer.ObservedLogs diff --git a/interceptors/logging/examples/zerolog/example_test.go b/interceptors/logging/examples/zerolog/example_test.go index 4114c757d..da6144da6 100644 --- a/interceptors/logging/examples/zerolog/example_test.go +++ b/interceptors/logging/examples/zerolog/example_test.go @@ -7,6 +7,7 @@ import ( "bytes" "context" "encoding/json" + "os" "runtime" "strings" "testing" @@ -21,6 +22,42 @@ import ( "google.golang.org/grpc" ) +func ExampleInterceptorLogger() { + logger := zerolog.New(os.Stderr) + + opts := []logging.Option{ + logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), + // Add any other option (check functions starting with logging.With). + } + + // 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...), + // Add any other interceptor you want. + ), + grpc.ChainStreamInterceptor( + logging.StreamServerInterceptor(examplezerolog.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // ...user server. + + // Similarly you can create client that will log for the unary and stream client started or finished calls. + _, _ = grpc.Dial( + "some-target", + grpc.WithChainUnaryInterceptor( + logging.UnaryClientInterceptor(examplezerolog.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + grpc.WithChainStreamInterceptor( + logging.StreamClientInterceptor(examplezerolog.InterceptorLogger(logger), opts...), + // Add any other interceptor you want. + ), + ) + // Output: +} + type zerologExampleTestSuite struct { *testpb.InterceptorTestSuite logBuffer *bytes.Buffer