Skip to content

[duplicate-code] Duplicate Code Pattern: CloseXxxLogger global function across 5 logger files #3688

@github-actions

Description

@github-actions

Part of duplicate code analysis: #3685

Summary

Every logger type exposes a package-level CloseXxxLogger() function that is a single-line wrapper around closeGlobalLogger(...). Five such functions exist, each body being structurally identical.

Duplication Details

Pattern: CloseXxxLogger single-line global close functions

  • Severity: Low
  • Occurrences: 5
  • Locations:
    • internal/logger/file_logger.go:155CloseGlobalLogger()
    • internal/logger/jsonl_logger.go:109CloseJSONLLogger()
    • internal/logger/markdown_logger.go:217CloseMarkdownLogger()
    • internal/logger/tools_logger.go:151CloseToolsLogger()
    • internal/logger/server_file_logger.go:179CloseServerFileLogger()
  • Code Sample:
    // CloseXxxLogger closes the global Xxx logger
    func CloseXxxLogger() error {
        return closeGlobalLogger(&globalXxxMu, &globalXxxLogger)
    }

Impact Analysis

  • Maintainability: Adding a new logger type requires remembering to add a CloseXxxLogger function; there is no central registry that ensures all loggers are closed.
  • Bug Risk: Low for existing loggers. Medium risk that a new logger's Close function is omitted from the shutdown sequence in cmd/root.go.
  • Code Bloat: ~15 lines of near-identical boilerplate.

Refactoring Recommendations

  1. Introduce a logger registry — Register each global logger at Init time into a slice; provide a single CloseAll() error function that iterates and closes them. Remove the 5 individual CloseXxxLogger functions (or keep them as thin aliases for backward compatibility).

    // global_helpers.go
    var globalLoggers []io.Closer
    
    func registerLogger(l io.Closer) { globalLoggers = append(globalLoggers, l) }
    
    func CloseAll() error {
        var firstErr error
        for _, l := range globalLoggers {
            if err := l.Close(); err != nil && firstErr == nil { firstErr = err }
        }
        return firstErr
    }
    • Estimated effort: ~1 hour
    • Benefits: New loggers automatically participate in shutdown; removes 5 near-identical functions; simplifies cmd/root.go shutdown sequence.
  2. Accept the pattern — The functions are trivial and adding a new logger is infrequent. Document that cmd/root.go must be updated whenever a new logger is added.

Implementation Checklist

  • Review duplication findings
  • Decide: introduce logger registry or document/accept
  • If refactoring: update cmd/root.go shutdown sequence to use CloseAll()
  • Run make agent-finished to verify no regressions

Parent Issue

See parent analysis report: #3685
Related to #3685

Generated by Duplicate Code Detector · ● 1.6M ·

  • expires on Apr 20, 2026, 6:16 AM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions