🔧 refactor: move log output setup to config package and change server logs to Trace level
This commit is contained in:
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"DanceLessonsCoach/pkg/config"
|
||||
"DanceLessonsCoach/pkg/server"
|
||||
@@ -16,9 +15,6 @@ func main() {
|
||||
log.Fatal().Err(err).Msg("Failed to load configuration")
|
||||
}
|
||||
|
||||
// Setup log output based on configuration
|
||||
setupLogOutput(cfg)
|
||||
|
||||
// Create readiness context to control readiness state
|
||||
readyCtx, readyCancel := context.WithCancel(context.Background())
|
||||
defer readyCancel()
|
||||
@@ -29,23 +25,3 @@ func main() {
|
||||
log.Fatal().Err(err).Msg("Server failed")
|
||||
}
|
||||
}
|
||||
|
||||
// setupLogOutput configures the log output based on configuration
|
||||
func setupLogOutput(cfg *config.Config) {
|
||||
output := cfg.GetLogOutput()
|
||||
if output == "" {
|
||||
// Use stderr by default
|
||||
return
|
||||
}
|
||||
|
||||
// Open the log file
|
||||
file, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("output", output).Msg("Failed to open log file, using stderr")
|
||||
return
|
||||
}
|
||||
|
||||
// Set the log output
|
||||
log.Logger = log.Output(file)
|
||||
log.Info().Str("output", output).Msg("Logging to file")
|
||||
}
|
||||
|
||||
@@ -200,6 +200,9 @@ func (c *Config) SetupLogging() {
|
||||
// Parse log level
|
||||
level := parseLogLevel(c.GetLogLevel())
|
||||
zerolog.SetGlobalLevel(level)
|
||||
|
||||
// Setup log output
|
||||
c.setupLogOutput()
|
||||
|
||||
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
||||
}
|
||||
@@ -226,3 +229,23 @@ func parseLogLevel(level string) zerolog.Level {
|
||||
return zerolog.TraceLevel
|
||||
}
|
||||
}
|
||||
|
||||
// setupLogOutput configures the log output based on configuration
|
||||
func (c *Config) setupLogOutput() {
|
||||
output := c.GetLogOutput()
|
||||
if output == "" {
|
||||
// Use stderr by default
|
||||
return
|
||||
}
|
||||
|
||||
// Open the log file
|
||||
file, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("output", output).Msg("Failed to open log file, using stderr")
|
||||
return
|
||||
}
|
||||
|
||||
// Set the log output
|
||||
log.Logger = log.Output(file)
|
||||
log.Trace().Str("output", output).Msg("Logging to file")
|
||||
}
|
||||
|
||||
@@ -83,20 +83,20 @@ func (s *Server) getAllMiddlewares() []func(http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||
log.Info().Msg("Health check requested")
|
||||
log.Trace().Msg("Health check requested")
|
||||
w.Write([]byte(`{"status":"healthy"}`))
|
||||
}
|
||||
|
||||
func (s *Server) handleReadiness(w http.ResponseWriter, r *http.Request) {
|
||||
log.Info().Msg("Readiness check requested")
|
||||
log.Trace().Msg("Readiness check requested")
|
||||
|
||||
select {
|
||||
case <-s.readyCtx.Done():
|
||||
log.Info().Msg("Readiness check: not ready (shutting down)")
|
||||
log.Trace().Msg("Readiness check: not ready (shutting down)")
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
w.Write([]byte(`{"ready":false}`))
|
||||
default:
|
||||
log.Info().Msg("Readiness check: ready")
|
||||
log.Trace().Msg("Readiness check: ready")
|
||||
w.Write([]byte(`{"ready":true}`))
|
||||
}
|
||||
}
|
||||
@@ -110,7 +110,7 @@ func (s *Server) Run() error {
|
||||
// Initialize OpenTelemetry if enabled
|
||||
var err error
|
||||
if s.withOTEL {
|
||||
log.Info().Msg("Initializing OpenTelemetry tracing")
|
||||
log.Trace().Msg("Initializing OpenTelemetry tracing")
|
||||
|
||||
telemetrySetup := &telemetry.Setup{
|
||||
ServiceName: s.config.GetServiceName(),
|
||||
@@ -124,7 +124,7 @@ func (s *Server) Run() error {
|
||||
log.Error().Err(err).Msg("Failed to initialize OpenTelemetry, continuing without tracing")
|
||||
s.withOTEL = false
|
||||
} else {
|
||||
log.Info().Msg("OpenTelemetry tracing initialized successfully")
|
||||
log.Trace().Msg("OpenTelemetry tracing initialized successfully")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ func (s *Server) Run() error {
|
||||
defer stopOngoingGracefully()
|
||||
|
||||
// Create HTTP server
|
||||
log.Info().Str("address", s.config.GetServerAddress()).Msg("Server running")
|
||||
log.Trace().Str("address", s.config.GetServerAddress()).Msg("Server running")
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: s.config.GetServerAddress(),
|
||||
@@ -159,17 +159,17 @@ func (s *Server) Run() error {
|
||||
// Wait for signal
|
||||
<-rootCtx.Done()
|
||||
stop()
|
||||
log.Info().Msg("Shutdown signal received")
|
||||
log.Trace().Msg("Shutdown signal received")
|
||||
|
||||
// Cancel readiness context to stop accepting new requests
|
||||
if cancelReady, ok := s.readyCtx.(interface{ Cancel() }); ok {
|
||||
cancelReady.Cancel()
|
||||
}
|
||||
log.Info().Msg("Readiness set to false, no longer accepting new requests")
|
||||
log.Trace().Msg("Readiness set to false, no longer accepting new requests")
|
||||
|
||||
// Give time for readiness check to propagate
|
||||
time.Sleep(1 * time.Second)
|
||||
log.Info().Msg("Readiness check propagated, now waiting for ongoing requests to finish.")
|
||||
log.Trace().Msg("Readiness check propagated, now waiting for ongoing requests to finish.")
|
||||
|
||||
// Create shutdown context with timeout from config
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), s.config.Shutdown.Timeout)
|
||||
@@ -179,14 +179,14 @@ func (s *Server) Run() error {
|
||||
log.Error().Err(err).Msg("Server shutdown failed")
|
||||
return err
|
||||
}
|
||||
log.Info().Msg("Server shutdown complete")
|
||||
log.Trace().Msg("Server shutdown complete")
|
||||
|
||||
// Shutdown OpenTelemetry tracer provider
|
||||
if s.tracerProvider != nil {
|
||||
if err := telemetry.Shutdown(context.Background(), s.tracerProvider); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to shutdown OpenTelemetry tracer provider")
|
||||
} else {
|
||||
log.Info().Msg("OpenTelemetry tracer provider shutdown complete")
|
||||
log.Trace().Msg("OpenTelemetry tracer provider shutdown complete")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user