fix(logging): align default paths and refactor logging setup

This commit is contained in:
Mauro Trulli 2026-02-19 13:04:56 +01:00
parent 12af8a72df
commit fce7fd3cfb
3 changed files with 87 additions and 16 deletions

View file

@ -1432,25 +1432,37 @@ func skillsShowCmd(loader *skills.SkillsLoader, skillName string) {
fmt.Println(content) fmt.Println(content)
} }
func setupLogging(cfg *config.Config) { func parseLogLevel(level string) logger.LogLevel {
if strings.ToLower(cfg.Logging.Level) == "debug" { switch strings.ToLower(level) {
logger.SetLevel(logger.DEBUG) case "debug":
} else if strings.ToLower(cfg.Logging.Level) == "warn" { return logger.DEBUG
logger.SetLevel(logger.WARN) case "warn":
} else if strings.ToLower(cfg.Logging.Level) == "error" { return logger.WARN
logger.SetLevel(logger.ERROR) case "error":
} else { return logger.ERROR
logger.SetLevel(logger.INFO) default:
return logger.INFO
} }
}
func expandPath(p string) string {
if p == "~" || strings.HasPrefix(p, "~/") {
if home, err := os.UserHomeDir(); err == nil {
return filepath.Join(home, p[1:])
}
}
return p
}
func setupLogging(cfg *config.Config) {
logger.SetLevel(parseLogLevel(cfg.Logging.Level))
if cfg.Logging.EnableFile && cfg.Logging.FilePath != "" { if cfg.Logging.EnableFile && cfg.Logging.FilePath != "" {
logPath := cfg.Logging.FilePath logPath := expandPath(cfg.Logging.FilePath)
if strings.HasPrefix(logPath, "~") {
home, _ := os.UserHomeDir()
logPath = filepath.Join(home, logPath[1:])
}
os.MkdirAll(filepath.Dir(logPath), 0755) if err := os.MkdirAll(filepath.Dir(logPath), 0755); err != nil {
fmt.Printf("Warning: unable to create log directory: %v\n", err)
}
if err := logger.EnableFileLogging(logPath); err != nil { if err := logger.EnableFileLogging(logPath); err != nil {
fmt.Printf("Warning: unable to create log file: %v\n", err) fmt.Printf("Warning: unable to create log file: %v\n", err)

59
cmd/picoclaw/main_test.go Normal file
View file

@ -0,0 +1,59 @@
package main
import (
"os"
"path/filepath"
"testing"
"github.com/sipeed/picoclaw/pkg/logger"
)
func TestParseLogLevel(t *testing.T) {
tests := []struct {
input string
expected logger.LogLevel
}{
{"debug", logger.DEBUG},
{"DEBUG", logger.DEBUG},
{"warn", logger.WARN},
{"error", logger.ERROR},
{"info", logger.INFO},
{"unknown", logger.INFO}, // Default fallback
{"", logger.INFO}, // Empty string fallback
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
if got := parseLogLevel(tt.input); got != tt.expected {
t.Errorf("parseLogLevel(%q) = %v, want %v", tt.input, got, tt.expected)
}
})
}
}
func TestExpandPath(t *testing.T) {
home, err := os.UserHomeDir()
if err != nil {
t.Skip("Skipping test: unable to get user home directory")
}
tests := []struct {
name string
input string
expected string
}{
{"Home directory root", "~", home},
{"Home directory subpath", "~/logs/audit.log", filepath.Join(home, "/logs/audit.log")},
{"Absolute path", "/var/log/app.log", "/var/log/app.log"},
{"Relative path", "./logs/app.log", "./logs/app.log"},
{"Other user home (should not expand)", "~user/logs.txt", "~user/logs.txt"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := expandPath(tt.input); got != tt.expected {
t.Errorf("expandPath(%q) = %q, want %q", tt.input, got, tt.expected)
}
})
}
}

View file

@ -365,7 +365,7 @@ func DefaultConfig() *Config {
}, },
Logging: LoggingConfig{ Logging: LoggingConfig{
EnableFile: false, EnableFile: false,
FilePath: "~/.picoclaw/workspace/picoclaw_audit.log", FilePath: "picoclaw_audit.log",
Level: "info", Level: "info",
}, },
} }