logger

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2020 License: Apache-2.0 Imports: 28 Imported by: 8

README

grpc-elasticsearch-logger

Logger for gRPC server to elasticsearch with elastic APM trace ID

Go Report Card GoDoc

Documentation

Overview

Package logger is used to log all intercepted unary and stream calls. The package exports the `NewLogger` function which sets up a logger with the elogrus hook and returns it.

The logger can be configured using environment variables:

LOG_LEVEL (default: "error") - Defines the log level of the logger, possible values:

"panic", "fatal", "error", "warn", "warning", "info", "debug", "trace"

LOG_INDEX (default: "log") - Defines the index to which the logs would be indexed to, the logs would be indexed to index pattern LOG_INDEX-*, where * is the current day's date in the format 'YYYY.MM.DD'

ELASTICSEARCH_URL (default: http://localhost:9200) - Defines the url of the elasticsearch server to index the logs to.

HOST_NAME (default: executable name) - Defines the host name of the server that is using the logger, which will be logged under 'Host' field.

There's the `ElasticsearchLoggerServerInterceptor` function which sets up a `grpc.ServerOption` to intercept streams and unary calls with `*logrus.Entry` of the logger, created with `NewLogger`, and the options given to it. Returns the `grpc.ServerOption` which will be used in `grpc.NewServer` to log all incoming stream and unary calls. It also sets up the APM agent's unary server interceptor to log metrics to elastic APM.

The function `ExtractTraceParent` gets a `context.Context` which holds the "Elastic-Apm-Traceparent", which is the HTTP header for trace propagation, and returns the trace id.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultDecider added in v1.0.7

func DefaultDecider(string) bool

DefaultDecider logs every payload.

func ElasticsearchLoggerServerInterceptor

func ElasticsearchLoggerServerInterceptor(
	logrusEntry *logrus.Entry,
	serverPayloadLoggingDecider DeciderFunc,
	extractInitialRequestDecider DeciderFunc,
	opts ...grpc_logrus.Option,
) []grpc.ServerOption

ElasticsearchLoggerServerInterceptor sets up a `grpc.ServerOption` to intercept streams with `*logrus.Entry` of the logger, created with `NewLogger`, and the options given to it. Returns the `grpc.ServerOption` which will be used in `grpc.NewServer` to log all incoming calls.

func ExtractTraceParent

func ExtractTraceParent(ctx context.Context) string

ExtractTraceParent gets a `context.Context` which holds the "Elastic-Apm-Traceparent", which is the HTTP header for trace propagation, and returns the trace id.

func LogError added in v1.2.0

func LogError(logger *logrus.Logger, err error)

LogError logs err with logger.Errorf if err is non-nil.

func NewLogger

func NewLogger() *logrus.Logger

NewLogger creates a `*logrus.Logger` with `elogrus` hook, which logs to elasticsearch, and returns it.

func RequestExtractor

func RequestExtractor(entry *logrus.Entry, decider DeciderFunc) grpc_ctxtags.RequestFieldExtractorFunc

RequestExtractor extracts the request and logs it as json under the key "grpc.request.content". Pass decider function to not extract the requests for certain methods.

func SetLogger added in v1.2.0

func SetLogger(config *Config) gin.HandlerFunc

SetLogger initializes the logging middleware.

func StartSpan added in v1.2.0

func StartSpan(ctx context.Context, name string) (*apm.Span, context.Context)

StartSpan starts an externalGRPCSpanType span under the transaction in ctx, returns the created span and the context with the traceparent header matadata.

Types

type Config added in v1.2.0

type Config struct {
	Logger             *logrus.Logger
	SkipBodyPath       []string
	SkipBodyPathRegexp *regexp.Regexp
	SkipPath           []string
	SkipPathRegexp     *regexp.Regexp
}

Config is the configuration struct for the logger, Logger - a logrus Logger to use in the logger. SkipPath - path to skip logging. SkipPathRegexp - a regex to skip paths.

type DeciderFunc added in v1.0.7

type DeciderFunc func(fullMethodName string) bool

DeciderFunc is a function type to decide whether to create a log for fullMethodName.

func IgnoreServerMethodsDecider added in v1.0.7

func IgnoreServerMethodsDecider(fullIgnoredMethodNames ...string) DeciderFunc

IgnoreServerMethodsDecider ignores logging the payload of method that is equal to any string of fullIgnoredMethodNames.

type JSONPbMarshaller

type JSONPbMarshaller struct {
	proto.Message
}

JSONPbMarshaller is a struct used to marshal a protobuf message to JSON.

func (*JSONPbMarshaller) MarshalJSON

func (j *JSONPbMarshaller) MarshalJSON() ([]byte, error)

MarshalJSON marshals a protobuf message to JSON.

Jump to

Keyboard shortcuts

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