feat: make logger time format configurable via config.json
- Add SetTimeFormat() function to apply time format to both console and file loggers - Apply zerolog.TimeFieldFormat for JSON file output consistency - Thread-safe configuration with mutex protection - Add LoggerConfig struct to main Config with TimeFormat field - Add example configuration to config.example.json
This commit is contained in:
parent
9676e51e89
commit
d5525675e7
3 changed files with 32 additions and 1 deletions
|
|
@ -510,6 +510,9 @@
|
||||||
"voice": {
|
"voice": {
|
||||||
"echo_transcription": false
|
"echo_transcription": false
|
||||||
},
|
},
|
||||||
|
"logger": {
|
||||||
|
"time_format": "15:04:05"
|
||||||
|
},
|
||||||
"gateway": {
|
"gateway": {
|
||||||
"host": "127.0.0.1",
|
"host": "127.0.0.1",
|
||||||
"port": 18790
|
"port": 18790
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/caarlos0/env/v11"
|
"github.com/caarlos0/env/v11"
|
||||||
|
|
||||||
"github.com/sipeed/picoclaw/pkg/fileutil"
|
"github.com/sipeed/picoclaw/pkg/fileutil"
|
||||||
|
"github.com/sipeed/picoclaw/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// rrCounter is a global counter for round-robin load balancing across models.
|
// 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LoggerConfig struct {
|
||||||
|
TimeFormat string `json:"time_format,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Agents AgentsConfig `json:"agents"`
|
Agents AgentsConfig `json:"agents"`
|
||||||
Bindings []AgentBinding `json:"bindings,omitempty"`
|
Bindings []AgentBinding `json:"bindings,omitempty"`
|
||||||
|
|
@ -86,6 +91,7 @@ type Config struct {
|
||||||
Heartbeat HeartbeatConfig `json:"heartbeat"`
|
Heartbeat HeartbeatConfig `json:"heartbeat"`
|
||||||
Devices DevicesConfig `json:"devices"`
|
Devices DevicesConfig `json:"devices"`
|
||||||
Voice VoiceConfig `json:"voice"`
|
Voice VoiceConfig `json:"voice"`
|
||||||
|
Logger LoggerConfig `json:"logger,omitempty"`
|
||||||
// BuildInfo contains build-time version information
|
// BuildInfo contains build-time version information
|
||||||
BuildInfo BuildInfo `json:"build_info,omitempty"`
|
BuildInfo BuildInfo `json:"build_info,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
@ -854,6 +860,11 @@ func LoadConfig(path string) (*Config, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply logger configuration if present
|
||||||
|
if cfg.Logger.TimeFormat != "" {
|
||||||
|
logger.SetTimeFormat(cfg.Logger.TimeFormat)
|
||||||
|
}
|
||||||
|
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ func init() {
|
||||||
|
|
||||||
consoleWriter := zerolog.ConsoleWriter{
|
consoleWriter := zerolog.ConsoleWriter{
|
||||||
Out: os.Stdout,
|
Out: os.Stdout,
|
||||||
TimeFormat: "15:04:05", // TODO: make it configurable???
|
TimeFormat: "15:04:05",
|
||||||
}
|
}
|
||||||
|
|
||||||
logger = zerolog.New(consoleWriter).With().Timestamp().Logger()
|
logger = zerolog.New(consoleWriter).With().Timestamp().Logger()
|
||||||
|
|
@ -52,6 +52,23 @@ func init() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetTimeFormat updates the logger's time format dynamically at runtime for both console and file.
|
||||||
|
func SetTimeFormat(format string) {
|
||||||
|
if format == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
// Apply format to zerolog's global JSON timestamp for file output
|
||||||
|
zerolog.TimeFieldFormat = format
|
||||||
|
|
||||||
|
logger = zerolog.New(zerolog.ConsoleWriter{
|
||||||
|
Out: os.Stdout,
|
||||||
|
TimeFormat: format,
|
||||||
|
}).With().Timestamp().Logger()
|
||||||
|
}
|
||||||
|
|
||||||
func SetLevel(level LogLevel) {
|
func SetLevel(level LogLevel) {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue