Refactor logger initialization with generic function to eliminate duplication#448
Merged
Refactor logger initialization with generic function to eliminate duplication#448
Conversation
- Add generic initLogger function in common.go - Refactor InitFileLogger to use generic function - Refactor InitJSONLLogger to use generic function - Refactor InitMarkdownLogger to use generic function - Add comprehensive tests for generic initLogger function - Reduce ~60-75 lines of duplicate boilerplate code Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Address code review feedback: simplify the error check in InitJSONLLogger to be clearer and only initialize the global logger on success. Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
- Explain zero value pattern for generic type T - Clarify why JSONLLogger has conditional initialization - Address code review feedback Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor logger initialization to reduce boilerplate code
Refactor logger initialization with generic function to eliminate duplication
Jan 23, 2026
This was referenced Jan 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Three logger initialization functions (
InitFileLogger,InitJSONLLogger,InitMarkdownLogger) contained ~60-75 lines of duplicated boilerplate with only minor variations in error handling and field setup.Changes
Added generic
initLoggerfunction (internal/logger/common.go)closableLoggerconstraintRefactored Init functions to use generic
InitFileLogger: Error handler returns fallback logger (stdout)InitJSONLLogger: Error handler returnsnil, err(fail-fast, conditional global init)InitMarkdownLogger: Error handler returns fallback logger (no stdout redirect)Added tests (
internal/logger/common_test.go)Example
Before (duplicated across 3 files):
After (behavior parameterized):
Impact
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
go.googlesource.comOriginal prompt
This section details on the original issue you should resolve
<issue_title>[duplicate-code] Duplicate Code Pattern: Logger Initialization Boilerplate</issue_title>
<issue_description># 🔍 Duplicate Code Pattern: Logger Initialization Boilerplate
Part of duplicate code analysis: #435
Summary
The three logger initialization functions (
InitFileLogger,InitJSONLLogger,InitMarkdownLogger) follow nearly identical structural patterns with only minor variations in error handling and field setup. This creates ~60-75 lines of duplicated boilerplate code across the logger package.Duplication Details
Pattern: Logger Initialization Functions
internal/logger/file_logger.go(lines 30-54, 25 lines)internal/logger/jsonl_logger.go(lines 39-56, 18 lines)internal/logger/markdown_logger.go(lines 28-48, 21 lines)Code Structure (All Three Functions):
Specific Differences:
FileLogger (25 lines):
os.O_APPENDlog.LoggerwrapperJSONLLogger (18 lines):
os.O_APPENDMarkdownLogger (21 lines):
os.O_TRUNCinitializedflag to falseImpact Analysis
Maintainability
Bug Risk
Code Bloat
Refactoring Recommendations
Option 1: Functional Options Pattern (Recommended)
Extract common initialization logic with customization via options:
Benefits:
Estimated effort: 2-3 hours
Option 2: Template Method with Interfaces
Define an interface for logger initialization and use a base implementation:
Benefits:
Drawbacks:
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.