Skip to content

refactor(logger): eliminate logger level factory duplication with generics#6828

Merged
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-logger-functions
Jun 1, 2026
Merged

refactor(logger): eliminate logger level factory duplication with generics#6828
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-logger-functions

Conversation

Copilot AI commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

internal/logger/common.go had two structurally identical function pairs (makeLevelLogger/makeServerLevelLogger and newLevelLoggerFuncs/newServerLevelLoggerFuncs) plus two mirrored struct types (levelLoggerFuncs/serverLevelLoggerFuncs), differing only in the presence of a serverID string parameter. Adding a new log level required changes in four places instead of two.

Changes

  • logFuncSet[F any] — new generic struct replacing both levelLoggerFuncs and serverLevelLoggerFuncs; holds the info/warn/error/debug quad for any function signature F
  • newLogFuncSet[F any] — new generic factory replacing both new*LevelLoggerFuncs bodies; calls makeFunc(level) once per level
  • Type aliaseslevelLoggerFuncs and serverLevelLoggerFuncs become aliases for their respective logFuncSet instantiations; no call-site changes needed in file_logger.go, markdown_logger.go, or server_file_logger.go
  • makeLevelLogger/makeServerLevelLogger removed — inlined into the two thin new* wrappers that now delegate to newLogFuncSet
// Before: two parallel structs + two parallel make* functions + two parallel new* functions

// After: one generic struct + one generic factory
type logFuncSet[F any] struct { info, warn, error, debug F }

func newLogFuncSet[F any](makeFunc func(LogLevel) F) logFuncSet[F] {
    return logFuncSet[F]{
        info:  makeFunc(LogLevelInfo),
        warn:  makeFunc(LogLevelWarn),
        error: makeFunc(LogLevelError),
        debug: makeFunc(LogLevelDebug),
    }
}

// Type aliases preserve existing call sites unchanged
type levelLoggerFuncs       = logFuncSet[func(string, string, ...interface{})]
type serverLevelLoggerFuncs = logFuncSet[func(string, string, string, ...interface{})]

A new log level now requires a single entry in newLogFuncSet instead of four.

Replace the four structurally-identical functions (makeLevelLogger,
makeServerLevelLogger, newLevelLoggerFuncs, newServerLevelLoggerFuncs)
and the two mirrored struct types (levelLoggerFuncs, serverLevelLoggerFuncs)
with:

- logFuncSet[F any] — a single generic struct that holds the
  info/warn/error/debug quad for any function signature F.
- newLogFuncSet[F any] — a single generic factory that wires all four
  levels, eliminating the repeated per-level initialisation block.
- levelLoggerFuncs and serverLevelLoggerFuncs are now type aliases for
  logFuncSet instantiated with their respective concrete signatures, so
  no call sites in file_logger.go, markdown_logger.go, or
  server_file_logger.go need to change.

Adding a new log level now requires updating only one place
(newLogFuncSet) instead of four.

Closes #<issue>
Copilot AI changed the title [WIP] Refactor duplicate logger level function patterns refactor(logger): eliminate logger level factory duplication with generics Jun 1, 2026
Copilot finished work on behalf of lpcox June 1, 2026 03:51
Copilot AI requested a review from lpcox June 1, 2026 03:51
@lpcox lpcox marked this pull request as ready for review June 1, 2026 04:16
Copilot AI review requested due to automatic review settings June 1, 2026 04:16

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 internal logger’s per-level closure wiring to remove duplicated factory/struct patterns by introducing a single generic logFuncSet + newLogFuncSet, while keeping existing call sites unchanged via type aliases.

Changes:

  • Introduced generic logFuncSet[F any] to represent the per-level (info/warn/error/debug) function bundle for any signature.
  • Added newLogFuncSet generic factory and rewired newLevelLoggerFuncs / newServerLevelLoggerFuncs to delegate to it (removing the duplicated make* helpers).
  • Converted levelLoggerFuncs / serverLevelLoggerFuncs to type aliases over logFuncSet[...] so downstream loggers don’t need updates.
Show a summary per file
File Description
internal/logger/common.go Consolidates per-level logger closure construction using generics and type aliases to eliminate duplicated implementations.

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/common.go
Comment on lines +235 to +237
// logFuncSet is a generic bundle of per-level logging closures all sharing the
// same function signature F. It is the single source of truth for the
// info/warn/error/debug quad used by every logger variant.
@lpcox

lpcox commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

@copilot address review feedback

Copilot AI commented Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

@copilot address review feedback

Fixed in 0a2ac79. Updated the stale guidance in internal/logger/common.go to reference newLogFuncSet as the centralized place to update per-level bindings.

Copilot finished work on behalf of lpcox June 1, 2026 04:27
@lpcox lpcox merged commit 53d57c7 into main Jun 1, 2026
16 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-logger-functions branch June 1, 2026 04:33
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: Logger Level Function Structural Duplication

3 participants