log

package
v16.4.0-rc42 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// LogTimestampFormat defines the timestamp format in log files
	LogTimestampFormat = "2006-01-02T15:04:05.000"
	// LogTimestampFormatUTC defines the utc timestamp format in log files
	LogTimestampFormatUTC = "2006-01-02T15:04:05.000Z"
)
View Source
const (
	// GitalyLogDirEnvKey defines the environment variable used to specify the Gitaly log directory
	GitalyLogDirEnvKey = "GITALY_LOG_DIR"
)

Variables

View Source
var SkipReplacingGlobalLoggers bool

SkipReplacingGlobalLoggers will cause `Configure()` to skip replacing global loggers. This is mostly a hack: command line applications are expected to call `log.Configure()` in their subcommand actions, and that should indeed always replace global loggers, as well. But when running tests, we invoke the subcommand actions multiple times, which is thus re-configuring the logger repeatedly. Because global logger are per definition a global shared resource, the consequence is that we might end up replacing the global loggers while tests are using them, and this race rightfully gets detected by Go's race detector.

This variable should thus only be set in the testhelper's setup routines such that we configure the global logger a single time for all of our tests, only.

Functions

func AddFields added in v16.4.0

func AddFields(ctx context.Context, fields Fields)

AddFields adds the given log fields to the context so that it will be used by any context logger extracted via `FromContext()`.

func DeciderOption

func DeciderOption() grpcmwlogrus.Option

DeciderOption returns a Option to support log filtering. If "GITALY_LOG_REQUEST_METHOD_DENY_PATTERN" ENV variable is set, logger will filter out the log whose "fullMethodName" matches it; If "GITALY_LOG_REQUEST_METHOD_ALLOW_PATTERN" ENV variable is set, logger will only keep the log whose "fullMethodName" matches it; Under any conditions, the error log will not be filtered out; If the ENV variables are not set, there will be no additional effects.

func InitContextCustomFields added in v16.2.0

func InitContextCustomFields(ctx context.Context) context.Context

InitContextCustomFields returns a new context with `CustomFields` added to the given context.

func MessageProducer

func MessageProducer(mp grpcmwlogrus.MessageProducer, fieldsProducers ...FieldsProducer) grpcmwlogrus.MessageProducer

MessageProducer returns a wrapper that extends passed mp to accept additional fields generated by each of the fieldsProducers.

func PropagationMessageProducer

func PropagationMessageProducer(actual grpcmwlogrus.MessageProducer) grpcmwlogrus.MessageProducer

PropagationMessageProducer catches logging information from the context and populates it to the special holder that should be present in the context. Should be used only in combination with PerRPCLogHandler.

func StreamLogDataCatcherServerInterceptor

func StreamLogDataCatcherServerInterceptor() grpc.StreamServerInterceptor

StreamLogDataCatcherServerInterceptor catches logging data produced by the upper interceptors and propagates it into the holder to pop up it to the HandleRPC method of the PerRPCLogHandler.

func UTCJsonFormatter

func UTCJsonFormatter() logrus.Formatter

UTCJsonFormatter returns a Formatter that formats a logrus Entry's as json and converts the time field into UTC

func UTCTextFormatter

func UTCTextFormatter() logrus.Formatter

UTCTextFormatter returns a Formatter that formats a logrus Entry's as text and converts the time field into UTC

func UnaryLogDataCatcherServerInterceptor

func UnaryLogDataCatcherServerInterceptor() grpc.UnaryServerInterceptor

UnaryLogDataCatcherServerInterceptor catches logging data produced by the upper interceptors and propagates it into the holder to pop up it to the HandleRPC method of the PerRPCLogHandler.

Types

type Config added in v16.3.0

type Config struct {
	Dir    string `toml:"dir,omitempty" json:"dir"`
	Format string `toml:"format,omitempty" json:"format"`
	Level  string `toml:"level,omitempty" json:"level"`
}

Config contains logging configuration values

type CustomFields added in v16.2.0

type CustomFields struct {
	sync.Mutex
	// contains filtered or unexported fields
}

CustomFields stores custom fields, which will be logged as a part of gRPC logs. The gRPC server is expected to add corresponding interceptors. They initialize a CustomFields object and inject it into the context. Callers can pull the object out with CustomFieldsFromContext.

func CustomFieldsFromContext added in v16.2.0

func CustomFieldsFromContext(ctx context.Context) *CustomFields

CustomFieldsFromContext gets the `CustomFields` from the given context.

func (*CustomFields) Fields added in v16.2.0

func (fields *CustomFields) Fields() Fields

Fields returns all the fields as Fields

func (*CustomFields) RecordMax added in v16.2.0

func (fields *CustomFields) RecordMax(key string, value int)

RecordMax will store the max value for a given key.

func (*CustomFields) RecordMetadata added in v16.2.0

func (fields *CustomFields) RecordMetadata(key string, value any)

RecordMetadata records a string metadata for the given key.

func (*CustomFields) RecordSum added in v16.2.0

func (fields *CustomFields) RecordSum(key string, value int)

RecordSum sums up all the values for a given key.

type Fields added in v16.4.0

type Fields = logrus.Fields

Fields contains key-value pairs of structured logging data.

type FieldsProducer

type FieldsProducer func(context.Context, error) Fields

FieldsProducer returns fields that need to be added into the logging context. error argument is the result of RPC handling.

type Logger added in v16.4.0

type Logger interface {
	WithField(key string, value any) Logger
	WithFields(fields Fields) Logger
	WithError(err error) Logger

	Debugf(format string, args ...any)
	Infof(format string, args ...any)
	Printf(format string, args ...any)
	Warnf(format string, args ...any)
	Warningf(format string, args ...any)
	Errorf(format string, args ...any)
	Fatalf(format string, args ...any)
	Panicf(format string, args ...any)

	Debug(args ...any)
	Info(args ...any)
	Print(args ...any)
	Warn(args ...any)
	Warning(args ...any)
	Error(args ...any)
	Fatal(args ...any)
	Panic(args ...any)

	Debugln(args ...any)
	Infoln(args ...any)
	Println(args ...any)
	Warnln(args ...any)
	Warningln(args ...any)
	Errorln(args ...any)
	Fatalln(args ...any)
	Panicln(args ...any)

	StreamServerInterceptor(...grpcmwlogrus.Option) grpc.StreamServerInterceptor
	UnaryServerInterceptor(...grpcmwlogrus.Option) grpc.UnaryServerInterceptor
}

Logger is the logging type used by Gitaly.

func Configure

func Configure(out io.Writer, format string, level string, hooks ...logrus.Hook) (Logger, error)

Configure configures the default and gRPC loggers. The gRPC logger's log level will be mapped in order to decrease its default verbosity. Returns the configured default logger that would also be returned by `Default()`.

func ConfigureCommand added in v16.4.0

func ConfigureCommand() Logger

ConfigureCommand configures the logging infrastructure such that it can be used with simple one-off commands. This configuration is supposed to be opinionated and ensures that all one-off commands behave in a sane way:

  • The server configuration does not influence logs generated by the command. This is done intentionally as you don't want to force administrators to adapt the server configuration to influence normal commands.

  • The output always goes to stderr such that output that is supposed to be consumed can be separated from log messages.

  • The output uses text format as it is supposed to be human-readable, not machine-readable.

  • The default log level is set to "error" such that we don't generate tons of log messages that are ultimately uninteresting.

Servers and commands with special requirements should instead use `Configure()`.

type LogrusLogger added in v16.4.0

type LogrusLogger struct {
	*logrus.Entry
}

LogrusLogger is an implementation of the Logger interface that is implemented via a `logrus.FieldLogger`.

func FromContext added in v16.4.0

func FromContext(ctx context.Context) LogrusLogger

FromContext extracts the logger from the context. If no logger has been injected then this will return a discarding logger.

func FromLogrusEntry added in v16.4.0

func FromLogrusEntry(entry *logrus.Entry) LogrusLogger

FromLogrusEntry constructs a new Gitaly-specific logger from a `logrus.Logger`.

func (LogrusLogger) StreamServerInterceptor added in v16.4.0

func (l LogrusLogger) StreamServerInterceptor(opts ...grpcmwlogrus.Option) grpc.StreamServerInterceptor

StreamServerInterceptor creates a gRPC interceptor that generates log messages for streaming RPC calls.

func (LogrusLogger) ToContext added in v16.4.0

func (l LogrusLogger) ToContext(ctx context.Context) context.Context

ToContext injects the logger into the given context so that it can be retrieved via `FromContext()`.

func (LogrusLogger) UnaryServerInterceptor added in v16.4.0

func (l LogrusLogger) UnaryServerInterceptor(opts ...grpcmwlogrus.Option) grpc.UnaryServerInterceptor

UnaryServerInterceptor creates a gRPC interceptor that generates log messages for unary RPC calls.

func (LogrusLogger) WithError added in v16.4.0

func (l LogrusLogger) WithError(err error) Logger

WithError creates a new logger with an appended error field.

func (LogrusLogger) WithField added in v16.4.0

func (l LogrusLogger) WithField(key string, value any) Logger

WithField creates a new logger with the given field appended.

func (LogrusLogger) WithFields added in v16.4.0

func (l LogrusLogger) WithFields(fields Fields) Logger

WithFields creates a new logger with the given fields appended.

type PerRPCLogHandler

type PerRPCLogHandler struct {
	Underlying     stats.Handler
	FieldProducers []FieldsProducer
}

PerRPCLogHandler is designed to collect stats that are accessible from the google.golang.org/grpc/stats.Handler, because some information can't be extracted on the interceptors level.

func (PerRPCLogHandler) HandleConn

func (lh PerRPCLogHandler) HandleConn(ctx context.Context, cs stats.ConnStats)

HandleConn only calls Underlying and exists to satisfy gRPC stats.Handler.

func (PerRPCLogHandler) HandleRPC

func (lh PerRPCLogHandler) HandleRPC(ctx context.Context, rs stats.RPCStats)

HandleRPC catches each RPC call and for the *stats.End stats invokes custom message producers to populate logging data. Once all data is collected the actual logging happens by using logger that is caught by PropagationMessageProducer.

func (PerRPCLogHandler) TagConn

TagConn only calls Underlying and exists to satisfy gRPC stats.Handler.

func (PerRPCLogHandler) TagRPC

TagRPC propagates a special data holder into the context that is responsible to hold logging information produced by the logging interceptor. The logging data should be caught by the UnaryLogDataCatcherServerInterceptor. It needs to be included into the interceptor chain below logging interceptor.

type URLSanitizerHook added in v16.3.0

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

URLSanitizerHook stores which gRPC methods to perform sanitization for.

func NewURLSanitizerHook added in v16.3.0

func NewURLSanitizerHook() *URLSanitizerHook

NewURLSanitizerHook returns a new logrus hook for sanitizing URLs.

func (*URLSanitizerHook) AddPossibleGrpcMethod added in v16.3.0

func (hook *URLSanitizerHook) AddPossibleGrpcMethod(methods ...string)

AddPossibleGrpcMethod adds method names that we should sanitize URLs from their logs.

func (*URLSanitizerHook) Fire added in v16.3.0

func (hook *URLSanitizerHook) Fire(entry *logrus.Entry) error

Fire is called by logrus.

func (*URLSanitizerHook) Levels added in v16.3.0

func (hook *URLSanitizerHook) Levels() []logrus.Level

Levels is called by logrus.

Jump to

Keyboard shortcuts

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