Skip to content

refactor(logger): extract emit helper to eliminate Printf/Print duplication#7936

Merged
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-remove-identical-logic
Jun 22, 2026
Merged

refactor(logger): extract emit helper to eliminate Printf/Print duplication#7936
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-remove-identical-logic

Conversation

Copilot AI commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Logger.Printf and Logger.Print in internal/logger/logger.go shared ~20 lines of identical logic (mutex/time-diff, stderr output, file-logger write), differing only in how the message string was assembled.

Changes

  • internal/logger/logger.go: Extract private emit(message string) that owns the shared block; Printf and Print now each check l.enabled then delegate with their respective format call:
func (l *Logger) emit(message string) {
    l.mu.Lock()
    now := time.Now()
    diff := now.Sub(l.lastLog)
    l.lastLog = now
    l.mu.Unlock()
    if l.color != "" {
        fmt.Fprintf(os.Stderr, "%s%s%s %s +%s\n", l.color, l.namespace, colorReset, message, strutil.FormatDuration(diff))
    } else {
        fmt.Fprintf(os.Stderr, "%s %s +%s\n", l.namespace, message, strutil.FormatDuration(diff))
    }
    LogDebug(l.namespace, "%s", message)
}

func (l *Logger) Printf(format string, args ...any) {
    if !l.enabled { return }
    l.emit(fmt.Sprintf(format, args...))
}

func (l *Logger) Print(args ...any) {
    if !l.enabled { return }
    l.emit(fmt.Sprint(args...))
}

Any future variants (Println, etc.) are now trivial to add correctly with a single emit call.

GitHub Advanced Security started work on behalf of lpcox June 22, 2026 17:04 View session
GitHub Advanced Security finished work on behalf of lpcox June 22, 2026 17:04
Copilot AI changed the title [WIP] Refactor duplicate logger methods in internal package refactor(logger): extract emit helper to eliminate Printf/Print duplication Jun 22, 2026
Copilot AI requested a review from lpcox June 22, 2026 17:07
Copilot finished work on behalf of lpcox June 22, 2026 17:07
GitHub Advanced Security started work on behalf of lpcox June 22, 2026 17:11 View session
GitHub Advanced Security finished work on behalf of lpcox June 22, 2026 17:12
@lpcox lpcox marked this pull request as ready for review June 22, 2026 17:20
Copilot AI review requested due to automatic review settings June 22, 2026 17:20

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

Refactors the debug Logger implementation to reduce duplicated logic between Printf and Print by extracting a shared internal helper, improving maintainability of stderr + file-logger emission behavior.

Changes:

  • Extracts a private (*Logger).emit(message string) helper for the common “mutex/time-diff + stderr output + file logger write” block.
  • Updates Logger.Printf and Logger.Print to early-return when disabled and delegate to emit with their respective message construction.
Show a summary per file
File Description
internal/logger/logger.go Deduplicates Printf/Print by extracting shared emission logic into emit.

Copilot's findings

Tip

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

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread internal/logger/logger.go
Comment on lines +131 to +136
func (l *Logger) Printf(format string, args ...any) {
if !l.enabled {
return
}
l.mu.Lock()
now := time.Now()
diff := now.Sub(l.lastLog)
l.lastLog = now
l.mu.Unlock()

message := fmt.Sprint(args...)
l.emit(fmt.Sprintf(format, args...))
}
@lpcox

lpcox commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

@copilot address review feedback

GitHub Advanced Security started work on behalf of lpcox June 22, 2026 18:48 View session
GitHub Advanced Security finished work on behalf of lpcox June 22, 2026 18:48

Copilot AI commented Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

@copilot address review feedback

Addressed in b6827db. The logger now captures the time diff before formatting, preserving the original +<diff> behavior, and I added a regression test covering slow formatting for both Print and Printf.

Copilot finished work on behalf of lpcox June 22, 2026 18:50
@lpcox lpcox merged commit edd8ceb into main Jun 22, 2026
27 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-remove-identical-logic branch June 22, 2026 19:00
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