diff --git a/bootstrap.go b/bootstrap.go index ea9b5d26..a5134285 100644 --- a/bootstrap.go +++ b/bootstrap.go @@ -18,7 +18,10 @@ import ( // InitLogger initializes the logger based on configuration func InitLogger(config *models.Config) models.Logger { - return internalbootstrap.InitLogger(internalbootstrap.LoggerOptions{Level: config.Logger.Level}) + return internalbootstrap.InitLogger(internalbootstrap.LoggerOptions{ + Level: config.Logger.Level, + Logger: config.Logger.Logger, + }) } // InitDatabase creates a Bun DB connection based on provider diff --git a/config/options.go b/config/options.go index e9b98f3c..d9af8128 100644 --- a/config/options.go +++ b/config/options.go @@ -158,6 +158,9 @@ func WithLogger(config models.LoggerConfig) ConfigOption { if config.Level != "" { c.Logger.Level = config.Level } + if config.Logger != nil { + c.Logger.Logger = config.Logger + } } } diff --git a/config/options_test.go b/config/options_test.go new file mode 100644 index 00000000..8c26d2e5 --- /dev/null +++ b/config/options_test.go @@ -0,0 +1,31 @@ +package config + +import ( + "testing" + + "github.com/Authula/authula/models" +) + +type customLogger struct{} + +func (l *customLogger) Debug(msg string, args ...any) {} +func (l *customLogger) Info(msg string, args ...any) {} +func (l *customLogger) Warn(msg string, args ...any) {} +func (l *customLogger) Error(msg string, args ...any) {} + +func TestWithLoggerConfiguresLevelAndCustomLogger(t *testing.T) { + logger := &customLogger{} + + config := NewConfig(WithLogger(models.LoggerConfig{ + Level: "debug", + Logger: logger, + })) + + if config.Logger.Level != "debug" { + t.Fatalf("expected logger level debug, got %q", config.Logger.Level) + } + + if config.Logger.Logger != logger { + t.Fatal("expected custom logger to be configured") + } +} diff --git a/internal/bootstrap/logger.go b/internal/bootstrap/logger.go index c93ad6be..a8171f05 100644 --- a/internal/bootstrap/logger.go +++ b/internal/bootstrap/logger.go @@ -12,11 +12,16 @@ import ( // LoggerOptions configures logger initialization type LoggerOptions struct { - Level string + Level string + Logger models.Logger } // InitLogger creates a configured logger instance func InitLogger(opts LoggerOptions) models.Logger { + if opts.Logger != nil { + return opts.Logger + } + environment := os.Getenv(env.EnvGoEnvironment) var logger *slog.Logger diff --git a/internal/bootstrap/logger_test.go b/internal/bootstrap/logger_test.go new file mode 100644 index 00000000..2ff804a1 --- /dev/null +++ b/internal/bootstrap/logger_test.go @@ -0,0 +1,18 @@ +package bootstrap + +import "testing" + +type customLogger struct{} + +func (l *customLogger) Debug(msg string, args ...any) {} +func (l *customLogger) Info(msg string, args ...any) {} +func (l *customLogger) Warn(msg string, args ...any) {} +func (l *customLogger) Error(msg string, args ...any) {} + +func TestInitLoggerUsesConfiguredLogger(t *testing.T) { + logger := &customLogger{} + + if got := InitLogger(LoggerOptions{Level: "debug", Logger: logger}); got != logger { + t.Fatal("expected configured logger to be used") + } +} diff --git a/models/config.go b/models/config.go index 1c826219..7e6c5d00 100644 --- a/models/config.go +++ b/models/config.go @@ -43,7 +43,8 @@ type DatabaseConfig struct { } type LoggerConfig struct { - Level string `json:"level" toml:"level"` + Level string `json:"level" toml:"level"` + Logger Logger `json:"-" toml:"-"` } type SessionConfig struct {