refactor(logging): move logging setup to internal/logging
All checks were successful
CI / Lint (pull_request) Successful in 5s
CI / Test (pull_request) Successful in 7s
CI / Build (pull_request) Successful in 7s
CI / Docker Build (pull_request) Successful in 21s

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:
Gemini CLI
2026-01-15 18:57:01 +00:00
parent ec689c6152
commit 4807edd23a
6 changed files with 101 additions and 58 deletions

View File

@@ -11,6 +11,7 @@ import (
"time"
"github.com/placeholder/golang-template/internal/config"
"github.com/placeholder/golang-template/internal/logging"
)
func main() {
@@ -27,11 +28,9 @@ func main() {
}
// Setup Logger
opts := &slog.HandlerOptions{
Level: config.ParseLogLevel(cfg.LogLevel),
}
logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
slog.SetDefault(logger)
logging.Configure(logging.Config{
Level: cfg.Logging.Level,
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

View File

@@ -1,7 +1,12 @@
# Application Configuration
# This file contains the default configuration for the application.
# Values can be overridden by environment variables with the prefix "APP_".
# Example: APP_LOG_LEVEL=debug
# Example: APP_LOGGING_LEVEL=debug
# Log Level: debug, info, warn, error
log_level: "info"
logging:
# Log Level: debug, info, warn, error
level: "info"
server:
host: "localhost"
port: 8080

View File

@@ -2,7 +2,6 @@ package config
import (
"fmt"
"log/slog"
"os"
"reflect"
"strconv"
@@ -14,8 +13,13 @@ import (
// Config holds the application configuration.
type Config struct {
LogLevel string `yaml:"log_level"`
Server ServerConfig `yaml:"server"`
Logging LoggingConfig `yaml:"logging"`
Server ServerConfig `yaml:"server"`
}
// LoggingConfig holds logging-specific configuration.
type LoggingConfig struct {
Level string `yaml:"level"`
}
// ServerConfig holds server-specific configuration.
@@ -50,22 +54,6 @@ func Load(configPath string, envPrefix string) (*Config, error) {
return cfg, nil
}
// ParseLogLevel converts a string log level to a slog.Level.
func ParseLogLevel(level string) slog.Level {
switch strings.ToLower(level) {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}
// overrideFromEnv uses reflection to traverse the struct and update fields from env vars.
func overrideFromEnv(cfg interface{}, prefix string) error {
v := reflect.ValueOf(cfg)

View File

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

View File

@@ -0,0 +1,37 @@
package logging
import (
"log/slog"
"os"
"strings"
)
// Config holds the logging configuration.
type Config struct {
Level string `yaml:"level"`
}
// Configure sets up the global logger based on the provided configuration.
func Configure(cfg Config) *slog.Logger {
opts := &slog.HandlerOptions{
Level: parseLogLevel(cfg.Level),
}
logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
slog.SetDefault(logger)
return logger
}
func parseLogLevel(level string) slog.Level {
switch strings.ToLower(level) {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}

View File

@@ -0,0 +1,37 @@
package logging
import (
"log/slog"
"testing"
)
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)
}
})
}
}
func TestConfigure(t *testing.T) {
cfg := Config{Level: "debug"}
logger := Configure(cfg)
if logger == nil {
t.Error("Configure() returned nil")
}
}