log

package
v18.6.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: MIT Imports: 13 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"
)

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 ConvertLoggingFields

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 InitContextCustomFields

func InitContextCustomFields(ctx context.Context) context.Context

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

func NewLogMatcher

func NewLogMatcher() *logMatcher

NewLogMatcher provides a new logMatcher.

func NewSyncWriter

func NewSyncWriter(w io.Writer) io.Writer

NewSyncWriter returns Writer wrapped with a mutex that is acquired before each Write call.

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

Types

type Config

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

Config contains logging configuration values

type CustomFields

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

func CustomFieldsFromContext(ctx context.Context) *CustomFields

CustomFieldsFromContext gets the `CustomFields` from the given context.

func (*CustomFields) Fields

func (fields *CustomFields) Fields() Fields

Fields returns all the fields as Fields

func (*CustomFields) RecordMax

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

RecordMax will store the max value for a given key.

func (*CustomFields) RecordMetadata

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

RecordMetadata records a string metadata for the given key.

func (*CustomFields) RecordSum

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

RecordSum sums up all the values for a given key.

type Fields

type Fields = logrus.Fields

Fields contains key-value pairs of structured logging data.

type Logger

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

	// Output returns the loggers output where fully formatted log entries can be written.
	Output() io.Writer
}

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

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

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

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

func FromContext

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

func FromLogrusEntry(entry *logrus.Entry) LogrusLogger

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

func (LogrusLogger) Data

func (l LogrusLogger) Data() Fields

Data returns the data stored inside the logger entry.

func (LogrusLogger) Debug

func (l LogrusLogger) Debug(msg string)

Debug writes a log message at debug level.

func (LogrusLogger) DebugContext

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

func (l LogrusLogger) Error(msg string)

Error writes a log message at error level.

func (LogrusLogger) ErrorContext

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

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

func (LogrusLogger) Info

func (l LogrusLogger) Info(msg string)

Info writes a log message at info level.

func (LogrusLogger) InfoContext

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

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) Output

func (l LogrusLogger) Output() io.Writer

Output returns the loggers output where fully formatted log entries can be written.

func (LogrusLogger) StreamServerInterceptor

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

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

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

func (l LogrusLogger) Warn(msg string)

Warn writes a log message at warn level.

func (LogrusLogger) WarnContext

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

func (l LogrusLogger) WithError(err error) Logger

WithError creates a new logger with an appended error field.

func (LogrusLogger) WithField

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

WithField creates a new logger with the given field appended.

func (LogrusLogger) WithFields

func (l LogrusLogger) WithFields(fields Fields) Logger

WithFields creates a new logger with the given fields appended.

type URLSanitizerHook

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

URLSanitizerHook stores which gRPC methods to perform sanitization for.

func NewURLSanitizerHook

func NewURLSanitizerHook() *URLSanitizerHook

NewURLSanitizerHook returns a new logrus hook for sanitizing URLs.

func (*URLSanitizerHook) AddPossibleGrpcMethod

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

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

func (*URLSanitizerHook) Fire

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

Fire is called by logrus.

func (*URLSanitizerHook) Levels

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