feat: make logger time format configurable via config.json

This commit is contained in:
Badgerbees 2026-03-13 14:38:09 +07:00
parent 9676e51e89
commit edec7a6cb5
3 changed files with 28 additions and 1 deletions

View file

@ -510,6 +510,9 @@
"voice": {
"echo_transcription": false
},
"logger": {
"time_format": "15:04:05"
},
"gateway": {
"host": "127.0.0.1",
"port": 18790

View file

@ -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
}

View file

@ -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()