Skip to content

refactor(logger): extract syncFileWithWarning to eliminate duplicated sync boilerplate#8504

Merged
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-resolution
Jul 2, 2026
Merged

refactor(logger): extract syncFileWithWarning to eliminate duplicated sync boilerplate#8504
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-resolution

Conversation

Copilot AI commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

FileLogger.Log() and ServerFileLogger.Log() both inlined the same three-step pattern: call file.Sync(), and on failure emit log.Printf("WARNING: Failed to sync log file..."). Any future change to sync behavior (throttling, retry, warning format) had to be applied twice.

Changes

  • internal/logger/global_helpers.go — adds syncFileWithWarning(file *os.File, context string): nil-guards the file, syncs it, and logs a warning to stderr on failure. The context parameter (e.g. " for server github") lets both call sites produce their original distinct warning messages.
  • internal/logger/file_logger.go — replaces the inline if logFile != nil { Sync/warn } block with syncFileWithWarning(fl.logFile, "").
  • internal/logger/server_file_logger.go — replaces the inline if file, exists := sfl.files[serverID]; exists { Sync/warn } block with syncFileWithWarning(sfl.files[serverID], " for server "+serverID). Go's zero-value map lookup (nil for a missing *os.File) is handled by the helper's nil guard, preserving identical behaviour.

GitHub Advanced Security started work on behalf of lpcox July 2, 2026 22:44 View session
GitHub Advanced Security finished work on behalf of lpcox July 2, 2026 22:45
GitHub Advanced Security started work on behalf of lpcox July 2, 2026 22:48 View session
Copilot AI changed the title [WIP] Refactor duplicate logging code in FileLogger and ServerFileLogger refactor(logger): extract syncFileWithWarning to eliminate duplicated sync boilerplate Jul 2, 2026
Copilot AI requested a review from lpcox July 2, 2026 22:48
Copilot finished work on behalf of lpcox July 2, 2026 22:48
GitHub Advanced Security finished work on behalf of lpcox July 2, 2026 22:48
@lpcox lpcox marked this pull request as ready for review July 2, 2026 22:51
Copilot AI review requested due to automatic review settings July 2, 2026 22:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the logger package to remove duplicated “sync log file + warn on failure” boilerplate by introducing a shared helper used by both the unified file logger and the per-server file logger.

Changes:

  • Added syncFileWithWarning(file *os.File, context string) helper to centralize file sync + warning emission and to nil-guard missing files.
  • Updated FileLogger.Log() to use the helper instead of inlining the sync/warn block.
  • Updated ServerFileLogger.Log() to use the helper, preserving the existing per-server warning context.
Show a summary per file
File Description
internal/logger/global_helpers.go Adds the shared syncFileWithWarning helper (nil-guard + sync + stderr warning) to centralize behavior.
internal/logger/file_logger.go Replaces inline sync/warn logic with syncFileWithWarning(fl.logFile, "").
internal/logger/server_file_logger.go Replaces inline sync/warn logic with syncFileWithWarning(sfl.files[serverID], " for server "+serverID).

Review details

Tip

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

  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Low

@lpcox lpcox merged commit 2d1298a into main Jul 2, 2026
37 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-resolution branch July 2, 2026 22:54
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.

[duplicate-code] Duplicate Code Pattern: FileLogger and ServerFileLogger Sync Boilerplate

3 participants