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:
@@ -11,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/placeholder/golang-template/internal/config"
|
"github.com/placeholder/golang-template/internal/config"
|
||||||
|
"github.com/placeholder/golang-template/internal/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -27,11 +28,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Setup Logger
|
// Setup Logger
|
||||||
opts := &slog.HandlerOptions{
|
logging.Configure(logging.Config{
|
||||||
Level: config.ParseLogLevel(cfg.LogLevel),
|
Level: cfg.Logging.Level,
|
||||||
}
|
})
|
||||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
|
|
||||||
slog.SetDefault(logger)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
# Application Configuration
|
# Application Configuration
|
||||||
# This file contains the default configuration for the application.
|
# This file contains the default configuration for the application.
|
||||||
# Values can be overridden by environment variables with the prefix "APP_".
|
# 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
|
logging:
|
||||||
log_level: "info"
|
# Log Level: debug, info, warn, error
|
||||||
|
level: "info"
|
||||||
|
|
||||||
|
server:
|
||||||
|
host: "localhost"
|
||||||
|
port: 8080
|
||||||
@@ -2,7 +2,6 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -14,10 +13,15 @@ import (
|
|||||||
|
|
||||||
// Config holds the application configuration.
|
// Config holds the application configuration.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
LogLevel string `yaml:"log_level"`
|
Logging LoggingConfig `yaml:"logging"`
|
||||||
Server ServerConfig `yaml:"server"`
|
Server ServerConfig `yaml:"server"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoggingConfig holds logging-specific configuration.
|
||||||
|
type LoggingConfig struct {
|
||||||
|
Level string `yaml:"level"`
|
||||||
|
}
|
||||||
|
|
||||||
// ServerConfig holds server-specific configuration.
|
// ServerConfig holds server-specific configuration.
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
@@ -50,22 +54,6 @@ func Load(configPath string, envPrefix string) (*Config, error) {
|
|||||||
return cfg, nil
|
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.
|
// overrideFromEnv uses reflection to traverse the struct and update fields from env vars.
|
||||||
func overrideFromEnv(cfg interface{}, prefix string) error {
|
func overrideFromEnv(cfg interface{}, prefix string) error {
|
||||||
v := reflect.ValueOf(cfg)
|
v := reflect.ValueOf(cfg)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -13,7 +12,8 @@ func TestLoad(t *testing.T) {
|
|||||||
configFile := filepath.Join(tmpDir, "config.yaml")
|
configFile := filepath.Join(tmpDir, "config.yaml")
|
||||||
|
|
||||||
yamlContent := []byte(`
|
yamlContent := []byte(`
|
||||||
log_level: "info"
|
logging:
|
||||||
|
level: "info"
|
||||||
server:
|
server:
|
||||||
host: "localhost"
|
host: "localhost"
|
||||||
port: 8080
|
port: 8080
|
||||||
@@ -27,8 +27,8 @@ server:
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Load() error = %v", err)
|
t.Fatalf("Load() error = %v", err)
|
||||||
}
|
}
|
||||||
if cfg.LogLevel != "info" {
|
if cfg.Logging.Level != "info" {
|
||||||
t.Errorf("expected LogLevel 'info', got '%s'", cfg.LogLevel)
|
t.Errorf("expected Logging.Level 'info', got '%s'", cfg.Logging.Level)
|
||||||
}
|
}
|
||||||
if cfg.Server.Host != "localhost" {
|
if cfg.Server.Host != "localhost" {
|
||||||
t.Errorf("expected Server.Host 'localhost', got '%s'", cfg.Server.Host)
|
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) {
|
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_PORT", "9090")
|
||||||
os.Setenv("TEST_SERVER_HOST", "0.0.0.0")
|
os.Setenv("TEST_SERVER_HOST", "0.0.0.0")
|
||||||
defer func() {
|
defer func() {
|
||||||
os.Unsetenv("TEST_LOG_LEVEL")
|
os.Unsetenv("TEST_LOGGING_LEVEL")
|
||||||
os.Unsetenv("TEST_SERVER_PORT")
|
os.Unsetenv("TEST_SERVER_PORT")
|
||||||
os.Unsetenv("TEST_SERVER_HOST")
|
os.Unsetenv("TEST_SERVER_HOST")
|
||||||
}()
|
}()
|
||||||
@@ -52,8 +52,8 @@ server:
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Load() error = %v", err)
|
t.Fatalf("Load() error = %v", err)
|
||||||
}
|
}
|
||||||
if cfg.LogLevel != "debug" {
|
if cfg.Logging.Level != "debug" {
|
||||||
t.Errorf("expected LogLevel 'debug', got '%s'", cfg.LogLevel)
|
t.Errorf("expected Logging.Level 'debug', got '%s'", cfg.Logging.Level)
|
||||||
}
|
}
|
||||||
if cfg.Server.Port != 9090 {
|
if cfg.Server.Port != 9090 {
|
||||||
t.Errorf("expected Server.Port 9090, got %d", cfg.Server.Port)
|
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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
37
internal/logging/logging.go
Normal file
37
internal/logging/logging.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
37
internal/logging/logging_test.go
Normal file
37
internal/logging/logging_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user