Add automatic log rotation for daemon (bd-154)
- Integrated lumberjack library for production-ready log rotation - Configurable via env vars: BEADS_DAEMON_LOG_MAX_SIZE, BEADS_DAEMON_LOG_MAX_BACKUPS, BEADS_DAEMON_LOG_MAX_AGE, BEADS_DAEMON_LOG_COMPRESS - Defaults: 10MB max size, 3 backups, 7 day retention, compression enabled - Added comprehensive tests for env var parsing and rotation config - Updated README.md and CHANGELOG.md with rotation documentation - Prevents unbounded log growth for long-running daemons Amp-Thread-ID: https://ampcode.com/threads/T-8232d41a-6872-4f4c-962c-7fae8f5e83b7 Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
129
cmd/bd/daemon_rotation_test.go
Normal file
129
cmd/bd/daemon_rotation_test.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLogRotation(t *testing.T) {
|
||||
|
||||
// Set small max size for testing (1 MB)
|
||||
os.Setenv("BEADS_DAEMON_LOG_MAX_SIZE", "1")
|
||||
os.Setenv("BEADS_DAEMON_LOG_MAX_BACKUPS", "2")
|
||||
os.Setenv("BEADS_DAEMON_LOG_MAX_AGE", "7")
|
||||
os.Setenv("BEADS_DAEMON_LOG_COMPRESS", "false") // disable for easier testing
|
||||
defer func() {
|
||||
os.Unsetenv("BEADS_DAEMON_LOG_MAX_SIZE")
|
||||
os.Unsetenv("BEADS_DAEMON_LOG_MAX_BACKUPS")
|
||||
os.Unsetenv("BEADS_DAEMON_LOG_MAX_AGE")
|
||||
os.Unsetenv("BEADS_DAEMON_LOG_COMPRESS")
|
||||
}()
|
||||
|
||||
// Test env parsing
|
||||
maxSize := getEnvInt("BEADS_DAEMON_LOG_MAX_SIZE", 10)
|
||||
if maxSize != 1 {
|
||||
t.Errorf("Expected max size 1, got %d", maxSize)
|
||||
}
|
||||
|
||||
maxBackups := getEnvInt("BEADS_DAEMON_LOG_MAX_BACKUPS", 3)
|
||||
if maxBackups != 2 {
|
||||
t.Errorf("Expected max backups 2, got %d", maxBackups)
|
||||
}
|
||||
|
||||
compress := getEnvBool("BEADS_DAEMON_LOG_COMPRESS", true)
|
||||
if compress {
|
||||
t.Errorf("Expected compress false, got true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnvInt(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
envValue string
|
||||
defaultValue int
|
||||
expected int
|
||||
}{
|
||||
{"not set", "", 10, 10},
|
||||
{"valid int", "42", 10, 42},
|
||||
{"invalid int", "invalid", 10, 10},
|
||||
{"zero", "0", 10, 0},
|
||||
{"negative", "-5", 10, -5},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.envValue != "" {
|
||||
os.Setenv("TEST_INT", tt.envValue)
|
||||
defer os.Unsetenv("TEST_INT")
|
||||
} else {
|
||||
os.Unsetenv("TEST_INT")
|
||||
}
|
||||
|
||||
result := getEnvInt("TEST_INT", tt.defaultValue)
|
||||
if result != tt.expected {
|
||||
t.Errorf("Expected %d, got %d", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnvBool(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
envValue string
|
||||
defaultValue bool
|
||||
expected bool
|
||||
}{
|
||||
{"not set default true", "", true, true},
|
||||
{"not set default false", "", false, false},
|
||||
{"true string", "true", false, true},
|
||||
{"1 string", "1", false, true},
|
||||
{"false string", "false", true, false},
|
||||
{"0 string", "0", true, false},
|
||||
{"invalid string", "invalid", true, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.envValue != "" {
|
||||
os.Setenv("TEST_BOOL", tt.envValue)
|
||||
defer os.Unsetenv("TEST_BOOL")
|
||||
} else {
|
||||
os.Unsetenv("TEST_BOOL")
|
||||
}
|
||||
|
||||
result := getEnvBool("TEST_BOOL", tt.defaultValue)
|
||||
if result != tt.expected {
|
||||
t.Errorf("Expected %v, got %v", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogFileRotationDefaults(t *testing.T) {
|
||||
// Test default values when no env vars set
|
||||
os.Unsetenv("BEADS_DAEMON_LOG_MAX_SIZE")
|
||||
os.Unsetenv("BEADS_DAEMON_LOG_MAX_BACKUPS")
|
||||
os.Unsetenv("BEADS_DAEMON_LOG_MAX_AGE")
|
||||
os.Unsetenv("BEADS_DAEMON_LOG_COMPRESS")
|
||||
|
||||
maxSize := getEnvInt("BEADS_DAEMON_LOG_MAX_SIZE", 10)
|
||||
if maxSize != 10 {
|
||||
t.Errorf("Expected default max size 10, got %d", maxSize)
|
||||
}
|
||||
|
||||
maxBackups := getEnvInt("BEADS_DAEMON_LOG_MAX_BACKUPS", 3)
|
||||
if maxBackups != 3 {
|
||||
t.Errorf("Expected default max backups 3, got %d", maxBackups)
|
||||
}
|
||||
|
||||
maxAge := getEnvInt("BEADS_DAEMON_LOG_MAX_AGE", 7)
|
||||
if maxAge != 7 {
|
||||
t.Errorf("Expected default max age 7, got %d", maxAge)
|
||||
}
|
||||
|
||||
compress := getEnvBool("BEADS_DAEMON_LOG_COMPRESS", true)
|
||||
if !compress {
|
||||
t.Errorf("Expected default compress true, got false")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user