zerolog

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2020 License: MIT Imports: 15 Imported by: 0

README

gRPC Zerolog

BuildStatus Widget CodeCov Widget GoReport Widget GoDoc Widget

Implementation of gRPC Logging Middleware, integrating Zerolog as a gRPC Interceptor to log the following fields:

  • Request Protobufs as JSON.
  • Response Protobufs as JSON, or Errors.
  • Status Code, Duration, Timestamp, Service Name, Service Method, IP, Metadata Fields and User Agent.

Usage

import (
	"github.com/philip-bui/grpc-zerolog"
)

func main() {
	// With global Zerolog logger.
	grpc.NewServer(
		zerolog.UnaryInterceptor(),
	)

	// With custom Zerolog instance.
	log := zerolog.New(os.Stdout)
	grpc.NewServer(
		zerolog.UnaryInterceptorWithLogger(&log),
	)
}

License

gRPC Zerolog is available under the MIT license. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Marshaller of Protobuf to JSON
	Marshaller = &jsonpb.Marshaler{}
	// TimestampLog call start.
	TimestampLog = true
	// ServiceField key.
	ServiceField = "service"
	// ServiceLog gRPC service name.
	ServiceLog = true
	// MethodField key.
	MethodField = "method"
	// MethodLog gRPC method name.
	MethodLog = true
	// DurationField key.
	DurationField = "dur"
	// DurationLog gRPC call duration.
	DurationLog = true
	// IPField key.
	IPField = "ip"
	// IPLog gRPC client IP.
	IPLog = true
	// MetadataField key.
	MetadataField = "md"
	// MetadataLog gRPC call metadata.
	MetadataLog = true
	// UserAgentField key.
	UserAgentField = "ua"
	// UserAgentLog gRPC client User Agent.
	UserAgentLog = true
	// ReqField key.
	ReqField = "req"
	// ReqLog gRPC request body.
	ReqLog = true
	// RespField key.
	RespField = "resp"
	// RespLog gRPC response body.
	RespLog = true
	// MaxSize to log gRPC bodies.
	MaxSize = 2048000
	// CodeField gRPC status code response.
	CodeField = "code"
	// MsgField gRPC response message.
	MsgField = "msg"
	// DetailsField gRPC response errors.
	DetailsField = "details"
	// UnaryMessageDefault of logging messages from unary.
	UnaryMessageDefault = "unary"
)

Functions

func GetRawJSON

func GetRawJSON(i interface{}) *bytes.Buffer

GetRawJSON converts a Protobuf message to JSON bytes if less than MaxSize.

func GrpcLogSetNewZeroLogger

func GrpcLogSetNewZeroLogger()

GrpcLogSetNewZeroLogger sets grpclog to a new GrpcZeroLogger.

func GrpcLogSetZeroLogger

func GrpcLogSetZeroLogger(logger GrpcZeroLogger)

GrpcLogSetZeroLogger sets grpclog to a GrpcZeroLogger.

func LogDuration

func LogDuration(logger *zerolog.Event, t time.Time)

LogDuration in seconds of gRPC call.

{
	DurationField: Timestamp,
}

func LogIP

func LogIP(ctx context.Context, logger *zerolog.Event)

LogIP address of gRPC client, if assigned.

{
	IpField: 127.0.0.1
}

func LogIncomingCall

func LogIncomingCall(ctx context.Context, logger *zerolog.Event, method string, t time.Time, req interface{})

LogIncomingCall of gRPC method.

{
	ServiceField: ExampleService,
	MethodField: ExampleMethod,
	DurationField: 1.00,
}

func LogIncomingMetadata

func LogIncomingMetadata(ctx context.Context, e *zerolog.Event)

LogIncomingMetadata or UserAgent field of incoming gRPC Request, if assigned.

{
	MetadataField: {
		MetadataKey1: MetadataValue1,
	}
}

{
	UserAgentField: "Client-assigned User-Agent",
}

func LogMetadata

func LogMetadata(md *metadata.MD) *zerolog.Event

LogMetadata of gRPC Request

{
	MetadataField: {
		MetadataKey1: MetadataValue1,
	}
}

func LogMethod

func LogMethod(logger *zerolog.Event, method string)

LogMethod of gRPC call.

{
	MethodField: gRPCMethodName,
}

func LogRequest

func LogRequest(e *zerolog.Event, req interface{})

LogRequest in JSON of gRPC Call, given Request is smaller than MaxSize (Default=2MB).

{
	ReqField: {}
}

func LogResponse

func LogResponse(e *zerolog.Event, resp interface{})

LogResponse in JSON of gRPC Call, given Response is smaller than MaxSize (Default=2MB).

{
	RespField: {}
}

func LogService

func LogService(logger *zerolog.Event, method string)

LogService of gRPC name.

{
	ServiceField: gRPCServiceName,
}

func LogStatusError

func LogStatusError(logger *zerolog.Event, err error)

LogStatusError of gRPC Error Response.

{
	Err: "An unexpected error occurred",
	CodeField: "Unknown",
	MsgField: "Error message returned from the server",
	DetailsField: [Errors],
}

func LogTimestamp

func LogTimestamp(logger *zerolog.Event, t time.Time)

LogTimestamp of call.

{
	TimestampField: Timestamp,
}

func LogUserAgent

func LogUserAgent(logger *zerolog.Event, md *metadata.MD)

LogUserAgent of gRPC Client, if assigned.

{
	UserAgentField: "Client-assigned User-Agent",
}

func NewUnaryServerInterceptor

func NewUnaryServerInterceptor() grpc.UnaryServerInterceptor

NewUnaryServerInterceptor that logs gRPC Requests using Zerolog.

{
	ServiceField: "ExampleService",
	MethodField: "ExampleMethod",
	DurationField: 1.00

	IpField: "127.0.0.1",

	MetadataField: {},

	UserAgentField: "ExampleClientUserAgent",
	ReqField: {}, // JSON representation of Request Protobuf

	Err: "An unexpected error occurred",
	CodeField: "Unknown",
	MsgField: "Error message returned from the server",
	DetailsField: [Errors],

	RespField: {}, // JSON representation of Response Protobuf

	ZerologMessageField: "UnaryMessageDefault",
}

func NewUnaryServerInterceptorWithLogger

func NewUnaryServerInterceptorWithLogger(log *zerolog.Logger) grpc.UnaryServerInterceptor

func UnaryInterceptor

func UnaryInterceptor() grpc.ServerOption

UnaryInterceptor is a gRPC Server Option that uses NewUnaryServerInterceptor() to log gRPC Requests.

func UnaryInterceptorWithLogger

func UnaryInterceptorWithLogger(log *zerolog.Logger) grpc.ServerOption

Types

type GrpcZeroLogger

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

GrpcZeroLogger transforms grpc log calls to Zerolog logger.

func NewGrpcZeroLogger

func NewGrpcZeroLogger(logger zerolog.Logger) GrpcZeroLogger

NewGrpcZeroLogger creates a new GrpcZeroLogger

func (GrpcZeroLogger) Error

func (l GrpcZeroLogger) Error(args ...interface{})

Error errors arguments.

func (GrpcZeroLogger) Errorf

func (l GrpcZeroLogger) Errorf(format string, args ...interface{})

Errorf errors formatted string with arguments.

func (GrpcZeroLogger) Errorln

func (l GrpcZeroLogger) Errorln(args ...interface{})

Errorln errors and new line.

func (GrpcZeroLogger) Fatal

func (l GrpcZeroLogger) Fatal(args ...interface{})

Fatal fatals arguments.

func (GrpcZeroLogger) Fatalf

func (l GrpcZeroLogger) Fatalf(format string, args ...interface{})

Fatalf fatals formatted string with arguments.

func (GrpcZeroLogger) Fatalln

func (l GrpcZeroLogger) Fatalln(args ...interface{})

Fatalln fatals and new line.

func (GrpcZeroLogger) Info

func (l GrpcZeroLogger) Info(args ...interface{})

Info infos arguments.

func (GrpcZeroLogger) Infof

func (l GrpcZeroLogger) Infof(format string, args ...interface{})

Infof infos formatted string with arguments.

func (GrpcZeroLogger) Infoln

func (l GrpcZeroLogger) Infoln(args ...interface{})

Infoln infos and new line.

func (GrpcZeroLogger) Print

func (l GrpcZeroLogger) Print(args ...interface{})

Print logs arguments.

func (GrpcZeroLogger) Printf

func (l GrpcZeroLogger) Printf(format string, args ...interface{})

Printf logs formatted string with arguments.

func (GrpcZeroLogger) Println

func (l GrpcZeroLogger) Println(args ...interface{})

Println logs with new line.

func (GrpcZeroLogger) V

func (l GrpcZeroLogger) V(level int) bool

V determines Verbosity Level.

func (GrpcZeroLogger) Warning

func (l GrpcZeroLogger) Warning(args ...interface{})

Warning warns arguments.

func (GrpcZeroLogger) Warningf

func (l GrpcZeroLogger) Warningf(format string, args ...interface{})

Warningf warns formatted string with arguments.

func (GrpcZeroLogger) Warningln

func (l GrpcZeroLogger) Warningln(args ...interface{})

Warningln warns and new line.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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