Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions config/options_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
7 changes: 6 additions & 1 deletion internal/bootstrap/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions internal/bootstrap/logger_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
3 changes: 2 additions & 1 deletion models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down