refactor(logging): move logging setup to internal/logging
Moves logging configuration and setup to a dedicated package. Updates Config struct to use nested LoggingConfig. Updates main.go to use the new logging package.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -13,7 +12,8 @@ func TestLoad(t *testing.T) {
|
||||
configFile := filepath.Join(tmpDir, "config.yaml")
|
||||
|
||||
yamlContent := []byte(`
|
||||
log_level: "info"
|
||||
logging:
|
||||
level: "info"
|
||||
server:
|
||||
host: "localhost"
|
||||
port: 8080
|
||||
@@ -27,8 +27,8 @@ server:
|
||||
if err != nil {
|
||||
t.Fatalf("Load() error = %v", err)
|
||||
}
|
||||
if cfg.LogLevel != "info" {
|
||||
t.Errorf("expected LogLevel 'info', got '%s'", cfg.LogLevel)
|
||||
if cfg.Logging.Level != "info" {
|
||||
t.Errorf("expected Logging.Level 'info', got '%s'", cfg.Logging.Level)
|
||||
}
|
||||
if cfg.Server.Host != "localhost" {
|
||||
t.Errorf("expected Server.Host 'localhost', got '%s'", cfg.Server.Host)
|
||||
@@ -39,11 +39,11 @@ server:
|
||||
})
|
||||
|
||||
t.Run("Override from Env", func(t *testing.T) {
|
||||
os.Setenv("TEST_LOG_LEVEL", "debug")
|
||||
os.Setenv("TEST_LOGGING_LEVEL", "debug")
|
||||
os.Setenv("TEST_SERVER_PORT", "9090")
|
||||
os.Setenv("TEST_SERVER_HOST", "0.0.0.0")
|
||||
defer func() {
|
||||
os.Unsetenv("TEST_LOG_LEVEL")
|
||||
os.Unsetenv("TEST_LOGGING_LEVEL")
|
||||
os.Unsetenv("TEST_SERVER_PORT")
|
||||
os.Unsetenv("TEST_SERVER_HOST")
|
||||
}()
|
||||
@@ -52,8 +52,8 @@ server:
|
||||
if err != nil {
|
||||
t.Fatalf("Load() error = %v", err)
|
||||
}
|
||||
if cfg.LogLevel != "debug" {
|
||||
t.Errorf("expected LogLevel 'debug', got '%s'", cfg.LogLevel)
|
||||
if cfg.Logging.Level != "debug" {
|
||||
t.Errorf("expected Logging.Level 'debug', got '%s'", cfg.Logging.Level)
|
||||
}
|
||||
if cfg.Server.Port != 9090 {
|
||||
t.Errorf("expected Server.Port 9090, got %d", cfg.Server.Port)
|
||||
@@ -63,26 +63,3 @@ server:
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseLogLevel(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected slog.Level
|
||||
}{
|
||||
{"debug", slog.LevelDebug},
|
||||
{"DEBUG", slog.LevelDebug},
|
||||
{"info", slog.LevelInfo},
|
||||
{"warn", slog.LevelWarn},
|
||||
{"error", slog.LevelError},
|
||||
{"unknown", slog.LevelInfo},
|
||||
{"", slog.LevelInfo},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
if got := ParseLogLevel(tt.input); got != tt.expected {
|
||||
t.Errorf("ParseLogLevel(%q) = %v, want %v", tt.input, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user