-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
486 additions
and
310 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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)) | ||
} | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,70 +1,67 @@ | ||
// Copyright (c) The go-grpc-middleware Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package kit_test | ||
package examplekit_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
examplekit "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/kit" | ||
"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" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"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)) | ||
} | ||
}) | ||
type kitExampleTestSuite struct { | ||
*testpb.InterceptorTestSuite | ||
logBuffer *bytes.Buffer | ||
} | ||
|
||
func ExampleInterceptorLogger() { | ||
logger := log.NewNopLogger() | ||
func TestSuite(t *testing.T) { | ||
if strings.HasPrefix(runtime.Version(), "go1.7") { | ||
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7") | ||
return | ||
} | ||
|
||
buffer := &bytes.Buffer{} | ||
logger := examplekit.InterceptorLogger(log.NewLogfmtLogger(buffer)) | ||
|
||
s := &kitExampleTestSuite{ | ||
InterceptorTestSuite: &testpb.InterceptorTestSuite{ | ||
TestService: &testpb.TestPingService{}, | ||
}, | ||
logBuffer: buffer, | ||
} | ||
|
||
opts := []logging.Option{ | ||
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), | ||
// Add any other option (check functions starting with logging.With). | ||
s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{ | ||
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)), | ||
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)), | ||
} | ||
|
||
// 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(InterceptorLogger(logger), opts...), | ||
// Add any other interceptor you want. | ||
), | ||
grpc.ChainStreamInterceptor( | ||
logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), | ||
// Add any other interceptor you want. | ||
), | ||
) | ||
// ...user server. | ||
suite.Run(t, s) | ||
} | ||
|
||
func (s *kitExampleTestSuite) TestPing() { | ||
ctx := context.Background() | ||
_, err := s.Client.Ping(ctx, testpb.GoodPing) | ||
assert.NoError(s.T(), err, "there must be not be an on a successful call") | ||
logStr := s.logBuffer.String() | ||
require.Contains(s.T(), logStr, "level=info") | ||
require.Contains(s.T(), logStr, "msg=\"started call\"") | ||
require.Contains(s.T(), logStr, "protocol=grpc") | ||
require.Contains(s.T(), logStr, "grpc.component=server") | ||
require.Contains(s.T(), logStr, "grpc.service=testing.testpb.v1.TestService") | ||
require.Contains(s.T(), logStr, "grpc.method=Ping") | ||
require.Contains(s.T(), logStr, "grpc.method_type=unary") | ||
require.Contains(s.T(), logStr, "start_time=") | ||
require.Contains(s.T(), logStr, "grpc.time_ms=") | ||
|
||
// 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(InterceptorLogger(logger), opts...), | ||
// Add any other interceptor you want. | ||
), | ||
grpc.WithChainStreamInterceptor( | ||
logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), | ||
// Add any other interceptor you want. | ||
), | ||
) | ||
// Output: | ||
} |
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 @@ | ||
// 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...)) | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,70 +1,66 @@ | ||
// Copyright (c) The go-grpc-middleware Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package log_test | ||
package examplelog_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" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"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...)) | ||
}) | ||
type logExampleTestSuite struct { | ||
*testpb.InterceptorTestSuite | ||
logBuffer *bytes.Buffer | ||
} | ||
|
||
func ExampleInterceptorLogger() { | ||
logger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile) | ||
func TestSuite(t *testing.T) { | ||
if strings.HasPrefix(runtime.Version(), "go1.7") { | ||
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7") | ||
return | ||
} | ||
buffer := &bytes.Buffer{} | ||
logger := examplelog.InterceptorLogger(log.New(buffer, "", 0)) | ||
|
||
s := &logExampleTestSuite{ | ||
InterceptorTestSuite: &testpb.InterceptorTestSuite{ | ||
TestService: &testpb.TestPingService{}, | ||
}, | ||
logBuffer: buffer, | ||
} | ||
|
||
opts := []logging.Option{ | ||
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), | ||
// Add any other option (check functions starting with logging.With). | ||
s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{ | ||
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)), | ||
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)), | ||
} | ||
|
||
// 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(InterceptorLogger(logger), opts...), | ||
// Add any other interceptor you want. | ||
), | ||
grpc.ChainStreamInterceptor( | ||
logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), | ||
// Add any other interceptor you want. | ||
), | ||
) | ||
// ...user server. | ||
suite.Run(t, s) | ||
} | ||
|
||
func (s *logExampleTestSuite) TestPing() { | ||
ctx := context.Background() | ||
_, err := s.Client.Ping(ctx, testpb.GoodPing) | ||
assert.NoError(s.T(), err, "there must be not be an on a successful call") | ||
logStr := s.logBuffer.String() | ||
require.Contains(s.T(), logStr, "msg INFO") | ||
require.Contains(s.T(), logStr, ":started call") | ||
require.Contains(s.T(), logStr, "protocol grpc") | ||
require.Contains(s.T(), logStr, "grpc.component server") | ||
require.Contains(s.T(), logStr, "grpc.service testing.testpb.v1.TestService") | ||
require.Contains(s.T(), logStr, "grpc.method Ping") | ||
require.Contains(s.T(), logStr, "grpc.method_type unary") | ||
require.Contains(s.T(), logStr, "start_time ") | ||
require.Contains(s.T(), logStr, "grpc.time_ms") | ||
|
||
// 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(InterceptorLogger(logger), opts...), | ||
// Add any other interceptor you want. | ||
), | ||
grpc.WithChainStreamInterceptor( | ||
logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), | ||
// Add any other interceptor you want. | ||
), | ||
) | ||
// Output: | ||
} |
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,40 @@ | ||
// 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)) | ||
} | ||
}) | ||
} |
Oops, something went wrong.