e1.1.0 Refactoring default config and unused functions, global auto-initiated logger instance removed

This commit is contained in:
2025-04-23 01:08:14 -04:00
parent c809396455
commit 0ddfa2c533
10 changed files with 260 additions and 428 deletions

View File

@ -12,8 +12,10 @@ import (
func main() {
var count atomic.Int64
logger := log.NewLogger()
// Initialize the logger with defaults first
err := log.InitWithDefaults()
err := logger.InitWithDefaults()
if err != nil {
fmt.Printf("Initial Init error: %v\n", err)
return
@ -22,7 +24,7 @@ func main() {
// Log something constantly
go func() {
for i := 0; ; i++ {
log.Info("Test log", i)
logger.Info("Test log", i)
count.Add(1)
time.Sleep(time.Millisecond)
}
@ -32,7 +34,7 @@ func main() {
for i := 0; i < 10; i++ {
// Use different buffer sizes to trigger channel recreation
bufSize := fmt.Sprintf("buffer_size=%d", 100*(i+1))
err := log.InitWithDefaults(bufSize)
err := logger.InitWithDefaults(bufSize)
if err != nil {
fmt.Printf("Init error: %v\n", err)
}
@ -42,14 +44,14 @@ func main() {
// Check if we see any inconsistency
time.Sleep(500 * time.Millisecond)
fmt.Printf("Total logs attempted: %d\n", count.Load())
fmt.Printf("Total logger. attempted: %d\n", count.Load())
// Gracefully shut down the logger
err = log.Shutdown(time.Second)
// Gracefully shut down the logger.er
err = logger.Shutdown(time.Second)
if err != nil {
fmt.Printf("Shutdown error: %v\n", err)
}
// Check for any error messages in the log files
// or dropped log count
// Check for any error messages in the logger.files
// or dropped logger.count
}

View File

@ -58,16 +58,17 @@ func main() {
}
// --- Initialize Logger ---
logger := log.NewLogger()
// Pass the config instance and the base path for logger settings
err = log.Init(cfg, configBasePath)
err = logger.Init(cfg, configBasePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize logger: %v\n", err)
fmt.Fprintf(os.Stderr, "Failed to initialize logger.er: %v\n", err)
os.Exit(1)
}
fmt.Println("Logger initialized.")
// --- SAVE CONFIGURATION ---
// Save the config state *after* log.Init has registered its keys/defaults
// Save the config state *after* logger.Init has registered its keys/defaults
// This will write the merged configuration (defaults + file overrides) back.
err = cfg.Save(configFile)
if err != nil {
@ -78,10 +79,10 @@ func main() {
// --- End Save Configuration ---
// --- Logging ---
log.Debug("This is a debug message.", "user_id", 123)
log.Info("Application starting...")
log.Warn("Potential issue detected.", "threshold", 0.95)
log.Error("An error occurred!", "code", 500)
logger.Debug("This is a debug message.", "user_id", 123)
logger.Info("Application starting...")
logger.Warn("Potential issue detected.", "threshold", 0.95)
logger.Error("An error occurred!", "code", 500)
// Logging from goroutines
var wg sync.WaitGroup
@ -89,21 +90,21 @@ func main() {
wg.Add(1)
go func(id int) {
defer wg.Done()
log.Info("Goroutine started", "id", id)
logger.Info("Goroutine started", "id", id)
time.Sleep(time.Duration(50+id*50) * time.Millisecond)
log.InfoTrace(1, "Goroutine finished", "id", id) // Log with trace
logger.InfoTrace(1, "Goroutine finished", "id", id) // Log with trace
}(i)
}
// Wait for goroutines to finish before shutting down logger
// Wait for goroutines to finish before shutting down logger.er
wg.Wait()
fmt.Println("Goroutines finished.")
// --- Shutdown Logger ---
fmt.Println("Shutting down logger...")
// Provide a reasonable timeout for logs to flush
fmt.Println("Shutting down logger.er...")
// Provide a reasonable timeout for logger. to flush
shutdownTimeout := 2 * time.Second
err = log.Shutdown(shutdownTimeout)
err = logger.Shutdown(shutdownTimeout)
if err != nil {
fmt.Fprintf(os.Stderr, "Logger shutdown error: %v\n", err)
} else {

View File

@ -53,6 +53,8 @@ var levels = []int64{
log.LevelError,
}
var logger *log.Logger
func generateRandomMessage(size int) string {
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
var sb strings.Builder
@ -78,13 +80,13 @@ func logBurst(burstID int) {
}
switch level {
case log.LevelDebug:
log.Debug(args...)
logger.Debug(args...)
case log.LevelInfo:
log.Info(args...)
logger.Info(args...)
case log.LevelWarn:
log.Warn(args...)
logger.Warn(args...)
case log.LevelError:
log.Error(args...)
logger.Error(args...)
}
}
}
@ -126,7 +128,8 @@ func main() {
}
// --- Initialize Logger ---
err = log.Init(cfg, configBasePath)
logger = log.NewLogger()
err = logger.Init(cfg, configBasePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize logger: %v\n", err)
os.Exit(1)
@ -195,7 +198,7 @@ endLoop:
// --- Shutdown Logger ---
fmt.Println("Shutting down logger (allowing up to 10s)...")
shutdownTimeout := 10 * time.Second
err = log.Shutdown(shutdownTimeout)
err = logger.Shutdown(shutdownTimeout)
if err != nil {
fmt.Fprintf(os.Stderr, "Logger shutdown error: %v\n", err)
} else {