Conversation
- Add logWithLevel helper in file_logger.go to eliminate duplicate mutex patterns - Add logWithLevelAndServer helper in server_file_logger.go for per-server logging - Add logWithMarkdownLevel helper in markdown_logger.go to reduce repetition - Reduces code duplication from ~120 lines to ~60 lines across 3 files - All logger tests pass successfully - Pre-existing test failures in internal/launcher unrelated to these changes Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
- Add TestLogWithLevel to verify file logger helper - Add TestLogWithLevelAndServer to verify server logger helper with dual logging - Add TestLogWithMarkdownLevel to verify markdown logger helper - Add TestHelperFunctionsWithFormatting to test format string support - Add TestHelperFunctionsConcurrency to verify thread-safety - All new tests pass successfully Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
|
@claude[agent] please fix these linting errors make lint |
- Align emoji comments to consistent spacing - Fixes linting error reported in PR feedback Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Fixed in commit 2aa8f03. The file has been formatted with gofmt and all linting checks now pass. |
There was a problem hiding this comment.
Pull request overview
This PR addresses duplicate code patterns in the logger package by introducing inline helper functions that consolidate repeated mutex lock/unlock and level-specific dispatching patterns. The refactoring reduces code duplication by approximately 25% across three logger files while maintaining identical behavior and thread-safety guarantees.
Changes:
- Extracted
logWithLevelhelper infile_logger.goto eliminate duplicate mutex patterns across Info/Warn/Error/Debug functions - Extracted
logWithLevelAndServerhelper inserver_file_logger.goto consolidate dual logging (server-specific + unified) logic - Extracted
logWithMarkdownLevelhelper inmarkdown_logger.goto reduce duplication in markdown logging functions - Added comprehensive test coverage in
helper_functions_test.gocovering all helper functions, formatting, and concurrency
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/logger/file_logger.go | Introduces logWithLevel helper to eliminate duplicate mutex locking and nil-checking patterns across 4 logging functions (Info/Warn/Error/Debug) |
| internal/logger/server_file_logger.go | Introduces logWithLevelAndServer helper to consolidate dual logging logic (server-specific + unified) with switch-based level dispatching |
| internal/logger/markdown_logger.go | Introduces logWithMarkdownLevel helper to map log levels to their corresponding regular log functions, reducing duplication |
| internal/logger/helper_functions_test.go | Adds comprehensive test coverage for all three helper functions, including concurrency tests and formatting validation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| case LogLevelDebug: | ||
| regularLogFunc = LogDebug | ||
| } | ||
|
|
There was a problem hiding this comment.
The logWithMarkdownLevel helper function doesn't handle the case where the log level doesn't match any of the expected values. If regularLogFunc remains nil after the switch statement, calling it on line 203 will cause a nil pointer panic. Consider adding a default case that either uses a fallback logging function or returns early to prevent potential panics. While this shouldn't happen in practice with the current usage, defensive programming would make this more robust.
| // If no matching log function was found for the provided level, | |
| // return early to avoid calling a nil function and causing a panic. | |
| if regularLogFunc == nil { | |
| return | |
| } |
| case LogLevelError: | ||
| LogError(category, "[%s] %s", serverID, formattedMessage) | ||
| case LogLevelDebug: | ||
| LogDebug(category, "[%s] %s", serverID, formattedMessage) |
There was a problem hiding this comment.
The logWithLevelAndServer helper function doesn't handle the case where the log level doesn't match any of the expected values in the switch statement. If an unexpected log level is passed, the server-specific logging will still occur (line 169), but the unified logging will be silently skipped. Consider adding a default case that logs a warning or falls back to LogInfo to ensure logs are never silently dropped due to an unexpected log level value.
| LogDebug(category, "[%s] %s", serverID, formattedMessage) | |
| LogDebug(category, "[%s] %s", serverID, formattedMessage) | |
| default: | |
| // Fallback to a warning to avoid silently dropping unified logs on unexpected levels | |
| LogWarn(category, "[%s] %s (unexpected log level: %v)", serverID, formattedMessage, level) |
Original prompt
This section details on the original issue you should resolve
<issue_title>[duplicate-code] Duplicate Code Pattern: Logger Functions with Repeated Mutex Lock Patterns</issue_title>
<issue_description># 🔍 Duplicate Code Pattern: Logger Functions with Mutex Lock Patterns
Part of duplicate code analysis: #970
Summary
The logger package contains 12+ nearly identical functions with repeated mutex lock/unlock patterns across three files. Each set of 4 functions (Info/Warn/Error/Debug) follows the exact same structure, differing only in the log level parameter.
Duplication Details
Pattern: Repeated Logger Function Structure
internal/logger/file_logger.go- Lines 114-151 (4 functions: LogInfo, LogWarn, LogError, LogDebug)internal/logger/server_file_logger.go- Lines 162-211 (4 functions: LogInfoWithServer, LogWarnWithServer, LogErrorWithServer, LogDebugWithServer)internal/logger/markdown_logger.go- Lines 187-204 (4 functions: LogInfoMd, LogWarnMd, LogErrorMd, LogDebugMd)Code Sample - file_logger.go Pattern:
Code Sample - server_file_logger.go Pattern:
Impact Analysis
Refactoring Recommendations
1. Extract Generic Logger Helper Function (Recommended)
Extract common mutex lock/unlock and nil-check pattern into a generic helper:
internal/logger/file_logger.go(addlogWithLevelhelper)2. Apply Same Pattern to server_file_logger.go
internal/logger/server_file_logger.go3. Apply Same Pattern to markdown_logger.go
Similar refactoring for
LogInfoMd,LogWarnMd,LogErrorMd,LogDebugMd: