Skip to content

Commit

Permalink
added GrcpGatewayLogger that implements grpc-middleware (#39)
Browse files Browse the repository at this point in the history
* added GrcpGatewayLogger that implements grpc-middleware

* added grcp-gateway example in readme

* expand grcp-gateway example in readme

Co-authored-by: Oscar Gimenez <oscar@daemez.com>
  • Loading branch information
ogimenezb and ogimenezb authored Jun 14, 2021
1 parent d0a334a commit 22df5ca
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,46 @@ func main() {
}
```

### GrcpGateway Interceptor

Using wrapped loggers for grcp-gateway in go-grpc-middleware.

```go
package main

import (
"context"
"testing"

grpcphuslog "github.com/grpc-ecosystem/go-grpc-middleware/providers/phuslog/v2"
"github.com/phuslu/log"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"

middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/tags"
)

func Example_initialization() {
// Logger is used, allowing pre-definition of certain fields by the user.
logger := log.DefaultLogger.GrcpGateway()
// You can also add grpc LoggerV2 logger wrapper
grpclog.SetLoggerV2(log.DefaultLogger.Grpc(nil))
// Create a server, make sure we put the tags context before everything else.
_ = grpc.NewServer(
middleware.WithUnaryServerChain(
tags.UnaryServerInterceptor(),
logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger)),
),
middleware.WithStreamServerChain(
tags.StreamServerInterceptor(),
logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger)),
),
)
}
```

### User-defined Data Structure

To log with user-defined struct effectively, implements `MarshalObject`. [![playground][play-marshal-img]][play-marshal]
Expand Down

0 comments on commit 22df5ca

Please sign in to comment.