Enhance build system and logging configuration
- Add scripts/build.sh to compile binaries into bin/ directory - Move all zerolog setup logic from cmd/server/main.go to pkg/config - Add log level configuration support (trace, debug, info, warn, error, fatal, panic) - Simplify cmd/server/main.go from 57 to 27 lines (53% reduction) - Update .gitignore to use bin/ directory instead of individual files - Document build process and bin directory in AGENTS.md - Maintain backward compatibility with all existing functionality
This commit is contained in:
@@ -3,8 +3,10 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -30,7 +32,8 @@ type ShutdownConfig struct {
|
||||
|
||||
// LoggingConfig holds logging-related configuration
|
||||
type LoggingConfig struct {
|
||||
JSON bool `mapstructure:"json"`
|
||||
JSON bool `mapstructure:"json"`
|
||||
Level string `mapstructure:"level"`
|
||||
}
|
||||
|
||||
// TelemetryConfig holds OpenTelemetry-related configuration
|
||||
@@ -53,12 +56,17 @@ type SamplerConfig struct {
|
||||
// To specify a custom config file path, set DLC_CONFIG_FILE environment variable
|
||||
func LoadConfig() (*Config, error) {
|
||||
v := viper.New()
|
||||
|
||||
// Set up initial console logging for config loading messages
|
||||
consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr}
|
||||
log.Logger = log.Output(consoleWriter)
|
||||
|
||||
// Set default values
|
||||
v.SetDefault("server.host", "0.0.0.0")
|
||||
v.SetDefault("server.port", 8080)
|
||||
v.SetDefault("shutdown.timeout", 30*time.Second)
|
||||
v.SetDefault("logging.json", false)
|
||||
v.SetDefault("logging.level", "trace")
|
||||
|
||||
// Telemetry defaults
|
||||
v.SetDefault("telemetry.enabled", false)
|
||||
@@ -97,6 +105,7 @@ func LoadConfig() (*Config, error) {
|
||||
v.BindEnv("server.port", "DLC_SERVER_PORT")
|
||||
v.BindEnv("shutdown.timeout", "DLC_SHUTDOWN_TIMEOUT")
|
||||
v.BindEnv("logging.json", "DLC_LOGGING_JSON")
|
||||
v.BindEnv("logging.level", "DLC_LOGGING_LEVEL")
|
||||
|
||||
// Telemetry environment variables
|
||||
v.BindEnv("telemetry.enabled", "DLC_TELEMETRY_ENABLED")
|
||||
@@ -113,11 +122,23 @@ func LoadConfig() (*Config, error) {
|
||||
return nil, fmt.Errorf("config unmarshal error: %w", err)
|
||||
}
|
||||
|
||||
// Configure log output format (JSON or console) first
|
||||
if config.Logging.JSON {
|
||||
log.Logger = log.Output(os.Stderr)
|
||||
} else {
|
||||
consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr}
|
||||
log.Logger = log.Output(consoleWriter)
|
||||
}
|
||||
|
||||
// Setup logging based on configuration
|
||||
config.SetupLogging()
|
||||
|
||||
log.Info().
|
||||
Str("host", config.Server.Host).
|
||||
Int("port", config.Server.Port).
|
||||
Dur("shutdown_timeout", config.Shutdown.Timeout).
|
||||
Bool("logging_json", config.Logging.JSON).
|
||||
Str("logging_level", config.Logging.Level).
|
||||
Bool("telemetry_enabled", config.Telemetry.Enabled).
|
||||
Str("telemetry_service", config.Telemetry.ServiceName).
|
||||
Msg("Configuration loaded")
|
||||
@@ -159,3 +180,40 @@ func (c *Config) GetSamplerType() string {
|
||||
func (c *Config) GetSamplerRatio() float64 {
|
||||
return c.Telemetry.Sampler.Ratio
|
||||
}
|
||||
|
||||
// GetLogLevel returns the logging level
|
||||
func (c *Config) GetLogLevel() string {
|
||||
return c.Logging.Level
|
||||
}
|
||||
|
||||
// SetupLogging configures zerolog based on the configuration
|
||||
func (c *Config) SetupLogging() {
|
||||
// Parse log level
|
||||
level := parseLogLevel(c.GetLogLevel())
|
||||
zerolog.SetGlobalLevel(level)
|
||||
|
||||
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
||||
}
|
||||
|
||||
// parseLogLevel converts a string log level to zerolog.Level
|
||||
func parseLogLevel(level string) zerolog.Level {
|
||||
switch strings.ToLower(level) {
|
||||
case "trace":
|
||||
return zerolog.TraceLevel
|
||||
case "debug":
|
||||
return zerolog.DebugLevel
|
||||
case "info":
|
||||
return zerolog.InfoLevel
|
||||
case "warn", "warning":
|
||||
return zerolog.WarnLevel
|
||||
case "error":
|
||||
return zerolog.ErrorLevel
|
||||
case "fatal":
|
||||
return zerolog.FatalLevel
|
||||
case "panic":
|
||||
return zerolog.PanicLevel
|
||||
default:
|
||||
log.Warn().Str("level", level).Msg("Unknown log level, defaulting to trace")
|
||||
return zerolog.TraceLevel
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user