log

package
v16.9.4 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 15 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 logging function like `InfoContext()` that receives a context as input.

func ConvertLoggingFields added in v16.7.0

func ConvertLoggingFields(fields grpcmwloggingv2.Fields) map[string]any

ConvertLoggingFields converts Fields in go-grpc-middleware/v2/interceptors/logging package into a general map[string]interface{}. So that other logging packages (such as Logrus) can use them.

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

	Debug(msg string)
	Info(msg string)
	Warn(msg string)
	Error(msg string)

	DebugContext(ctx context.Context, msg string)
	InfoContext(ctx context.Context, msg string)
	WarnContext(ctx context.Context, msg string)
	ErrorContext(ctx context.Context, msg string)

	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 {
	// contains filtered or unexported fields
}

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

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) Debug added in v16.5.0

func (l LogrusLogger) Debug(msg string)

Debug writes a log message at debug level.

func (LogrusLogger) DebugContext added in v16.5.0

func (l LogrusLogger) DebugContext(ctx context.Context, msg string)

DebugContext logs a new log message at Debug level. Fields added to the context via AddFields will be appended.

func (LogrusLogger) Error added in v16.5.0

func (l LogrusLogger) Error(msg string)

Error writes a log message at error level.

func (LogrusLogger) ErrorContext added in v16.5.0

func (l LogrusLogger) ErrorContext(ctx context.Context, msg string)

ErrorContext level. Fields added to the context via AddFields will be appended.

func (LogrusLogger) Info added in v16.5.0

func (l LogrusLogger) Info(msg string)

Info writes a log message at info level.

func (LogrusLogger) InfoContext added in v16.5.0

func (l LogrusLogger) InfoContext(ctx context.Context, msg string)

InfoContext logs a new log message at Info level. Fields added to the context via AddFields will be appended.

func (LogrusLogger) LogrusEntry deprecated added in v16.5.0

func (l LogrusLogger) LogrusEntry() *logrus.Entry

LogrusEntry returns the `logrus.Entry` that backs this logger. Note that this interface only exists during the transition period and will be eventually removed. It is thus heavily discouraged to use it.

Deprecated: This will be removed once all callsites have been converted to do something that is independent of the 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) 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) Warn added in v16.5.0

func (l LogrusLogger) Warn(msg string)

Warn writes a log message at warn level.

func (LogrusLogger) WarnContext added in v16.5.0

func (l LogrusLogger) WarnContext(ctx context.Context, msg string)

WarnContext logs a new log message at Warn level. Fields added to the context via AddFields will be appended.

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