Skip to content

Commit

Permalink
v2: Add support for the phuslog logging provider (#425)
Browse files Browse the repository at this point in the history
* v2: Add support for the phuslog logging provider

* Update providers/phuslog/logger.go

fallback to error when no level is detected.
Add unknown level With to log and log error message
  • Loading branch information
ogimenezb authored Jun 19, 2021
1 parent cc20532 commit 0c113ab
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ myServer := grpc.NewServer(
* [`logrus`](providers/logrus) - integration of [logrus](https://github.com/sirupsen/logrus) logging library into gRPC handlers.
* [`kit`](providers/kit) - integration of [go-kit/log](https://github.com/go-kit/log) logging library into gRPC handlers.
* [`zerolog`](providers/zerolog) - integration of [zerolog](https://github.com/rs/zerolog) logging Library into gRPC handlers.
* [`phuslog`](providers/phuslog) - integration of [phuslog](https://github.com/phuslu/log) logging Library into gRPC handlers.

#### Monitoring
* [`grpc_prometheus`](https://github.com/grpc-ecosystem/go-grpc-prometheus) - Prometheus client-side and server-side monitoring middleware
Expand Down
1 change: 1 addition & 0 deletions interceptors/logging/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Implementations:
* providers/zap
* providers/kit
* providers/zerolog
* providers/phuslog
See relevant packages below.
*/
Expand Down
5 changes: 5 additions & 0 deletions providers/phuslog/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
Package phuslog provides a small adapter required to use phuslog in logging gRPC middlewares.
Please see examples for examples of use.
*/
package phuslog
135 changes: 135 additions & 0 deletions providers/phuslog/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package phuslog_test

import (
"context"
"testing"

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

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"
)

var (
customFunc logging.CodeToLevel
customDurationToFields logging.DurationToFields
)

func Example_initializationWithCustomLevels() {
// Logger is used, allowing pre-definition of certain fields by the user.
logger := log.DefaultLogger.GrcpGateway()
// Shared options for the logger, with a custom gRPC code to log level function.
opts := []logging.Option{
logging.WithLevels(customFunc),
}
// Create a server, make sure we put the tags context before everything else.
_ = grpc.NewServer(
middleware.WithUnaryServerChain(
tags.UnaryServerInterceptor(),
logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
tags.StreamServerInterceptor(),
logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...),
),
)
}

func Example_initializationWithDurationFieldOverride() {
// Logger is used, allowing pre-definition of certain fields by the user.
logger := log.DefaultLogger.GrcpGateway()
// Shared options for the logger, with a custom duration to log field function.
opts := []logging.Option{
logging.WithDurationField(customDurationToFields),
}
// Create a server, make sure we put the tags context before everything else.
_ = grpc.NewServer(
middleware.WithUnaryServerChain(
tags.UnaryServerInterceptor(),
logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
tags.StreamServerInterceptor(),
logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...),
),
)
}

func Example_initializationWithCodeGenRequestFieldExtractor() {
// Logger is used, allowing pre-definition of certain fields by the user.
logger := log.DefaultLogger.GrcpGateway()
// Create a server, make sure we put the tags context before everything else.
_ = grpc.NewServer(
middleware.WithUnaryServerChain(
tags.UnaryServerInterceptor(tags.WithFieldExtractor(tags.CodeGenRequestFieldExtractor)),
logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger)),
),
middleware.WithStreamServerChain(
tags.StreamServerInterceptor(tags.WithFieldExtractor(tags.CodeGenRequestFieldExtractor)),
logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger)),
),
)
}

func ExampleWithDecider() {
// Logger is used, allowing pre-definition of certain fields by the user.
logger := log.DefaultLogger.GrcpGateway()
// Shared options for the logger, with a custom decider that log everything except successful
// calls from "/blah.foo.healthcheck/Check" method.
opts := []logging.Option{
logging.WithDecider(func(methodFullName string) logging.Decision {
// will not log gRPC calls if it was a call to healthcheck and no error was raised
if methodFullName == "/blah.foo.healthcheck/Check" {
return logging.NoLogCall
}

// by default you will log all calls
return logging.LogStartAndFinishCall
}),
}
// Create a server, make sure we put the tags context before everything else.
_ = []grpc.ServerOption{
middleware.WithUnaryServerChain(
tags.UnaryServerInterceptor(),
logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
tags.StreamServerInterceptor(),
logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...),
),
}
}

func ExampleServerPayloadLoggingDecider() {
// Logger is used, allowing pre-definition of certain fields by the user.
logger := log.DefaultLogger.GrcpGateway()
// Expect payload from "/blah.foo.healthcheck/Check" call to be logged.
payloadDecider := func(ctx context.Context, fullMethodName string, servingObject interface{}) bool {
return fullMethodName == "/blah.foo.healthcheck/Check"
}

// Create a server, make sure we put the tags context before everything else.
_ = []grpc.ServerOption{
middleware.WithUnaryServerChain(
tags.UnaryServerInterceptor(),
logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger)),
logging.PayloadUnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger), payloadDecider),
),
middleware.WithStreamServerChain(
tags.StreamServerInterceptor(),
logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger)),
logging.PayloadStreamServerInterceptor(grpcphuslog.InterceptorLogger(logger), payloadDecider),
),
}
}

func TestExamplesBuildable(t *testing.T) {
Example_initializationWithCustomLevels()
Example_initializationWithDurationFieldOverride()
Example_initializationWithCodeGenRequestFieldExtractor()
ExampleWithDecider()
ExampleServerPayloadLoggingDecider()
}
9 changes: 9 additions & 0 deletions providers/phuslog/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/grpc-ecosystem/go-grpc-middleware/providers/phuslog/v2

go 1.14

require (
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.2.0.20201002093600-73cf2ae9d891
github.com/phuslu/log v1.0.72
google.golang.org/grpc v1.30.0
)
79 changes: 79 additions & 0 deletions providers/phuslog/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.2.0.20201002093600-73cf2ae9d891 h1:2F7/en805byWQR92etfFjOqtRtWsUu09R7wm6LtlHEw=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.2.0.20201002093600-73cf2ae9d891/go.mod h1:GhphxcdlaRyAuBSvo6rV71BvQcvB/vuX8ugCyybuS2k=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/phuslu/log v1.0.72 h1:bjAbUBXqipXi0TCnpkqArnFSU6qd0U+Js5aATJmRj3g=
github.com/phuslu/log v1.0.72/go.mod h1:kzJN3LRifrepxThMjufQwS7S35yFAB+jAV1qgA7eBW4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
google.golang.org/grpc v1.30.0 h1:M5a8xTlYTxwMn5ZFkwhRabsygDY5G8TYLyQDBxJNAxE=
google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
47 changes: 47 additions & 0 deletions providers/phuslog/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package phuslog

import (
"fmt"

"github.com/phuslu/log"

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

// Compatibility check.
var _ logging.Logger = &Logger{}

// Logger is a phuslog logging adapter compatible with logging middlewares.
type Logger struct {
log.GrcpGatewayLogger
}

// InterceptorLogger is a phuslog.Logger to Logger adapter.
func InterceptorLogger(logger log.GrcpGatewayLogger) *Logger {
return &Logger{logger}
}

// Log implements the logging.Logger interface.
func (l *Logger) Log(lvl logging.Level, msg string) {
switch lvl {
case logging.DEBUG:
l.Debug(msg)
case logging.INFO:
l.Info(msg)
case logging.WARNING:
l.Warning(msg)
case logging.ERROR:
l.Error(msg)
default:
l.With("error-lvl", fmt.Sprintf("phuslog: unknown level %s", lvl)).Log(logging.ERROR, msg)
}
}

// With implements the logging.Logger interface.
func (l *Logger) With(fields ...string) logging.Logger {
vals := make([]interface{}, len(fields))
for i := 0; i < len(fields); i++ {
vals[i] = fields[i]
}
return InterceptorLogger(l.WithValues(vals...))
}

0 comments on commit 0c113ab

Please sign in to comment.