feat: add json log format option

Add log_format config field ("text" default, "json" for structured
JSON output) to support machine-readable logging.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 16:45:38 +01:00
parent a40110f2f5
commit 3ebf88fb3e
3 changed files with 14 additions and 2 deletions

View File

@@ -34,7 +34,14 @@ func main() {
level.Set(slog.LevelInfo)
}
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: level}))
var handler slog.Handler
opts := &slog.HandlerOptions{Level: level}
if cfg.LogFormat == "json" {
handler = slog.NewJSONHandler(os.Stderr, opts)
} else {
handler = slog.NewTextHandler(os.Stderr, opts)
}
logger := slog.New(handler)
slog.SetDefault(logger)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

View File

@@ -12,6 +12,7 @@ type Config struct {
SSH SSHConfig `toml:"ssh"`
Auth AuthConfig `toml:"auth"`
LogLevel string `toml:"log_level"`
LogFormat string `toml:"log_format"` // "text" (default) or "json"
}
type SSHConfig struct {
@@ -73,6 +74,9 @@ func applyDefaults(cfg *Config) {
if cfg.LogLevel == "" {
cfg.LogLevel = "info"
}
if cfg.LogFormat == "" {
cfg.LogFormat = "text"
}
}
func validate(cfg *Config) error {

View File

@@ -1,4 +1,5 @@
log_level = "info"
log_format = "text" # "text" or "json"
[ssh]
listen_addr = ":2222"