zerolog

package module
v0.0.0-...-e41e6bd Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package zerolog provides a small adapter required to use zerolog in logging gRPC middlewares. Please see examples for examples of use.

Example (InitializationWithCustomLevels)
package main

import (
	"os"

	grpczerolog "github.com/grpc-ecosystem/go-grpc-middleware/providers/zerolog"
	"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
	"github.com/rs/zerolog"
	"google.golang.org/grpc"
)

var customFunc logging.CodeToLevel

func main() {
	// Logger is used, allowing pre-definition of certain fields by the user.
	logger := zerolog.New(os.Stderr)
	// 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(
		grpc.ChainUnaryInterceptor(
			logging.UnaryServerInterceptor(grpczerolog.InterceptorLogger(logger), opts...),
		),
		grpc.ChainStreamInterceptor(
			logging.StreamServerInterceptor(grpczerolog.InterceptorLogger(logger), opts...),
		),
	)
}
Output:

Example (InitializationWithDurationFieldOverride)
package main

import (
	"os"

	grpczerolog "github.com/grpc-ecosystem/go-grpc-middleware/providers/zerolog"
	"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
	"github.com/rs/zerolog"
	"google.golang.org/grpc"
)

var customDurationToFields logging.DurationToFields

func main() {
	// Logger is used, allowing pre-definition of certain fields by the user.
	logger := zerolog.New(os.Stderr)
	// 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(
		grpc.ChainUnaryInterceptor(
			logging.UnaryServerInterceptor(grpczerolog.InterceptorLogger(logger), opts...),
		),
		grpc.ChainStreamInterceptor(
			logging.StreamServerInterceptor(grpczerolog.InterceptorLogger(logger), opts...),
		),
	)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	zerolog.Logger
}

Logger is a zerolog logging adapter compatible with logging middlewares.

func InterceptorLogger

func InterceptorLogger(logger zerolog.Logger) *Logger

InterceptorLogger is a zerolog.Logger to Logger adapter.

func (*Logger) Log

func (l *Logger) Log(lvl logging.Level, msg string)

Log implements the logging.Logger interface.

func (Logger) With

func (l Logger) With(fields ...string) logging.Logger

With implements the logging.Logger interface.

Jump to

Keyboard shortcuts

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