refactor(logging): implement common interfaces (#3994)

This implements and leverages some common library logging interfaces.
pull/3993/head
James Elliott 2022-09-10 18:02:57 +10:00 committed by GitHub
parent 4e88698984
commit d7fd9ca506
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 18 deletions

View File

@ -17,6 +17,22 @@ func Logger() *logrus.Logger {
return logrus.StandardLogger() 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. // InitializeLogger configures the default loggers stack levels, formatting, and the output destinations.
func InitializeLogger(config schema.LogConfiguration, log bool) error { func InitializeLogger(config schema.LogConfiguration, log bool) error {
setLevelStr(config.Level, log) setLevelStr(config.Level, log)

View File

@ -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...)
}

View File

@ -9,6 +9,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/sirupsen/logrus"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"github.com/authelia/authelia/v4/internal/configuration/schema" "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, ReadTimeout: config.Server.Timeouts.Read,
WriteTimeout: config.Server.Timeouts.Write, WriteTimeout: config.Server.Timeouts.Write,
IdleTimeout: config.Server.Timeouts.Idle, IdleTimeout: config.Server.Timeouts.Idle,
Logger: logging.LoggerPrintf(logrus.WarnLevel),
} }
address := net.JoinHostPort(config.Server.Host, strconv.Itoa(config.Server.Port)) 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, ReadTimeout: config.Timeouts.Read,
WriteTimeout: config.Timeouts.Write, WriteTimeout: config.Timeouts.Write,
IdleTimeout: config.Timeouts.Idle, IdleTimeout: config.Timeouts.Idle,
Logger: logging.LoggerPrintf(logrus.DebugLevel),
} }
logging.Logger().Infof(fmtLogServerInit, "server (metrics)", connNonTLS, listener.Addr().String(), "/metrics") logging.Logger().Infof(fmtLogServerInit, "server (metrics)", connNonTLS, listener.Addr().String(), "/metrics")

View File

@ -9,6 +9,7 @@ import (
"github.com/fasthttp/session/v2" "github.com/fasthttp/session/v2"
"github.com/fasthttp/session/v2/providers/redis" "github.com/fasthttp/session/v2/providers/redis"
"github.com/sirupsen/logrus"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"github.com/authelia/authelia/v4/internal/configuration/schema" "github.com/authelia/authelia/v4/internal/configuration/schema"
@ -93,7 +94,7 @@ func NewProviderConfig(config schema.SessionConfiguration, certPool *x509.CertPo
providerName = "redis-sentinel" providerName = "redis-sentinel"
redisSentinelConfig = &redis.FailoverConfig{ redisSentinelConfig = &redis.FailoverConfig{
Logger: &redisLogger{logger: logging.Logger()}, Logger: logging.LoggerCtxPrintf(logrus.TraceLevel),
MasterName: config.Redis.HighAvailability.SentinelName, MasterName: config.Redis.HighAvailability.SentinelName,
SentinelAddrs: addrs, SentinelAddrs: addrs,
SentinelUsername: config.Redis.HighAvailability.SentinelUsername, SentinelUsername: config.Redis.HighAvailability.SentinelUsername,
@ -123,7 +124,7 @@ func NewProviderConfig(config schema.SessionConfiguration, certPool *x509.CertPo
} }
redisConfig = &redis.Config{ redisConfig = &redis.Config{
Logger: newRedisLogger(), Logger: logging.LoggerCtxPrintf(logrus.TraceLevel),
Network: network, Network: network,
Addr: addr, Addr: addr,
Username: config.Redis.Username, Username: config.Redis.Username,

View File

@ -1,16 +1,13 @@
package session package session
import ( import (
"context"
"time" "time"
"github.com/fasthttp/session/v2" session "github.com/fasthttp/session/v2"
"github.com/fasthttp/session/v2/providers/redis" "github.com/fasthttp/session/v2/providers/redis"
"github.com/go-webauthn/webauthn/webauthn" "github.com/go-webauthn/webauthn/webauthn"
"github.com/sirupsen/logrus"
"github.com/authelia/authelia/v4/internal/authentication" "github.com/authelia/authelia/v4/internal/authentication"
"github.com/authelia/authelia/v4/internal/logging"
"github.com/authelia/authelia/v4/internal/oidc" "github.com/authelia/authelia/v4/internal/oidc"
) )
@ -55,15 +52,3 @@ type Identity struct {
Email string Email string
DisplayName 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...)
}