Files
job-search/internal/config/print.go
T
bwilliams 043e307663 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.
2026-10-15 16:10:52 +01:00

59 lines
1.5 KiB
Go

package config
import (
"context"
"fmt"
"log/slog"
"maps"
"slices"
"git.avsdev.uk/Go/job-search/internal/logging"
)
// PrintTerminal prints the configuration to stdout as String formats it.
func (conf JobSearchConfig) PrintTerminal() {
fmt.Print(conf.String() + "\n")
}
// PrintJson prints the configuration to stdout as JSON.
func (conf JobSearchConfig) PrintJson() {
fmt.Print(conf.JSON() + "\n")
}
// PrintYaml prints the configuration to stdout as YAML.
func (conf JobSearchConfig) PrintYaml() {
fmt.Print(conf.YAML() + "\n")
}
// PrintLog logs each configuration key and value, sorted by key, at atLevel:
// debug, warn, error, or info for any other level. On a terminal it logs one
// line and prints the values under it as "key = value" lines; elsewhere each
// value is a log entry of its own.
func (conf JobSearchConfig) PrintLog(atLevel slog.Level) {
switch atLevel {
case slog.LevelDebug, slog.LevelWarn, slog.LevelError:
default:
atLevel = slog.LevelInfo
}
ctx := context.Background()
logger := slog.Default()
flat := conf.Flatten()
keys := slices.Sorted(maps.Keys(flat))
if logging.WritesToTerminal(logger.Handler()) {
if !logger.Enabled(ctx, atLevel) {
return
}
logger.Log(ctx, atLevel, "config values", "count", len(keys))
for _, key := range keys {
logging.PrintToTerminal(fmt.Sprintf("\t%s = %v", key, flat[key]))
}
return
}
for _, key := range keys {
logger.Log(ctx, atLevel, "config value", "key", key, "value", flat[key])
}
}