Skip to content

feat: add log.file, log.format, log.level config file options#30

Merged
robbiet480 merged 5 commits intomainfrom
feat/log-config
Mar 7, 2026
Merged

feat: add log.file, log.format, log.level config file options#30
robbiet480 merged 5 commits intomainfrom
feat/log-config

Conversation

@robbiet480
Copy link
Member

@robbiet480 robbiet480 commented Mar 7, 2026

Summary

Adds a log section to settings.yaml so logging can be configured without passing flags on every invocation. CLI flags still take precedence.

log:
  file: "axm2snipe.log"   # append to this file in addition to stderr
  format: "text"           # "text" or "json"
  level: "debug"           # "warn" (default), "info", or "debug"

Test plan

  • go test ./... passes
  • Set log.level: debug in config, run axm2snipe sync without -d — debug output appears
  • Pass -v on CLI — overrides log.level: debug down to info
  • Set log.file: axm2snipe.log — log file written on each run

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Configure logging via config files: set log level, output file, and format; config values act as defaults when CLI flags are not set.
  • Bug Fixes
    • Validates config log level and emits a warning (after log format/output are set) for unknown levels.
    • Config-specified log file and format are applied while still writing to stderr; explicit CLI flags retain precedence.

CLI flags still take precedence. log.level accepts "debug", "info", or
"warn". Useful for always writing debug logs to a file without passing
flags on every invocation.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link

coderabbitai bot commented Mar 7, 2026

Caution

Review failed

Pull request was closed or merged during review

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 71afa764-ad9f-4f71-8315-a9e64c20e943

📥 Commits

Reviewing files that changed from the base of the PR and between 9425e3c and 5e63cf6.

📒 Files selected for processing (2)
  • cmd/root.go
  • config/config.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • config/config.go

Walkthrough

Derive effective logging settings from loaded config when CLI flags (debug, verbose, log-file, log-format) are unset; apply those effective values to choose log level, formatter, and output file without mutating flag-backed globals; defer and emit a warning for unknown config log.level after logger is configured.

Changes

Cohort / File(s) Summary
Root command
cmd/root.go
Compute effective logging values (effectiveDebug, effectiveVerbose, effectiveLogFile, effectiveLogFormat) from Config.Log when CLI flags are not set; validate Config.Log.Level, defer unknown-level warning, select formatter by effectiveLogFormat, open effectiveLogFile for output, and apply log level after format/output setup.
Configuration
config/config.go
Add exported LogConfig type with fields File, Format, Level and add Log LogConfig yaml:"log"`` to Config to hold logging defaults from config.

Sequence Diagram(s)

sequenceDiagram
    participant User as "User (CLI)"
    participant Root as "Root Command"
    participant Config as "Config Loader"
    participant Logger as "Logger Setup"

    User->>Root: invoke CLI (with or without flags)
    Root->>Config: load configuration
    Config-->>Root: return Config (contains Log)
    Root->>Root: determine effectiveDebug/effectiveVerbose/effectiveLogFile/effectiveLogFormat
    Root->>Logger: select formatter (by effectiveLogFormat)
    Root->>Logger: open log file (effectiveLogFile) if set
    Root->>Logger: set log level (using effective debug/verbose or validated Config.Log.Level)
    Logger-->>Root: logger initialized
    Root-->>User: start application (emit deferred unknown-level warning if present)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I hop through configs, sniffing each file,
If flags stay silent, I choose with a smile.
I pick the format, open logs with care,
A cautious warning waits till outputs are there.
I stamp each line — soft, steady, and sly.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding log.file, log.format, and log.level as configuration file options, which aligns with the core PR objectives and implementation changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/log-config

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
cmd/root.go (1)

59-73: Config-seeded logging logic looks correct; minor consistency suggestion.

The implementation correctly prioritizes CLI flags over config values. A few observations:

  1. Boolean flag check (Line 60): Using !debug && !verbose works because these flags default to false, but it's inconsistent with the cmd.Flags().Changed() approach used for log-file and log-format. For clarity and consistency, consider:

  2. Invalid config values: If Cfg.Log.Level contains an unrecognized value (e.g., "trace"), it silently falls through to the default (warn). This is probably fine, but you could log a warning for discoverability.

♻️ Optional: Use Changed() for consistency
 	// Seed log settings from config file; CLI flags take precedence.
-	if Cfg.Log.Level != "" && !debug && !verbose {
+	if Cfg.Log.Level != "" && !cmd.Flags().Changed("debug") && !cmd.Flags().Changed("verbose") {
 		switch strings.ToLower(Cfg.Log.Level) {
 		case "debug":
 			debug = true
 		case "info":
 			verbose = true
+		case "warn":
+			// default, no action needed
+		default:
+			log.Warnf("Unknown log.level %q in config, using default (warn)", Cfg.Log.Level)
 		}
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/root.go` around lines 59 - 73, Replace the implicit check "!debug &&
!verbose" when seeding Cfg.Log.Level with explicit CLI-flag checks using
cmd.Flags().Changed("debug") and cmd.Flags().Changed("verbose") so config only
wins when those flags were not provided (i.e., if !cmd.Flags().Changed("debug")
&& !cmd.Flags().Changed("verbose") then apply Cfg.Log.Level to set
debug/verbose); also add a default case for the switch on Cfg.Log.Level that
emits a warning (e.g., logger.Warnf or similar) when an unrecognized level like
"trace" is encountered to improve discoverability while still falling back to
the default behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@cmd/root.go`:
- Around line 59-73: Replace the implicit check "!debug && !verbose" when
seeding Cfg.Log.Level with explicit CLI-flag checks using
cmd.Flags().Changed("debug") and cmd.Flags().Changed("verbose") so config only
wins when those flags were not provided (i.e., if !cmd.Flags().Changed("debug")
&& !cmd.Flags().Changed("verbose") then apply Cfg.Log.Level to set
debug/verbose); also add a default case for the switch on Cfg.Log.Level that
emits a warning (e.g., logger.Warnf or similar) when an unrecognized level like
"trace" is encountered to improve discoverability while still falling back to
the default behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: df110b21-223d-4dac-badb-1ea122012a2c

📥 Commits

Reviewing files that changed from the base of the PR and between 38d54b4 and 7b08a4e.

📒 Files selected for processing (2)
  • cmd/root.go
  • config/config.go

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 7, 2026
…own level

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
cmd/root.go (1)

59-77: Use local effective logging values instead of mutating the flag-backed globals.

This block turns debug, verbose, logFile, and logFormat into merged runtime state. That makes later diagnostics ambiguous — e.g. Line 99 reports invalid --log-format even when the bad value came from settings.yaml — and makes repeated LoadConfig() calls depend on those globals being reset elsewhere. Prefer local effectiveLogLevel / effectiveLogFile / effectiveLogFormat values derived from flags + config, then apply them below.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/root.go` around lines 59 - 77, Do not mutate the flag-backed globals
(debug, verbose, logFile, logFormat) when seeding from Cfg.Log; instead compute
local effective values (e.g.
effectiveDebug/effectiveVerbose/effectiveLogFile/effectiveLogFormat) by checking
Cfg.Log.* together with
cmd.Flags().Changed("debug"|"verbose"|"log-file"|"log-format"), use
strings.ToLower(Cfg.Log.Level) to set effectiveDebug/effectiveVerbose (handle
"debug"/"info"/"warn"/"warning" and log.Warnf for unknown levels), and then
apply those effective variables where logging is initialized later so
diagnostics (e.g. the invalid --log-format check) use the effective* values
without altering the original flag-backed globals.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@cmd/root.go`:
- Around line 59-77: Do not mutate the flag-backed globals (debug, verbose,
logFile, logFormat) when seeding from Cfg.Log; instead compute local effective
values (e.g.
effectiveDebug/effectiveVerbose/effectiveLogFile/effectiveLogFormat) by checking
Cfg.Log.* together with
cmd.Flags().Changed("debug"|"verbose"|"log-file"|"log-format"), use
strings.ToLower(Cfg.Log.Level) to set effectiveDebug/effectiveVerbose (handle
"debug"/"info"/"warn"/"warning" and log.Warnf for unknown levels), and then
apply those effective variables where logging is initialized later so
diagnostics (e.g. the invalid --log-format check) use the effective* values
without altering the original flag-backed globals.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c686c0cd-3ff5-49e7-898f-889ee44f04a8

📥 Commits

Reviewing files that changed from the base of the PR and between 7b08a4e and bf01eec.

📒 Files selected for processing (1)
  • cmd/root.go

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 7, 2026
Seed log settings from config into local effectiveDebug/effectiveVerbose/
effectiveLogFile/effectiveLogFormat variables rather than mutating the
cobra flag-backed globals, so CLI flag state remains unaltered.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@cmd/root.go`:
- Around line 66-76: The current switch on Cfg.Log.Level calls log.Warnf
immediately for unknown values; instead capture that warning text into a local
variable (e.g., unknownLogLevelMsg) inside the existing conditional/switch that
sets effectiveDebug/effectiveVerbose and do not call log.Warnf there, then after
calling setAllLogFormatters(...) and setAllLogOutputs(...) check if
unknownLogLevelMsg is non-empty and emit the warning with
log.Warnf(unknownLogLevelMsg) so the message uses the configured format/output;
reference the existing symbols Cfg.Log.Level, effectiveDebug, effectiveVerbose,
the switch that currently calls log.Warnf, and the functions setAllLogFormatters
and setAllLogOutputs.
- Around line 99-105: The default error message in the switch over
effectiveLogFormat incorrectly references the CLI flag name; update the error
returned in the default branch (the switch handling effectiveLogFormat) to be
source-agnostic — e.g., say "invalid log format %q: must be 'text' or 'json'" —
so it doesn't assume the value came from --log-format; change the string
produced where the switch currently returns fmt.Errorf("invalid --log-format %q:
must be 'text' or 'json'", effectiveLogFormat) to a neutral message using
effectiveLogFormat.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2152d09a-99cb-42e8-a53f-b02ba81dd486

📥 Commits

Reviewing files that changed from the base of the PR and between bf01eec and 6813fb8.

📒 Files selected for processing (1)
  • cmd/root.go

…figured

Also make the invalid log format error message source-agnostic
("invalid log format" rather than "invalid --log-format") since
the value may come from config rather than a CLI flag.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@robbiet480 robbiet480 merged commit 7a1d882 into main Mar 7, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant