Skip to content

Commit

Permalink
feat: add verbosity flag
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed Jan 20, 2024
1 parent c4a6b54 commit 2b9873f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func NewCmdRoot(version string) *cobra.Command {
})

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.sparrow.yaml)")
rootCmd.PersistentFlags().BoolP("verbosity", "v", false, "Enable verbose logging for enhanced visibility and troubleshooting")

return rootCmd
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package cmd
import (
"context"
"fmt"
"log/slog"
"os"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -64,7 +66,14 @@ func NewCmdRun() *cobra.Command {
// run is the entry point to start the sparrow
func run() func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
v, _ := cmd.Flags().GetBool("verbosity")
log := logger.NewLogger()
if v {
log = logger.NewLogger(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}))
}
ctx := logger.IntoContext(context.Background(), log)

cfg := config.NewConfig()
Expand Down
1 change: 1 addition & 0 deletions docs/sparrow.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The check results are exposed via an API.
```
--config string config file (default is $HOME/.sparrow.yaml)
-h, --help help for sparrow
-v, --verbosity Enable verbose logging for enhanced visibility and troubleshooting
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/sparrow_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sparrow run [flags]

```
--config string config file (default is $HOME/.sparrow.yaml)
-v, --verbosity Enable verbose logging for enhanced visibility and troubleshooting
```

### SEE ALSO
Expand Down
2 changes: 1 addition & 1 deletion internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewLogger(h ...slog.Handler) *slog.Logger {
if len(h) > 0 {
handler = h[0]
} else {
handler = slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
handler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
})
}
Expand Down

0 comments on commit 2b9873f

Please sign in to comment.