Simplify logging config

This commit is contained in:
2025-12-12 18:13:18 +01:00
parent e2a49402d6
commit 0b3d654945
7 changed files with 75 additions and 68 deletions

View File

@@ -63,8 +63,8 @@ func main() {
}
// Create logs directory
if err := os.MkdirAll(cfg.Instances.Logging.LogsDir, 0755); err != nil {
log.Printf("Error creating log directory %s: %v\nInstance logs will not be available.", cfg.Instances.Logging.LogsDir, err)
if err := os.MkdirAll(cfg.Instances.LogsDir, 0755); err != nil {
log.Printf("Error creating log directory %s: %v\nInstance logs will not be available.", cfg.Instances.LogsDir, err)
}
}

View File

@@ -88,20 +88,10 @@ type DatabaseConfig struct {
ConnMaxLifetime time.Duration `yaml:"connection_max_lifetime" json:"connection_max_lifetime" swaggertype:"string" example:"1h"`
}
// LoggingConfig contains all logging-related configuration for instances
type LoggingConfig struct {
// Logs directory override (relative to data_dir if not absolute)
LogsDir string `yaml:"logs_dir" json:"logs_dir"`
// Log rotation configuration
LogRotation LogRotationConfig `yaml:"log_rotation" json:"log_rotation"`
}
// LogRotationConfig contains log rotation settings for instances
type LogRotationConfig struct {
Enabled bool `yaml:"enabled" default:"true"`
MaxSizeMB int `yaml:"max_size_mb" default:"100"` // MB
MaxBackups int `yaml:"max_backups" default:"3"`
Compress bool `yaml:"compress" default:"false"`
}
@@ -143,8 +133,17 @@ type InstancesConfig struct {
// Interval for checking instance timeouts (in minutes)
TimeoutCheckInterval int `yaml:"timeout_check_interval" json:"timeout_check_interval"`
// Logging configuration
Logging LoggingConfig `yaml:"logging" json:"logging"`
// Logs directory override (relative to data_dir if not absolute)
LogsDir string `yaml:"logs_dir" json:"logs_dir"`
// Log rotation enabled
LogRotationEnabled bool `yaml:"log_rotation_enabled" default:"true"`
// Maximum log file size in MB before rotation
LogRotationMaxSizeMB int `yaml:"log_rotation_max_size_mb" default:"100"`
// Whether to compress rotated log files
LogRotationCompress bool `yaml:"log_rotation_compress" default:"false"`
}
// AuthConfig contains authentication settings
@@ -235,15 +234,10 @@ func LoadConfig(configPath string) (AppConfig, error) {
DefaultOnDemandStart: true,
OnDemandStartTimeout: 120, // 2 minutes
TimeoutCheckInterval: 5, // Check timeouts every 5 minutes
Logging: LoggingConfig{
LogsDir: "", // Will be set to data_dir/logs if empty
LogRotation: LogRotationConfig{
Enabled: true,
MaxSizeMB: 100,
MaxBackups: 3,
Compress: false,
},
},
LogRotationEnabled: true,
LogRotationMaxSizeMB: 100,
LogRotationCompress: false,
},
Database: DatabaseConfig{
Path: "", // Will be set to data_dir/llamactl.db if empty
@@ -285,8 +279,8 @@ func LoadConfig(configPath string) (AppConfig, error) {
// Log deprecation warning if using custom instances dir
log.Println("⚠️ Instances directory is deprecated and will be removed in future versions. Instances are persisted in the database.")
}
if cfg.Instances.Logging.LogsDir == "" {
cfg.Instances.Logging.LogsDir = filepath.Join(cfg.DataDir, "logs")
if cfg.Instances.LogsDir == "" {
cfg.Instances.LogsDir = filepath.Join(cfg.DataDir, "logs")
}
if cfg.Database.Path == "" {
cfg.Database.Path = filepath.Join(cfg.DataDir, "llamactl.db")
@@ -353,7 +347,7 @@ func loadEnvVars(cfg *AppConfig) {
cfg.Instances.InstancesDir = instancesDir
}
if logsDir := os.Getenv("LLAMACTL_LOGS_DIR"); logsDir != "" {
cfg.Instances.Logging.LogsDir = logsDir
cfg.Instances.LogsDir = logsDir
}
if autoCreate := os.Getenv("LLAMACTL_AUTO_CREATE_DATA_DIR"); autoCreate != "" {
if b, err := strconv.ParseBool(autoCreate); err == nil {
@@ -574,6 +568,23 @@ func loadEnvVars(cfg *AppConfig) {
cfg.Database.ConnMaxLifetime = d
}
}
// Log rotation config
if logRotationEnabled := os.Getenv("LLAMACTL_LOG_ROTATION_ENABLED"); logRotationEnabled != "" {
if b, err := strconv.ParseBool(logRotationEnabled); err == nil {
cfg.Instances.LogRotationEnabled = b
}
}
if logRotationMaxSizeMB := os.Getenv("LLAMACTL_LOG_ROTATION_MAX_SIZE_MB"); logRotationMaxSizeMB != "" {
if m, err := strconv.Atoi(logRotationMaxSizeMB); err == nil {
cfg.Instances.LogRotationMaxSizeMB = m
}
}
if logRotationCompress := os.Getenv("LLAMACTL_LOG_ROTATION_COMPRESS"); logRotationCompress != "" {
if b, err := strconv.ParseBool(logRotationCompress); err == nil {
cfg.Instances.LogRotationCompress = b
}
}
}
// ParsePortRange parses port range from string formats like "8000-9000" or "8000,9000"

View File

@@ -44,8 +44,8 @@ func TestLoadConfig_Defaults(t *testing.T) {
if cfg.Instances.InstancesDir != filepath.Join(homedir, ".local", "share", "llamactl", "instances") {
t.Errorf("Expected default instances directory '%s', got %q", filepath.Join(homedir, ".local", "share", "llamactl", "instances"), cfg.Instances.InstancesDir)
}
if cfg.Instances.Logging.LogsDir != filepath.Join(homedir, ".local", "share", "llamactl", "logs") {
t.Errorf("Expected default logs directory '%s', got %q", filepath.Join(homedir, ".local", "share", "llamactl", "logs"), cfg.Instances.Logging.LogsDir)
if cfg.Instances.LogsDir != filepath.Join(homedir, ".local", "share", "llamactl", "logs") {
t.Errorf("Expected default logs directory '%s', got %q", filepath.Join(homedir, ".local", "share", "llamactl", "logs"), cfg.Instances.LogsDir)
}
if !cfg.Instances.AutoCreateDirs {
t.Error("Expected default instances auto-create to be true")
@@ -79,7 +79,6 @@ server:
instances:
port_range: [7000, 8000]
max_instances: 5
logging:
logs_dir: "/custom/logs"
llama_executable: "/usr/bin/llama-server"
default_auto_restart: false
@@ -107,8 +106,8 @@ instances:
if cfg.Instances.PortRange != [2]int{7000, 8000} {
t.Errorf("Expected port range [7000, 8000], got %v", cfg.Instances.PortRange)
}
if cfg.Instances.Logging.LogsDir != "/custom/logs" {
t.Errorf("Expected logs directory '/custom/logs', got %q", cfg.Instances.Logging.LogsDir)
if cfg.Instances.LogsDir != "/custom/logs" {
t.Errorf("Expected logs directory '/custom/logs', got %q", cfg.Instances.LogsDir)
}
if cfg.Instances.MaxInstances != 5 {
t.Errorf("Expected max instances 5, got %d", cfg.Instances.MaxInstances)
@@ -158,8 +157,8 @@ func TestLoadConfig_EnvironmentOverrides(t *testing.T) {
if cfg.Instances.PortRange != [2]int{5000, 6000} {
t.Errorf("Expected port range [5000, 6000], got %v", cfg.Instances.PortRange)
}
if cfg.Instances.Logging.LogsDir != "/env/logs" {
t.Errorf("Expected logs directory '/env/logs', got %q", cfg.Instances.Logging.LogsDir)
if cfg.Instances.LogsDir != "/env/logs" {
t.Errorf("Expected logs directory '/env/logs', got %q", cfg.Instances.LogsDir)
}
if cfg.Instances.MaxInstances != 20 {
t.Errorf("Expected max instances 20, got %d", cfg.Instances.MaxInstances)

View File

@@ -68,10 +68,15 @@ func New(name string, globalConfig *config.AppConfig, opts *Options, onStatusCha
// Only create logger, proxy, and process for local instances
if !instance.IsRemote() {
logRotationConfig := &config.LogRotationConfig{
Enabled: globalInstanceSettings.LogRotationEnabled,
MaxSizeMB: globalInstanceSettings.LogRotationMaxSizeMB,
Compress: globalInstanceSettings.LogRotationCompress,
}
instance.logger = newLogger(
name,
globalInstanceSettings.Logging.LogsDir,
&globalInstanceSettings.Logging.LogRotation,
globalInstanceSettings.LogsDir,
logRotationConfig,
)
instance.process = newProcess(instance)
}

View File

@@ -28,9 +28,7 @@ func TestNewInstance(t *testing.T) {
},
Instances: config.InstancesConfig{
DefaultAutoRestart: true,
Logging: config.LoggingConfig{
LogsDir: "/tmp/test",
},
DefaultMaxRestarts: 3,
DefaultRestartDelay: 5,
},
@@ -123,9 +121,7 @@ func TestSetOptions(t *testing.T) {
},
Instances: config.InstancesConfig{
DefaultAutoRestart: true,
Logging: config.LoggingConfig{
LogsDir: "/tmp/test",
},
DefaultMaxRestarts: 3,
DefaultRestartDelay: 5,
},
@@ -180,7 +176,7 @@ func TestMarshalJSON(t *testing.T) {
Backends: config.BackendConfig{
LlamaCpp: config.BackendSettings{Command: "llama-server"},
},
Instances: config.InstancesConfig{Logging: config.LoggingConfig{LogsDir: "/tmp/test"}},
Instances: config.InstancesConfig{LogsDir: "/tmp/test"},
Nodes: map[string]config.NodeConfig{},
LocalNode: "main",
}
@@ -317,10 +313,8 @@ func TestCreateOptionsValidation(t *testing.T) {
},
},
Instances: config.InstancesConfig{
Logging: config.LoggingConfig{
LogsDir: "/tmp/test",
},
},
Nodes: map[string]config.NodeConfig{},
LocalNode: "main",
}
@@ -364,7 +358,7 @@ func TestStatusChangeCallback(t *testing.T) {
Backends: config.BackendConfig{
LlamaCpp: config.BackendSettings{Command: "llama-server"},
},
Instances: config.InstancesConfig{Logging: config.LoggingConfig{LogsDir: "/tmp/test"}},
Instances: config.InstancesConfig{LogsDir: "/tmp/test"},
Nodes: map[string]config.NodeConfig{},
LocalNode: "main",
}
@@ -406,7 +400,7 @@ func TestSetOptions_NodesPreserved(t *testing.T) {
Backends: config.BackendConfig{
LlamaCpp: config.BackendSettings{Command: "llama-server"},
},
Instances: config.InstancesConfig{Logging: config.LoggingConfig{LogsDir: "/tmp/test"}},
Instances: config.InstancesConfig{LogsDir: "/tmp/test"},
Nodes: map[string]config.NodeConfig{},
LocalNode: "main",
}
@@ -488,7 +482,7 @@ func TestProcessErrorCases(t *testing.T) {
Backends: config.BackendConfig{
LlamaCpp: config.BackendSettings{Command: "llama-server"},
},
Instances: config.InstancesConfig{Logging: config.LoggingConfig{LogsDir: "/tmp/test"}},
Instances: config.InstancesConfig{LogsDir: "/tmp/test"},
Nodes: map[string]config.NodeConfig{},
LocalNode: "main",
}
@@ -524,7 +518,7 @@ func TestRemoteInstanceOperations(t *testing.T) {
Backends: config.BackendConfig{
LlamaCpp: config.BackendSettings{Command: "llama-server"},
},
Instances: config.InstancesConfig{Logging: config.LoggingConfig{LogsDir: "/tmp/test"}},
Instances: config.InstancesConfig{LogsDir: "/tmp/test"},
Nodes: map[string]config.NodeConfig{
"remote-node": {Address: "http://remote-node:8080"},
},
@@ -572,7 +566,7 @@ func TestIdleTimeout(t *testing.T) {
Backends: config.BackendConfig{
LlamaCpp: config.BackendSettings{Command: "llama-server"},
},
Instances: config.InstancesConfig{Logging: config.LoggingConfig{LogsDir: "/tmp/test"}},
Instances: config.InstancesConfig{LogsDir: "/tmp/test"},
Nodes: map[string]config.NodeConfig{},
LocalNode: "main",
}

View File

@@ -49,7 +49,7 @@ func (l *logger) create() error {
t := &timber.Logger{
Filename: logPath,
MaxSize: l.cfg.MaxSizeMB,
MaxBackups: l.cfg.MaxBackups,
MaxBackups: 0, // No limit on backups - use index-based naming
// Compression: "gzip" if Compress is true, else "none"
Compression: func() string {
if l.cfg.Compress {
@@ -58,7 +58,7 @@ func (l *logger) create() error {
return "none"
}(),
FileMode: 0644, // default; timberjack uses 640 if 0
LocalTime: true, // use local time for consistency with lumberjack
LocalTime: false, // Use index-based naming instead of timestamps
}
// If rotation is disabled, set MaxSize to 0 so no rotation occurs

View File

@@ -207,9 +207,7 @@ func createTestAppConfig(instancesDir string) *config.AppConfig {
MaxRunningInstances: 10,
DefaultAutoRestart: true,
DefaultMaxRestarts: 3,
Logging: config.LoggingConfig{
LogsDir: instancesDir,
},
DefaultRestartDelay: 5,
TimeoutCheckInterval: 5,
},