sabbelasichon / typo3-rector

Rector for TYPO3
MIT License
231 stars 63 forks source link

Feature: #95079 - Support PHP 8 style Channel attribute for logger injection #4440

Closed simonschaufi closed 3 weeks ago

simonschaufi commented 3 weeks ago

Feature: #95079 - Support PHP 8 style Channel attribute for logger injection

https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/11.4/Feature-95079-SupportPHP8StyleChannelAttributeForLoggerInjection.html

Feature: #95079 - Support PHP 8 style Channel attribute for logger injection

See 95079

Description

Services are now able to control the component name that an injected logger is created with. This allows to group logs of related classes and is basically a channel system as often used in monolog.

The \TYPO3\CMS\Core\Log\Channel attribute is supported for constructor argument injection as a class and parameter specific attribute and for \Psr\Log\LoggerAwareInterface dependency injection services as a class attribute.

This feature is only available with PHP 8. The channel attribute will be gracefully ignored in PHP 7, and the classic component name will be used instead.

Registration via class attribute for \Psr\Log\LoggerInterface injection:

use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Log\Channel;
#[Channel('security')]
class MyClass
{
    private LoggerInterface $logger;
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
        // do your magic
    }
}

Registration via parameter attribute for LoggerInterface injection, overwrites possible class attributes:

use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Log\Channel;
class MyClass
{
    private LoggerInterface $logger;
    public function __construct(
        #[Channel('security')]
        LoggerInterface $logger
    ) {
        $this->logger = $logger;
        // do your magic
    }
}

Registration via class attribute for LoggerAwareInterface services.

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use TYPO3\CMS\Core\Log\Channel;
#[Channel('security')]
class MyClass implements LoggerAwareInterface
{
    use LoggerAwareTrait;
}

Impact

It is now possible to group several classes into channels, regardless of the PHP namespace.

PHP-API, ext:core