Commit ad2b3373 authored by Markus Klein's avatar Markus Klein Committed by Helmut Hummel
Browse files

[FEATURE] Inject logger into newly created objects

Instantiating new objects with makeInstance or the ObjectManager
will automatically inject a logger into the new objects, if those
implement the LoggerAwareInterface.

Resolves: #82441
Releases: master
Change-Id: I38e1a4619857f3fc2d88b0b95a9721b289c679d3
Reviewed-on: https://review.typo3.org/54101


Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: default avatarStefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: default avatarStefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Helmut Hummel's avatarHelmut Hummel <typo3@helhum.io>
Tested-by: Helmut Hummel's avatarHelmut Hummel <typo3@helhum.io>
parent 71950c54
......@@ -15,10 +15,12 @@ namespace TYPO3\CMS\Core\Utility;
*/
use GuzzleHttp\Exception\RequestException;
use Psr\Log\LoggerAwareInterface;
use TYPO3\CMS\Core\Core\ApplicationContext;
use TYPO3\CMS\Core\Core\ClassLoadingInformation;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Service\OpcodeCacheService;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
......@@ -3488,6 +3490,9 @@ class GeneralUtility
if ($instance instanceof SingletonInterface) {
self::$singletonInstances[$finalClassName] = $instance;
}
if ($instance instanceof LoggerAwareInterface) {
$instance->setLogger(static::makeInstance(LogManager::class)->getLogger($className));
}
return $instance;
}
......
.. include:: ../../Includes.txt
=====================================================
Feature: #82441 - Inject logger when creating objects
=====================================================
See :issue:`82441`
Description
===========
Classes that implement :php:`\Psr\Log\LoggerAwareInterface` automatically
get a logger instance injected when a class instance is created via
:php:`GeneralUtility::makeInstance()` and :php:`ObjectManger::get()`.
For developer convenience the :php:`\Psr\Log\LoggerAwareTrait` can be used.
The trait adds a public :php:`setLogger()` and a protected :php:`$logger` property
to the class, no further code is needed to successfully implement the interface.
A minimal example looks like this (example from a test case fixture):
.. code-block:: php
<?php
declare(strict_types=1);
namespace TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
class GeneralUtilityMakeInstanceInjectLoggerFixture implements LoggerAwareInterface
{
use LoggerAwareTrait;
}
.. index:: PHP-API
\ No newline at end of file
<?php
declare(strict_types=1);
namespace TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
/**
* Fixture for a class which gets logger injected
*/
class GeneralUtilityMakeInstanceInjectLoggerFixture implements LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* @return LoggerInterface
*/
public function getLogger(): LoggerInterface
{
return $this->logger;
}
}
......@@ -17,11 +17,13 @@ namespace TYPO3\CMS\Core\Tests\Unit\Utility;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamWrapper;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Package\Package;
use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFilesystemFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityMakeInstanceInjectLoggerFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\OriginalClassFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\OtherReplacementClassFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\ReplacementClassFixture;
......@@ -3851,6 +3853,15 @@ class GeneralUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
$this->assertNotSame($instance, GeneralUtility::makeInstance($className));
}
/**
* @test
*/
public function makeInstanceInjectsLogger()
{
$instance = GeneralUtility::makeInstance(GeneralUtilityMakeInstanceInjectLoggerFixture::class);
$this->assertInstanceOf(LoggerInterface::class, $instance->getLogger());
}
/**
* @test
*/
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment