Skip to content

Commit

Permalink
Added customizable GRPC-Zerolog output writer option (#5)
Browse files Browse the repository at this point in the history
Fixed GRPC-Zerolog output writer to be customizable and default to global Zerolog instance.
  • Loading branch information
philip-bui committed Sep 5, 2020
1 parent ab6699b commit 3f3b16b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ import (
)

func main() {
// With global Zerolog logger.
grpc.NewServer(
zerolog.UnaryInterceptor(),
)

// With custom Zerolog instance.
log := zerolog.New(os.Stdout)
grpc.NewServer(
zerolog.UnaryInterceptorWithLogger(&log),
)
}
```

Expand Down
10 changes: 10 additions & 0 deletions unary_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
)

Expand All @@ -12,6 +14,10 @@ func UnaryInterceptor() grpc.ServerOption {
return grpc.UnaryInterceptor(NewUnaryServerInterceptor())
}

func UnaryInterceptorWithLogger(log *zerolog.Logger) grpc.ServerOption {
return grpc.UnaryInterceptor(NewUnaryServerInterceptorWithLogger(log))
}

// NewUnaryServerInterceptor that logs gRPC Requests using Zerolog.
// {
// ServiceField: "ExampleService",
Expand All @@ -35,6 +41,10 @@ func UnaryInterceptor() grpc.ServerOption {
// ZerologMessageField: "UnaryMessageDefault",
// }
func NewUnaryServerInterceptor() grpc.UnaryServerInterceptor {
return NewUnaryServerInterceptorWithLogger(&log.Logger)
}

func NewUnaryServerInterceptorWithLogger(log *zerolog.Logger) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
now := time.Now()
resp, err := handler(ctx, req)
Expand Down
7 changes: 5 additions & 2 deletions unary_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type TestUnaryInterceptorSuite struct {
suite.Suite
out *bytes.Buffer
log zerolog.Logger
client *test.TestClient
}

Expand All @@ -21,15 +22,17 @@ func TestUnaryServerInterceptor(t *testing.T) {
}

func (s *TestUnaryInterceptorSuite) SetupSuite() {
s.out = &bytes.Buffer{}
s.log = zerolog.New(s.out)
go func() {
test.StartServer(NewUnaryServerInterceptor())
test.StartServer(NewUnaryServerInterceptorWithLogger(&s.log))
}()
s.client = test.GetClient()
}

func (s *TestUnaryInterceptorSuite) SetupTest() {
s.out = &bytes.Buffer{}
log = zerolog.New(s.out)
s.log = zerolog.New(s.out)
}

func TestUnaryInterceptor(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ var (
DetailsField = "details"
// UnaryMessageDefault of logging messages from unary.
UnaryMessageDefault = "unary"
log = zerolog.Logger{}
)

// LogIncomingCall of gRPC method.
Expand Down

0 comments on commit 3f3b16b

Please sign in to comment.