From d7fd9ca50692544b2c13501028c60915677bcabe Mon Sep 17 00:00:00 2001 From: James Elliott Date: Sat, 10 Sep 2022 18:02:57 +1000 Subject: [PATCH] refactor(logging): implement common interfaces (#3994) This implements and leverages some common library logging interfaces. --- internal/logging/logger.go | 16 ++++++++++++++++ internal/logging/printf.go | 29 +++++++++++++++++++++++++++++ internal/server/server.go | 3 +++ internal/session/provider_config.go | 5 +++-- internal/session/types.go | 17 +---------------- 5 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 internal/logging/printf.go diff --git a/internal/logging/logger.go b/internal/logging/logger.go index 9b5540cce..761c8e646 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -17,6 +17,22 @@ func Logger() *logrus.Logger { return logrus.StandardLogger() } +// LoggerPrintf returns a new PrintfLogger given a level. +func LoggerPrintf(level logrus.Level) (logger *PrintfLogger) { + return &PrintfLogger{ + level: level, + logrus: logrus.StandardLogger(), + } +} + +// LoggerCtxPrintf returns a new CtxPrintfLogger given a level. +func LoggerCtxPrintf(level logrus.Level) (logger *CtxPrintfLogger) { + return &CtxPrintfLogger{ + level: level, + logrus: logrus.StandardLogger(), + } +} + // InitializeLogger configures the default loggers stack levels, formatting, and the output destinations. func InitializeLogger(config schema.LogConfiguration, log bool) error { setLevelStr(config.Level, log) diff --git a/internal/logging/printf.go b/internal/logging/printf.go new file mode 100644 index 000000000..8f6cb7f98 --- /dev/null +++ b/internal/logging/printf.go @@ -0,0 +1,29 @@ +package logging + +import ( + "context" + + "github.com/sirupsen/logrus" +) + +// PrintfLogger is a logger that implements a common Printf logger. +type PrintfLogger struct { + level logrus.Level + logrus *logrus.Logger +} + +// Printf is the implementation of the interface. +func (l *PrintfLogger) Printf(format string, args ...interface{}) { + l.logrus.Logf(l.level, format, args...) +} + +// CtxPrintfLogger is a logger that implements a common Printf logger with a ctx. +type CtxPrintfLogger struct { + level logrus.Level + logrus *logrus.Logger +} + +// Printf is the implementation of the interface. +func (l *CtxPrintfLogger) Printf(_ context.Context, format string, args ...interface{}) { + l.logrus.Logf(l.level, format, args...) +} diff --git a/internal/server/server.go b/internal/server/server.go index 37dc41c86..37e6f6a79 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" + "github.com/sirupsen/logrus" "github.com/valyala/fasthttp" "github.com/authelia/authelia/v4/internal/configuration/schema" @@ -27,6 +28,7 @@ func CreateDefaultServer(config schema.Configuration, providers middlewares.Prov ReadTimeout: config.Server.Timeouts.Read, WriteTimeout: config.Server.Timeouts.Write, IdleTimeout: config.Server.Timeouts.Idle, + Logger: logging.LoggerPrintf(logrus.WarnLevel), } address := net.JoinHostPort(config.Server.Host, strconv.Itoa(config.Server.Port)) @@ -104,6 +106,7 @@ func CreateMetricsServer(config schema.TelemetryMetricsConfig) (server *fasthttp ReadTimeout: config.Timeouts.Read, WriteTimeout: config.Timeouts.Write, IdleTimeout: config.Timeouts.Idle, + Logger: logging.LoggerPrintf(logrus.DebugLevel), } logging.Logger().Infof(fmtLogServerInit, "server (metrics)", connNonTLS, listener.Addr().String(), "/metrics") diff --git a/internal/session/provider_config.go b/internal/session/provider_config.go index e02410002..0a1ba6e11 100644 --- a/internal/session/provider_config.go +++ b/internal/session/provider_config.go @@ -9,6 +9,7 @@ import ( "github.com/fasthttp/session/v2" "github.com/fasthttp/session/v2/providers/redis" + "github.com/sirupsen/logrus" "github.com/valyala/fasthttp" "github.com/authelia/authelia/v4/internal/configuration/schema" @@ -93,7 +94,7 @@ func NewProviderConfig(config schema.SessionConfiguration, certPool *x509.CertPo providerName = "redis-sentinel" redisSentinelConfig = &redis.FailoverConfig{ - Logger: &redisLogger{logger: logging.Logger()}, + Logger: logging.LoggerCtxPrintf(logrus.TraceLevel), MasterName: config.Redis.HighAvailability.SentinelName, SentinelAddrs: addrs, SentinelUsername: config.Redis.HighAvailability.SentinelUsername, @@ -123,7 +124,7 @@ func NewProviderConfig(config schema.SessionConfiguration, certPool *x509.CertPo } redisConfig = &redis.Config{ - Logger: newRedisLogger(), + Logger: logging.LoggerCtxPrintf(logrus.TraceLevel), Network: network, Addr: addr, Username: config.Redis.Username, diff --git a/internal/session/types.go b/internal/session/types.go index e4a046f89..a3adbb89b 100644 --- a/internal/session/types.go +++ b/internal/session/types.go @@ -1,16 +1,13 @@ package session import ( - "context" "time" - "github.com/fasthttp/session/v2" + session "github.com/fasthttp/session/v2" "github.com/fasthttp/session/v2/providers/redis" "github.com/go-webauthn/webauthn/webauthn" - "github.com/sirupsen/logrus" "github.com/authelia/authelia/v4/internal/authentication" - "github.com/authelia/authelia/v4/internal/logging" "github.com/authelia/authelia/v4/internal/oidc" ) @@ -55,15 +52,3 @@ type Identity struct { Email string DisplayName string } - -func newRedisLogger() *redisLogger { - return &redisLogger{logger: logging.Logger()} -} - -type redisLogger struct { - logger *logrus.Logger -} - -func (l *redisLogger) Printf(_ context.Context, format string, v ...interface{}) { - l.logger.Tracef(format, v...) -}