zaplogger

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2022 License: GPL-3.0 Imports: 10 Imported by: 0

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

View Source
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

View Source
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

func GetFromContext(ctx context.Context, noopLoggerIfNotPresent ...bool) (*zap.Logger, error)

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 Field

type Field string

Field is the type of logger fields

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is a uber/zap logger for a grpc server methods

func New

func New(opts ...Option) (*Logger, error)

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

func (l *Logger) GetLogger() *zap.Logger

GetLogger returns the zap logger

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

type Option func(*Logger) error

Option is the Logger option functions type

func WithFields

func WithFields(fields ...Field) Option

WithFields adds fields to log messages

func WithLogger

func WithLogger(logger *zap.Logger) Option

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

func WithServerName(serverName string) Option

WithServerName sets the FieldServerName field value to log message.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL