Documentation ¶
Overview ¶
Package zaplogger allows to set and use a go.uber.org/zap logger in gRPC service handlers
Example (OtherInterceptors) ¶
Example_otherInterceptors shows how to use the interceptors of zaplogger
package main import ( "google.golang.org/grpc" "github.com/jucrouzet/grpcutils/internal/pkg/foobar" "github.com/jucrouzet/grpcutils/pkg/zaplogger" ) func main() { l, err := zaplogger.New() if err != nil { panic(err) } // Interceptor can be used directly server := grpc.NewServer( grpc.UnaryInterceptor(l.UnaryInterceptor()), grpc.StreamInterceptor(l.StreamInterceptor()), ) // Or chained with other interceptors server = grpc.NewServer( grpc.ChainUnaryInterceptor( // UnaryInterceptor l.UnaryInterceptor(), // other interceptor ), grpc.ChainStreamInterceptor( // StreamInterceptor l.StreamInterceptor(), // other interceptor ), ) foobar.RegisterDummyServiceServer(server, &foobar.UnimplementedDummyServiceServer{}) }
Output:
Index ¶
Examples ¶
Constants ¶
const ( // FieldServerName adds the server name in log messages FieldServerName = "server_name" // FieldServerType adds the server type in log messages FieldServerType = "server_type" // FieldRemoteAddr adds the remote address of the caller in log messages FieldRemoteAddr = "remote_addr" // FieldMethod adds the called method name in log messages FieldMethod = "method" // FieldRequestID adds the request unique correlation ID in log messages // See github.com/jucrouzet/grpcutils/pkg/requestid FieldRequestID = "requestid" )
Variables ¶
var ( // ErrInvalidOptionValue is returned when trying to use an invalid option value ErrInvalidOptionValue = errors.New("invalid option value") // ErrNoLoggerInContext is returned when trying get a logger from a context that doesn't have one ErrNoLoggerInContext = errors.New("no logger in context") )
Functions ¶
func GetFromContext ¶
GetFromContext returns the logger from a context that has been set in UnaryInterceptor or StreamInterceptor. If logger is not set in context and noopLoggerIfNotPresent is not specified or false ErrNoLoggerInContext is returned. If logger is not set in context and noopLoggerIfNotPresent is true, a noop logger is returned and err can be ignored.
Example (Stream) ¶
package main import ( "go.uber.org/zap" "github.com/jucrouzet/grpcutils/internal/pkg/foobar" "github.com/jucrouzet/grpcutils/pkg/zaplogger" ) func main() { // in the body of a service method like : // func MyServiceStreamMethod(s service.Service_MethodServer) error { logger, err := zaplogger.GetFromContext(s.Context()) if err != nil { panic(err) } logger.With(zap.String("foo", "bar")).Info("important message") } var s foobar.DummyService_FooSServer
Output:
Example (Unary) ¶
package main import ( "context" "go.uber.org/zap" "github.com/jucrouzet/grpcutils/pkg/zaplogger" ) func main() { // in the body of a service method like : // func MyServiceUnaryMethod(ctx context.Context, param service.Type) (service.Type, error) { logger, err := zaplogger.GetFromContext(ctx) if err != nil { panic(err) } logger.With(zap.String("foo", "bar")).Info("important message") } var ctx context.Context
Output:
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a uber/zap logger for a grpc server methods
func New ¶
New creates a new instance of Logger with specified options
Example ¶
ExampleNew creates a new zaplogger
package main import ( "github.com/jucrouzet/grpcutils/pkg/zaplogger" ) func main() { l, err := zaplogger.New( zaplogger.WithServerName("foobar service"), zaplogger.WithFields( zaplogger.FieldMethod, zaplogger.FieldRemoteAddr, zaplogger.FieldServerName, ), ) if err != nil { panic(err) } l.GetLogger().Debug("hello world") }
Output:
func (*Logger) StreamInterceptor ¶
func (l *Logger) StreamInterceptor() grpc.StreamServerInterceptor
StreamInterceptor returns a gRPC server stream interceptor that sets logger in stream context
func (*Logger) UnaryInterceptor ¶
func (l *Logger) UnaryInterceptor() grpc.UnaryServerInterceptor
UnaryInterceptor returns a gRPC server unary interceptor that sets logger in call context
type Option ¶
Option is the Logger option functions type
func WithLogger ¶
WithLogger specifies which uber/zap instance to use for logging. If not set, a new Development logger will be created.
Example ¶
ExampleWithLogger creates a new zaplogger specifying the zap logger
package main import ( "go.uber.org/zap" "github.com/jucrouzet/grpcutils/pkg/zaplogger" ) func main() { logger, err := zap.NewProduction() if err != nil { panic(err) } a, err := zaplogger.New( zaplogger.WithLogger(logger), zaplogger.WithServerName("foobar service"), ) if err != nil { panic(err) } a.GetLogger().Debug("hello world") }
Output:
func WithServerName ¶
WithServerName sets the FieldServerName field value to log message.