Skip to content

Conversation

butschster
Copy link
Collaborator

This PR introduces a more elegant approach to logger prefix management by leveraging PHP attributes. Instead of manually configuring logger prefixes in bootloaders, we now use a declarative approach with attributes.

Key Improvements:

  1. Attribute-Based Logger Naming:

    • Added LoggerPrefix attribute that can be applied to parameters, properties or classes
    • Simplified code by removing redundant logger prefix assignments in bootloaders
  2. Automatic DI Resolution:

    • Enhanced LoggerBootloader to implement InjectorInterface
    • Automatically detects and applies prefixes during dependency injection
    • Extracts prefix from attribute or falls back to class name
  3. Consistent Logger Prefixing:

    • Applied LoggerPrefix attributes consistently across all services
    • Classes now explicitly declare their preferred logger prefix
    • Unified prefix naming convention throughout the codebase
  4. Cleaner Bootloader Implementation:

    • Removed repetitive withPrefix() calls from bootloaders
    • Factory-based instantiation for cleanly passing dependencies
    • Each component now declares its own logging requirements

This approach significantly reduces boilerplate code while making logger prefix configuration more transparent and maintainable. Services now self-document their logging requirements directly at the class or parameter level.

Example: Before and After

Before this PR:

// In Bootloader
public function defineSingletons(): array
{
    return [
        ImportSourceProvider::class => static fn(
            ImportSourceRegistry $sourceRegistry,
            HasPrefixLoggerInterface $logger,
        ) => new ImportSourceProvider(
            sourceRegistry: $sourceRegistry,
            logger: $logger->withPrefix('import-sources'),
        ),
        
        UrlImportSource::class => static fn(
            HttpClientInterface $httpClient,
            VariableResolver $variables,
            HasPrefixLoggerInterface $logger,
        ) => new UrlImportSource(
            httpClient: $httpClient,
            variables: $variables,
            logger: $logger->withPrefix('import-source-url'),
        ),
        
        // ...dozens more similar declarations
    ];
}

// In Service class
public function __construct(
    private ImportSourceRegistry $sourceRegistry,
    private ?LoggerInterface $logger = null,
) {}

After this PR:

// In Bootloader - much cleaner!
public function defineSingletons(): array
{
    return [
        ImportSourceProvider::class => ImportSourceProvider::class,
        UrlImportSource::class => UrlImportSource::class,
    ];
}

// In Service class - self-documenting logger prefix
public function __construct(
    private ImportSourceRegistry $sourceRegistry,
    #[LoggerPrefix(prefix: 'import-sources')]
    private ?LoggerInterface $logger = null,
) {}

// Or simply at class level
#[LoggerPrefix(prefix: 'import-source-url')]
final class UrlImportSource extends AbstractImportSource
{
    // No need to specify prefix in constructor
    public function __construct(
        private HttpClientInterface $httpClient,
        private VariableResolver $variables,
        private ?LoggerInterface $logger = null,
    ) {}
}

- Add `LoggerPrefix` attribute for declarative logger naming
- Implement automatic logger prefix injection during DI resolution
- Simplify bootloader code by removing redundant logger configuration
- Apply attribute-based logger prefixing across the entire codebase
@butschster butschster self-assigned this Apr 6, 2025
@butschster butschster added type:enhancement New feature or request type:refactoring Code cleanup and refactoring tasks labels Apr 6, 2025
@butschster butschster added this to the 1.x milestone Apr 6, 2025
@butschster butschster moved this to In review in Context Generator Apr 6, 2025
@butschster butschster merged commit 2021d8a into main Apr 6, 2025
8 checks passed
@butschster butschster deleted the feature/injectable-logger branch April 6, 2025 08:51
@github-project-automation github-project-automation bot moved this from In review to Done in Context Generator Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement New feature or request type:refactoring Code cleanup and refactoring tasks
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

1 participant