Skip to content

Commit

Permalink
refactor(logging): implement common interfaces (authelia#3994)
Browse files Browse the repository at this point in the history
This implements and leverages some common library logging interfaces.
  • Loading branch information
james-d-elliott authored Sep 10, 2022
1 parent 4e88698 commit d7fd9ca
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
16 changes: 16 additions & 0 deletions internal/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions internal/logging/printf.go
Original file line number Diff line number Diff line change
@@ -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...)
}
3 changes: 3 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"

"github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"

"github.com/authelia/authelia/v4/internal/configuration/schema"
Expand All @@ -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))
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions internal/session/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 1 addition & 16 deletions internal/session/types.go
Original file line number Diff line number Diff line change
@@ -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"
)

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

0 comments on commit d7fd9ca

Please sign in to comment.