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 (c JobSearchConfig) PrintTerminal() { fmt.Print(c.String() + "\n") } // PrintJSON prints the configuration to stdout as JSON. func (c JobSearchConfig) PrintJSON() { fmt.Print(c.JSON() + "\n") } // PrintYAML prints the configuration to stdout as YAML. func (c JobSearchConfig) PrintYAML() { fmt.Print(c.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 (c 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 := c.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]) } }