grpc_zap

package
v0.0.0-...-11ddc06 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamServerInterceptor

func StreamServerInterceptor(logger *zap.Logger, opts ...Option) grpc.StreamServerInterceptor
Example
package main

import (
	"context"
	"github.com/acrazing/ctxzap"
	"github.com/acrazing/ctxzap/grpc_zap"
	"go.uber.org/zap"
	"google.golang.org/grpc"
)

func main() {
	logger := zap.NewExample()
	_ = grpc.NewServer(
		grpc.ChainUnaryInterceptor(
			grpc_zap.UnaryServerInterceptor(logger, grpc_zap.WithAccessLog(true, true)),
		),
		grpc.ChainStreamInterceptor(
			grpc_zap.StreamServerInterceptor(logger, grpc_zap.WithEventLog(true, true)),
		),
	)

	// In any context based method called by grpc servers
	_ = func(ctx context.Context) {
		// add a field to the context, this field will always be printed in the next
		// log calls with the context.
		ctxzap.AddFields(ctx, zap.String("data", "1"))

		// replace an existing field or append it to the context
		ctxzap.ReplaceField(ctx, zap.String("data", "2"))

		// print a log
		ctxzap.Info(ctx, "call with data", zap.String("value", "2"))
	}
}
Output:

func UnaryServerInterceptor

func UnaryServerInterceptor(logger *zap.Logger, opts ...Option) grpc.UnaryServerInterceptor

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithAccessLog

func WithAccessLog(started, finished bool) Option

WithAccessLog enable/disable access log both started & finished log are enabled for stream server by default only finished log is enabled for unary server by default

func WithErrorFieldsFunc

func WithErrorFieldsFunc(fn func(ctx context.Context, err error) []zap.Field) Option

WithErrorFieldsFunc extract error detail fields

func WithEventLog

func WithEventLog(send, recv bool) Option

WithEventLog enable/disable event log only works with stream interceptor both send/recv are disabled by default

func WithFieldsFunc

func WithFieldsFunc(fn func(ctx context.Context) []zap.Field) Option

WithFieldsFunc attach custom zap fields to all the logs please note the values are extracted synchronously.

func WithMetadataFields

func WithMetadataFields(fields ...string) Option

WithMetadataFields attach all the specified keys to all the logs please note the values are extracted synchronously.

Jump to

Keyboard shortcuts

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