feat: write log on file

This commit is contained in:
Mauro Trulli 2026-02-18 10:56:51 +01:00
parent 3390576eea
commit d65d0ea5dc
2 changed files with 44 additions and 0 deletions

View file

@ -405,6 +405,9 @@ func agentCmd() {
os.Exit(1) os.Exit(1)
} }
setupLogging(cfg)
defer logger.DisableFileLogging()
msgBus := bus.NewMessageBus() msgBus := bus.NewMessageBus()
agentLoop := agent.NewAgentLoop(cfg, msgBus, provider) agentLoop := agent.NewAgentLoop(cfg, msgBus, provider)
@ -534,6 +537,9 @@ func gatewayCmd() {
os.Exit(1) os.Exit(1)
} }
setupLogging(cfg)
defer logger.DisableFileLogging()
provider, err := providers.CreateProvider(cfg) provider, err := providers.CreateProvider(cfg)
if err != nil { if err != nil {
fmt.Printf("Error creating provider: %v\n", err) fmt.Printf("Error creating provider: %v\n", err)
@ -1425,3 +1431,29 @@ func skillsShowCmd(loader *skills.SkillsLoader, skillName string) {
fmt.Println("----------------------") fmt.Println("----------------------")
fmt.Println(content) 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)
}
if cfg.Logging.EnableFile && cfg.Logging.FilePath != "" {
logPath := cfg.Logging.FilePath
if strings.HasPrefix(logPath, "~") {
home, _ := os.UserHomeDir()
logPath = filepath.Join(home, logPath[1:])
}
os.MkdirAll(filepath.Dir(logPath), 0755)
if err := logger.EnableFileLogging(logPath); err != nil {
fmt.Printf("Warning: unable to create log file: %v\n", err)
}
}
}

View file

@ -51,6 +51,7 @@ type Config struct {
Tools ToolsConfig `json:"tools"` Tools ToolsConfig `json:"tools"`
Heartbeat HeartbeatConfig `json:"heartbeat"` Heartbeat HeartbeatConfig `json:"heartbeat"`
Devices DevicesConfig `json:"devices"` Devices DevicesConfig `json:"devices"`
Logging LoggingConfig `json:"logging"`
mu sync.RWMutex mu sync.RWMutex
} }
@ -166,6 +167,12 @@ type DevicesConfig struct {
MonitorUSB bool `json:"monitor_usb" env:"PICOCLAW_DEVICES_MONITOR_USB"` MonitorUSB bool `json:"monitor_usb" env:"PICOCLAW_DEVICES_MONITOR_USB"`
} }
type LoggingConfig struct {
EnableFile bool `json:"enable_file" env:"PICOCLAW_LOGGING_ENABLE_FILE"`
FilePath string `json:"file_path" env:"PICOCLAW_LOGGING_FILE_PATH"`
Level string `json:"level" env:"PICOCLAW_LOGGING_LEVEL"`
}
type ProvidersConfig struct { type ProvidersConfig struct {
Anthropic ProviderConfig `json:"anthropic"` Anthropic ProviderConfig `json:"anthropic"`
OpenAI OpenAIProviderConfig `json:"openai"` OpenAI OpenAIProviderConfig `json:"openai"`
@ -356,6 +363,11 @@ func DefaultConfig() *Config {
Enabled: false, Enabled: false,
MonitorUSB: true, MonitorUSB: true,
}, },
Logging: LoggingConfig{
EnableFile: false,
FilePath: "~/.picoclaw/workspace/picoclaw_audit.log",
Level: "info",
},
} }
} }