Part of duplicate code analysis: #1765
Summary
Five logger initialization functions (InitFileLogger, InitMarkdownLogger, InitJSONLLogger, InitServerFileLogger, InitToolsLogger) follow an identical structural boilerplate pattern, resulting in approximately 120 lines of duplicated initialization logic spread across 5 files.
Duplication Details
Pattern: Logger Init* Function Boilerplate
func InitXxxLogger(logDir string) error {
logger, err := initLogger(
logDir, "filename.ext", flagType,
func(file *os.File, logDir, fileName string) (*XxxLogger, error) {
// Initialize struct, create writer, configure
return &XxxLogger{...}, nil
},
func(err error, logDir, fileName string) (*XxxLogger, error) {
// Fallback to stdout/stderr
return &XxxLogger{...}, fallbackErr
},
)
initGlobalXxxLogger(logger)
return err
}
Note: A generic initLogger[T]() function exists in internal/logger/common.go (lines 318–339), which was added to reduce duplication, but the wrapper Init* functions still replicate identical structural patterns.
Impact Analysis
- Maintainability: Any change to the initialization pattern (e.g., adding a new constructor parameter, changing fallback behavior) must be replicated across all 5
Init* functions.
- Bug Risk: Medium — inconsistent fallback behavior could be introduced when one file is updated but not others.
- Code Bloat: ~120 lines of near-identical code across 5 files.
Refactoring Recommendations
-
Register Logger Factories
- Create a
LoggerFactory interface and register each logger type at startup
- The
initLogger[T]() generic is already in place; wrap it with a unified registration system
- Estimated effort: Medium (2–4 hours)
- Benefits: Single initialization call path; adding new logger types requires only factory registration
-
Shared Init Helper with Interface
- Define a
LoggerInitializer interface with Setup(file *os.File, logDir, fileName string) error and Fallback(err error) error
- Each logger implements this interface;
InitLogger(dir string, impl LoggerInitializer) handles the common flow
- Estimated effort: Low-Medium (1–2 hours)
Implementation Checklist
Parent Issue
See parent analysis report: #1765
Related to #1765
Generated by Duplicate Code Detector · ◷
Part of duplicate code analysis: #1765
Summary
Five logger initialization functions (
InitFileLogger,InitMarkdownLogger,InitJSONLLogger,InitServerFileLogger,InitToolsLogger) follow an identical structural boilerplate pattern, resulting in approximately 120 lines of duplicated initialization logic spread across 5 files.Duplication Details
Pattern: Logger
Init*Function BoilerplateSeverity: Medium
Occurrences: 5 instances
Locations:
internal/logger/file_logger.go(lines 30–60):InitFileLogger()internal/logger/markdown_logger.go(lines 28–54):InitMarkdownLogger()internal/logger/jsonl_logger.go(lines 41–68):InitJSONLLogger()internal/logger/server_file_logger.go(lines 27–53):InitServerFileLogger()internal/logger/tools_logger.go(lines 44–82):InitToolsLogger()Code Sample (structural pattern repeated in each
Init*function):Note: A generic
initLogger[T]()function exists ininternal/logger/common.go(lines 318–339), which was added to reduce duplication, but the wrapperInit*functions still replicate identical structural patterns.Impact Analysis
Init*functions.Refactoring Recommendations
Register Logger Factories
LoggerFactoryinterface and register each logger type at startupinitLogger[T]()generic is already in place; wrap it with a unified registration systemShared Init Helper with Interface
LoggerInitializerinterface withSetup(file *os.File, logDir, fileName string) errorandFallback(err error) errorInitLogger(dir string, impl LoggerInitializer)handles the common flowImplementation Checklist
Init*functions to use shared helperParent Issue
See parent analysis report: #1765
Related to #1765