Skip to content

feat: add OutputFile option for log destination in logger#152

Open
MyMirelHub wants to merge 4 commits intodapr:mainfrom
MyMirelHub:logfile-out
Open

feat: add OutputFile option for log destination in logger#152
MyMirelHub wants to merge 4 commits intodapr:mainfrom
MyMirelHub:logfile-out

Conversation

@MyMirelHub
Copy link
Copy Markdown

@MyMirelHub MyMirelHub commented Mar 25, 2026

Description

This PR adds support for writing logger output to a file.

Changes made:

  • Added OutputFile to logger options and initialized it in defaults.
  • Added a new --log-file CLI flag in AttachCmdFlags.
  • Updated ApplyOptionsToLoggers to open the configured file and route logger output to it.
  • Added tests for default option values, flag registration, and file-output behavior.

Issue reference

We strive to have all PR being opened based on an issue, where the problem or feature have been discussed prior to implementation.

Please reference the issue this PR will close: #[issue number]

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests

Signed-off-by: MyMirelHub <15373565+MyMirelHub@users.noreply.github.com>
Signed-off-by: MyMirelHub <15373565+MyMirelHub@users.noreply.github.com>
Signed-off-by: MyMirelHub <15373565+MyMirelHub@users.noreply.github.com>
Signed-off-by: MyMirelHub <15373565+MyMirelHub@users.noreply.github.com>
@MyMirelHub MyMirelHub marked this pull request as ready for review March 31, 2026 20:25
@MyMirelHub MyMirelHub requested review from a team as code owners March 31, 2026 20:25
@JoshVanL JoshVanL requested a review from Copilot April 1, 2026 12:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds support for directing logger output to a user-specified file via a new option/flag, and validates the behavior with unit tests.

Changes:

  • Introduces Options.OutputFile and registers a new --log-file CLI flag.
  • Updates ApplyOptionsToLoggers to open the configured file and route logger output to it.
  • Adds tests covering default values, flag registration, and file-output behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
logger/options.go Adds OutputFile option + --log-file flag and applies file output in ApplyOptionsToLoggers.
logger/options_test.go Extends options/flag tests and adds a test that verifies logs are written to the configured file.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +117 to +126
if options.OutputFile != "" {
file, err := os.OpenFile(options.OutputFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
if err != nil {
return fmt.Errorf("failed to open log file %q: %w", options.OutputFile, err)
}

for _, v := range internalLoggers {
v.SetOutput(file)
}
}
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

ApplyOptionsToLoggers opens the configured log file and sets it as output for all loggers, but the file handle is never tracked/closed. If options are re-applied (eg during a reload) this will leak file descriptors, and clearing/changing OutputFile won’t revert outputs or close the previous file. Consider storing the current output writer/file in package-level state (protected by a mutex), closing/replacing it on subsequent calls, and explicitly resetting output back to the default (stdout) when OutputFile is empty.

Copilot uses AI. Check for mistakes.
}

if options.OutputFile != "" {
file, err := os.OpenFile(options.OutputFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we relax permissions a bit and allow reads?

Suggested change
file, err := os.OpenFile(options.OutputFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
file, err := os.OpenFile(options.OutputFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)

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.

3 participants