diff --git a/config/config.example.json b/config/config.example.json index 094aa46df..b979de149 100644 --- a/config/config.example.json +++ b/config/config.example.json @@ -510,6 +510,9 @@ "voice": { "echo_transcription": false }, + "logger": { + "time_format": "15:04:05" + }, "gateway": { "host": "127.0.0.1", "port": 18790 diff --git a/pkg/config/config.go b/pkg/config/config.go index 190341224..c98454e89 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -10,6 +10,7 @@ import ( "github.com/caarlos0/env/v11" "github.com/sipeed/picoclaw/pkg/fileutil" + "github.com/sipeed/picoclaw/pkg/logger" ) // rrCounter is a global counter for round-robin load balancing across models. @@ -74,6 +75,10 @@ func (f *FlexibleStringSlice) UnmarshalText(text []byte) error { return nil } +type LoggerConfig struct { + TimeFormat string `json:"time_format,omitempty"` +} + type Config struct { Agents AgentsConfig `json:"agents"` Bindings []AgentBinding `json:"bindings,omitempty"` @@ -86,6 +91,7 @@ type Config struct { Heartbeat HeartbeatConfig `json:"heartbeat"` Devices DevicesConfig `json:"devices"` Voice VoiceConfig `json:"voice"` + Logger LoggerConfig `json:"logger,omitempty"` // BuildInfo contains build-time version information BuildInfo BuildInfo `json:"build_info,omitempty"` } @@ -854,6 +860,11 @@ func LoadConfig(path string) (*Config, error) { return nil, err } + // Apply logger configuration if present + if cfg.Logger.TimeFormat != "" { + logger.SetTimeFormat(cfg.Logger.TimeFormat) + } + return cfg, nil } diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 302613f33..08f84beb6 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -44,7 +44,7 @@ func init() { consoleWriter := zerolog.ConsoleWriter{ Out: os.Stdout, - TimeFormat: "15:04:05", // TODO: make it configurable??? + TimeFormat: "15:04:05", } logger = zerolog.New(consoleWriter).With().Timestamp().Logger() @@ -52,6 +52,19 @@ func init() { }) } +// SetTimeFormat updates the logger's time format dynamically at runtime. +func SetTimeFormat(format string) { + if format == "" { + return + } + mu.Lock() + defer mu.Unlock() + logger = zerolog.New(zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: format, + }).With().Timestamp().Logger() +} + func SetLevel(level LogLevel) { mu.Lock() defer mu.Unlock()