Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2023-08-29 09:18:58 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2023-09-05 08:43:29 +0300
commit060b140dcb267a821cb397bcb606d6d798d24109 (patch)
treee826522f092b0a1978a094668f365f7b967739bf
parent7a20c469ae6468868cda814aa4bc056bbe53eeaa (diff)
log: Remove global logging instance
We have refactored all uses of our global logging instance in favor of using either an injected logger or the logger provided via the context. Let's drop it and instead only configure the standard logger without exposing it in any way. This ensures that dependencies which use it will continue to function as expected.
-rw-r--r--internal/log/logger.go32
1 files changed, 14 insertions, 18 deletions
diff --git a/internal/log/logger.go b/internal/log/logger.go
index 7aacc409f..899cf5d06 100644
--- a/internal/log/logger.go
+++ b/internal/log/logger.go
@@ -39,17 +39,6 @@ func UTCTextFormatter() logrus.Formatter {
return &utcFormatter{Formatter: &logrus.TextFormatter{TimestampFormat: LogTimestampFormatUTC}}
}
-// defaultLogger is the logger that is returned via `Default()` and via logrus' direct log invocations like e.g.
-// `logrus.Info()`. By default it is configured to log to standard output, but in practice it should be configured via
-// a call to `Configure()` after the configuration has been loaded.
-var defaultLogger = func() *logrus.Logger {
- //nolint:forbidigo // We reuse the standard logger such that dependencies which might use logrus are properly
- // configured, as well.
- logger := logrus.StandardLogger()
- logger.Out = os.Stdout
- return logger
-}()
-
// 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
@@ -71,22 +60,32 @@ type Config struct {
// 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 Configure(out io.Writer, format string, level string, hooks ...logrus.Hook) (logrus.FieldLogger, error) {
- if err := configure(defaultLogger, out, format, level, hooks...); err != nil {
- return nil, err
+ logger := logrus.New() //nolint:forbidigo
+
+ if err := configure(logger, out, format, level, hooks...); err != nil {
+ return nil, fmt.Errorf("configuring logger: %w", err)
}
if !SkipReplacingGlobalLoggers {
+ // Replace the logrus standar logger. While we shouldn't ever be using it in our own codebase, there
+ // will very likely be cases where dependencies use it.
+ //
+ //nolint:forbidigo
+ if err := configure(logrus.StandardLogger(), out, format, level, hooks...); err != nil {
+ return nil, fmt.Errorf("configuring global logrus logger: %w", err)
+ }
+
// We replace the gRPC logger with a custom one because the default one is too chatty.
grpcLogger := logrus.New() //nolint:forbidigo
if err := configure(grpcLogger, out, format, mapGRPCLogLevel(level), hooks...); err != nil {
- return nil, err
+ return nil, fmt.Errorf("configuring global gRPC logger: %w", err)
}
grpcmwlogrus.ReplaceGrpcLogger(grpcLogger.WithField("pid", os.Getpid()))
}
- return Default(), nil
+ return logger.WithField("pid", os.Getpid()), nil
}
func configure(logger *logrus.Logger, out io.Writer, format, level string, hooks ...logrus.Hook) error {
@@ -134,6 +133,3 @@ func mapGRPCLogLevel(level string) string {
return level
}
-
-// Default returns the default logger that has been configured via `Configure()`.
-func Default() *logrus.Entry { return defaultLogger.WithField("pid", os.Getpid()) }