Logging: structured slog messages throughout, and WithLogger options so callers choose the logger

Log messages are lower-case event names with their details as key/value
attributes ("error" for errors), in place of formatted strings.

Loggable package take a WithLogger option as part of their "New*" call,
defaulting to slog.Default(). Each tags its lines with its `component`.

Some debug loggers print directly rather than using slog when in
terminal logging mode.
This commit is contained in:
bwilliams committed 2026-10-15 16:10:52 +01:00
1 parent 0b5fde9563
commit 043e307663
49 files changed
+598 -248

No files matched your search

+2 -2
View File
@@ -14,10 +14,10 @@ func NewCommand(opts *cli.Options) *cobra.Command {
Use: "config",
Short: "Configuration management",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("config.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "config")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("config.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "config")
},
}
cmd.AddCommand(commandShow(opts))
+3 -3
View File
@@ -15,13 +15,13 @@ func commandGet(opts *cli.Options) *cobra.Command {
Args: cobra.ExactArgs(1),
Short: "Get a value from configuration",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("config.get.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "config.get")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("config.get.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "config.get")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("config.get.Run")
slog.Debug("command run", "command", "config.get")
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
+3 -3
View File
@@ -19,13 +19,13 @@ func commandInit() *cobra.Command {
Args: cobra.MatchAll(cobra.MinimumNArgs(0), cobra.MaximumNArgs(1)),
Short: "Create an empty configuration file",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("config.init.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "config.init")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("config.init.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "config.init")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("config.init.Run")
slog.Debug("command run", "command", "config.init")
if err := cobra.MinimumNArgs(0)(cmd, args); err != nil {
return err
+3 -3
View File
@@ -18,13 +18,13 @@ func commandSet(opts *cli.Options) *cobra.Command {
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.MaximumNArgs(2)),
Short: "Set a value to configuration",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("config.set.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "config.set")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("config.set.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "config.set")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("config.set.Run")
slog.Debug("command run", "command", "config.set")
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
return err
+3 -3
View File
@@ -14,13 +14,13 @@ func commandShow(opts *cli.Options) *cobra.Command {
Use: "show",
Short: "Print config information",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("config.show.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "config.show")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("config.show.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "config.show")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("config.show.Run")
slog.Debug("command run", "command", "config.show")
conf, err := opts.RunSetup(cmd, true, true)
if err != nil {
+3 -2
View File
@@ -3,7 +3,7 @@ package cli
import (
"bufio"
"fmt"
"log"
"log/slog"
"os"
"strings"
)
@@ -19,7 +19,8 @@ func Confirm(s string) bool {
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
slog.Error("reading the answer", "error", err)
os.Exit(1)
}
response = strings.ToLower(strings.TrimSpace(response))
+2 -2
View File
@@ -14,10 +14,10 @@ func NewCommand(opts *cli.Options) *cobra.Command {
Use: "fetch",
Short: "Fetch current opportunities",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("fetch.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "fetch")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("fetch.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "fetch")
},
}
cmd.AddCommand(commandOnce(opts))
+3 -3
View File
@@ -53,13 +53,13 @@ func commandOnce(opts *cli.Options) *cobra.Command {
Use: "once",
Short: "Fetch current opportunities - single shot",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("fetch.once.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "fetch.once")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("fetch.once.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "fetch.once")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("fetch.once.Run")
slog.Debug("command run", "command", "fetch.once")
conf, err := opts.RunSetup(cmd, true, true)
if err != nil {
+3 -3
View File
@@ -20,13 +20,13 @@ func commandService(opts *cli.Options) *cobra.Command {
Use: "service",
Short: "Fetch current opportunities - background service",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("fetch.service.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "fetch.service")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("fetch.service.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "fetch.service")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("fetch.service.Run")
slog.Debug("command run", "command", "fetch.service")
foreground, _ := cmd.Flags().GetBool("foreground")
+3 -3
View File
@@ -21,13 +21,13 @@ func commandDown(opts *cli.Options) *cobra.Command {
confirmed by providing -yes.`,
Args: cobra.NoArgs,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("migrate.down.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "migrate.down")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("migrate.down.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "migrate.down")
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
slog.Debug("migrate.down.Run")
slog.Debug("command run", "command", "migrate.down")
yes, _ := cmd.Flags().GetBool("yes")
steps, _ := cmd.Flags().GetUint("steps")
+3 -3
View File
@@ -26,13 +26,13 @@ Running without a subcommand is the same as running "migrate up": every pending
migration is applied.`,
Args: cobra.NoArgs,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("migrate.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "migrate")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("migrate.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "migrate")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("migrate.Run")
slog.Debug("command run", "command", "migrate")
return runStepUp(cmd, opts, 0, false)
},
}
+3 -3
View File
@@ -75,13 +75,13 @@ func commandStatus(opts *cli.Options) *cobra.Command {
Short: "Show which migrations have been applied to the SQLite database",
Args: cobra.NoArgs,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("migrate.status.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "migrate.status")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("migrate.status.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "migrate.status")
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
slog.Debug("migrate.status.Run")
slog.Debug("command run", "command", "migrate.status")
m, db, err := openMigrator(cmd, opts, false, false)
if err != nil {
+3 -3
View File
@@ -46,13 +46,13 @@ func commandUp(opts *cli.Options) *cobra.Command {
Short: "Migrate SQLite database to the latest version, or by a number of steps",
Args: cobra.NoArgs,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("migrate.up.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "migrate.up")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("migrate.up.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "migrate.up")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("migrate.up.Run")
slog.Debug("command run", "command", "migrate.up")
steps, _ := cmd.Flags().GetUint("steps")
create, _ := cmd.Flags().GetBool("create")
+1 -1
View File
@@ -65,7 +65,7 @@ func (o *Options) RunSetup(
// Check if the logging should be switched to file
if !terminalLog {
if len(conf.Logging.File) == 0 {
slog.Warn("No log file configured, remaining in console logging mode!")
slog.Warn("no log file configured; logging to the terminal")
} else if err := logging.LogToFile(conf.Logging.File); err != nil {
return nil, fmt.Errorf("opening log file: %w", err)
}
+3 -3
View File
@@ -16,13 +16,13 @@ func NewCommand() *cobra.Command {
Short: "Print version information",
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("version.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "version")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("version.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "version")
},
Run: func(cmd *cobra.Command, args []string) {
slog.Debug("version.Run")
slog.Debug("command run", "command", "version")
fmt.Printf("JobSearch - %s\n", version.Version)
if version.BuildTime != "unknown" {
+5 -5
View File
@@ -22,13 +22,13 @@ func NewCommand(opts *cli.Options) *cobra.Command {
Use: "wsserve",
Short: "Run JobSearch TCP Websocket server",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("wsserve.PersistentPreRun")
slog.Debug("command persistent pre-run", "command", "wsserve")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("wsserve.PersistentPostRun")
slog.Debug("command persistent post-run", "command", "wsserve")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("wsserve.Run")
slog.Debug("command run", "command", "wsserve")
addrOverride, _ := cmd.Flags().GetString("listen")
hostOverride, _ := cmd.Flags().GetString("host")
@@ -74,8 +74,8 @@ func NewCommand(opts *cli.Options) *cobra.Command {
stop()
}()
hub := websocket.NewHub(conf.Server.WebSocket, slog.Default())
svr := tcpserver.New(&conf.Server, hub, slog.Default())
hub := websocket.NewHub(conf.Server.WebSocket)
svr := tcpserver.New(&conf.Server, hub)
return svr.Run(ctx)
},