logging_zap

Server Interceptor的json格式

  1. {
  2. "level": "info", // string logrus log levels
  3. "msg": "finished unary call", // string log message
  4. "grpc.code": "OK", // string grpc status code
  5. "grpc.method": "Ping", // string method name
  6. "grpc.service": "mwitkow.testproto.TestService", // string full name of the called service
  7. "grpc.start_time": "2006-01-02T15:04:05Z07:00", // string RFC3339 representation of the start time
  8. "grpc.request.deadline": "2006-01-02T15:04:05Z07:00", // string RFC3339 deadline of the current request if supplied
  9. "grpc.request.value": "something", // string value on the request
  10. "grpc.time_ms": 1.234, // float32 run time of the call in ms
  11. "peer.address": {
  12. "IP": "127.0.0.1", // string IP address of calling party
  13. "Port": 60216, // int port call is coming in on
  14. "Zone": "" // string peer zone for caller
  15. },
  16. "span.kind": "server", // string client | server
  17. "system": "grpc" // string
  18. "custom_field": "custom_value", // string user defined field
  19. "custom_tags.int": 1337, // int user defined tag on the ctx
  20. "custom_tags.string": "something", // string user defined tag on the ctx
  21. }

Payload Interceptor的json格式

  1. {
  2. "level": "info", // string logrus log levels
  3. "msg": "client request payload logged as grpc.request.content", // string log message
  4. "grpc.request.content": { // object content of RPC request
  5. "value": "something", // string defined by caller
  6. "sleepTimeMs": 9999 // int defined by caller
  7. },
  8. "grpc.method": "Ping", // string method being called
  9. "grpc.service": "mwitkow.testproto.TestService", // string service being called
  10. "span.kind": "client", // string client | server
  11. "system": "grpc" // string
  12. }

e1:Initialization

  1. package main
  2. import (
  3. grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
  4. grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
  5. grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
  6. "go.uber.org/zap"
  7. "google.golang.org/grpc"
  8. )
  9. var (
  10. zapLogger *zap.Logger
  11. customFunc grpc_zap.CodeToLevel
  12. )
  13. func main() {
  14. // Shared options for the logger, with a custom gRPC code to log level function.
  15. opts := []grpc_zap.Option{
  16. grpc_zap.WithLevels(customFunc),
  17. }
  18. // Make sure that log statements internal to gRPC library are logged using the zapLogger as well.
  19. grpc_zap.ReplaceGrpcLoggerV2(zapLogger)
  20. // Create a server, make sure we put the grpc_ctxtags context before everything else.
  21. _ = grpc.NewServer(
  22. grpc_middleware.WithUnaryServerChain(
  23. grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
  24. grpc_zap.UnaryServerInterceptor(zapLogger, opts...),
  25. ),
  26. grpc_middleware.WithStreamServerChain(
  27. grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
  28. grpc_zap.StreamServerInterceptor(zapLogger, opts...),
  29. ),
  30. )
  31. }

e2:InitializationWithDecider

  1. package main
  2. import (
  3. "time"
  4. grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
  5. grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
  6. grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
  7. "go.uber.org/zap"
  8. "go.uber.org/zap/zapcore"
  9. "google.golang.org/grpc"
  10. )
  11. var zapLogger *zap.Logger
  12. func main() {
  13. opts := []grpc_zap.Option{
  14. grpc_zap.WithDurationField(func(duration time.Duration) zapcore.Field {
  15. return zap.Int64("grpc.time_ns", duration.Nanoseconds())
  16. }),
  17. }
  18. _ = grpc.NewServer(
  19. grpc_middleware.WithUnaryServerChain(
  20. grpc_ctxtags.UnaryServerInterceptor(),
  21. grpc_zap.UnaryServerInterceptor(zapLogger, opts...),
  22. ),
  23. grpc_middleware.WithStreamServerChain(
  24. grpc_ctxtags.StreamServerInterceptor(),
  25. grpc_zap.StreamServerInterceptor(zapLogger, opts...),
  26. ),
  27. )
  28. }

func ReplaceGrpcLoggerV2(logger zap.Logger) 替换grpclog,应该在初始化就调用
func UnaryClientInterceptor(logger
zap.Logger, opts …Option) grpc.UnaryClientInterceptor
func UnaryServerInterceptor(logger zap.Logger, opts …Option) grpc.UnaryServerInterceptor
func StreamClientInterceptor(logger
zap.Logger, opts …Option) grpc.StreamClientInterceptor
func StreamServerInterceptor(logger *zap.Logger, opts …Option) grpc.StreamServerInterceptor

  1. func PayloadUnaryClientInterceptor(logger *zap.Logger, decider grpc_logging.ClientPayloadLoggingDecider) grpc.UnaryClientInterceptor
  2. func PayloadUnaryServerInterceptor(logger *zap.Logger, decider grpc_logging.ServerPayloadLoggingDecider) grpc.UnaryServerInterceptor
  3. func PayloadStreamClientInterceptor(logger *zap.Logger, decider grpc_logging.ClientPayloadLoggingDecider) grpc.StreamClientInterceptor
  4. func PayloadStreamServerInterceptor(logger *zap.Logger, decider grpc_logging.ServerPayloadLoggingDecider) grpc.StreamServerInterceptor

type CodeToLevel

  1. type CodeToLevel func(code codes.Code) zapcore.Level

type DurationToField

  1. type DurationToField func(duration time.Duration) zapcore.Field


type MessageProducer

  1. type MessageProducer func(ctx context.Context, msg string, level zapcore.Level, code codes.Code, err error, duration zapcore.Field)


type Option

func WithCodes(f grpc_logging.ErrorToCode) Option
func WithDecider(f grpc_logging.Decider) Option
func WithDurationField(f DurationToField) Option
func WithLevels(f CodeToLevel) Option
func WithMessageProducer(f MessageProducer) Option
func WithTimestampFormat(format string) Option

zap/ctxzap

func AddFields(ctx context.Context, fields …zapcore.Field)
func TagsToFields(ctx context.Context) []zapcore.Field
func ToContext(ctx context.Context, logger zap.Logger) context.Context
func Extract(ctx context.Context)
zap.Logger

  1. package main
  2. import (
  3. "context"
  4. "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
  5. "github.com/grpc-ecosystem/go-grpc-middleware/tags"
  6. pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
  7. "go.uber.org/zap"
  8. )
  9. var zapLogger *zap.Logger
  10. // Simple unary handler that adds custom fields to the requests's context. These will be used for all log statements.
  11. func main() {
  12. _ = func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
  13. // Add fields the ctxtags of the request which will be added to all extracted loggers.
  14. grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
  15. // Extract a single request-scoped zap.Logger and log messages.
  16. l := ctxzap.Extract(ctx)
  17. l.Info("some ping")
  18. l.Info("another ping")
  19. return &pb_testproto.PingResponse{Value: ping.Value}, nil
  20. }
  21. }