@@ -3,6 +3,7 @@ package app
33import (
44 "context"
55 "fmt"
6+ "log/slog"
67 "os"
78 "os/signal"
89 "syscall"
@@ -24,6 +25,8 @@ type config struct {
2425 expose string
2526 daemon bool
2627 metricsPrefix string
28+ logFormat string
29+ logger * slog.Logger
2730}
2831
2932func newRootCmd (version string ) * cobra.Command {
@@ -61,6 +64,7 @@ Keybindings:
6164 SilenceUsage : true ,
6265 SilenceErrors : true ,
6366 PreRunE : func (cmd * cobra.Command , args []string ) error {
67+ initLogger (& cfg )
6468 return validate (& cfg )
6569 },
6670 RunE : func (cmd * cobra.Command , args []string ) error {
@@ -89,7 +93,7 @@ Keybindings:
8993
9094 switch {
9195 case cfg .jsonMode :
92- runJSON (ctx , f , cfg .interval , cfg .once )
96+ runJSON (ctx , f , cfg .interval , cfg .once , cfg . logger )
9397 case cfg .daemon :
9498 return runDaemon (ctx , f , & cfg )
9599 default :
@@ -113,6 +117,7 @@ Keybindings:
113117 f .StringVar (& cfg .expose , "expose" , "" , "Expose Prometheus metrics (e.g. :9191)" )
114118 f .BoolVar (& cfg .daemon , "daemon" , false , "Headless mode (requires --expose)" )
115119 f .StringVar (& cfg .metricsPrefix , "metrics-prefix" , "" , "Prefix for exported Prometheus metric names" )
120+ f .StringVar (& cfg .logFormat , "log-format" , "text" , "Log format for daemon/json modes (text or json)" )
116121
117122 cmd .AddCommand (newStatusCmd (& cfg ))
118123 cmd .AddCommand (newWaitCmd (& cfg ))
@@ -135,6 +140,15 @@ func contextWithTimeout(parent context.Context, timeout time.Duration) (context.
135140 return parent , func () {}
136141}
137142
143+ func initLogger (cfg * config ) {
144+ switch cfg .logFormat {
145+ case "json" :
146+ cfg .logger = slog .New (slog .NewJSONHandler (os .Stderr , nil ))
147+ default :
148+ cfg .logger = slog .New (slog .NewTextHandler (os .Stderr , nil ))
149+ }
150+ }
151+
138152func validate (cfg * config ) error {
139153 if cfg .daemon && cfg .expose == "" {
140154 return fmt .Errorf ("--daemon requires --expose" )
0 commit comments