fix(logging): align default paths and refactor logging setup
This commit is contained in:
parent
12af8a72df
commit
fce7fd3cfb
3 changed files with 87 additions and 16 deletions
|
|
@ -1432,25 +1432,37 @@ func skillsShowCmd(loader *skills.SkillsLoader, skillName string) {
|
|||
fmt.Println(content)
|
||||
}
|
||||
|
||||
func setupLogging(cfg *config.Config) {
|
||||
if strings.ToLower(cfg.Logging.Level) == "debug" {
|
||||
logger.SetLevel(logger.DEBUG)
|
||||
} else if strings.ToLower(cfg.Logging.Level) == "warn" {
|
||||
logger.SetLevel(logger.WARN)
|
||||
} else if strings.ToLower(cfg.Logging.Level) == "error" {
|
||||
logger.SetLevel(logger.ERROR)
|
||||
} else {
|
||||
logger.SetLevel(logger.INFO)
|
||||
func parseLogLevel(level string) logger.LogLevel {
|
||||
switch strings.ToLower(level) {
|
||||
case "debug":
|
||||
return logger.DEBUG
|
||||
case "warn":
|
||||
return logger.WARN
|
||||
case "error":
|
||||
return logger.ERROR
|
||||
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 != "" {
|
||||
logPath := cfg.Logging.FilePath
|
||||
if strings.HasPrefix(logPath, "~") {
|
||||
home, _ := os.UserHomeDir()
|
||||
logPath = filepath.Join(home, logPath[1:])
|
||||
}
|
||||
logPath := expandPath(cfg.Logging.FilePath)
|
||||
|
||||
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 {
|
||||
fmt.Printf("Warning: unable to create log file: %v\n", err)
|
||||
|
|
|
|||
59
cmd/picoclaw/main_test.go
Normal file
59
cmd/picoclaw/main_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -365,7 +365,7 @@ func DefaultConfig() *Config {
|
|||
},
|
||||
Logging: LoggingConfig{
|
||||
EnableFile: false,
|
||||
FilePath: "~/.picoclaw/workspace/picoclaw_audit.log",
|
||||
FilePath: "picoclaw_audit.log",
|
||||
Level: "info",
|
||||
},
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue