Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Introduce an utility to add log environment from method name
This utility can be used to easily add the log environment to log message using the PSR compatible logger. Example: ``` $logger->debug('Running sub process loop.', LogEnvironment::fromMethodName(__METHOD__)); ```
- Loading branch information
1 parent
ef04d42
commit e09f4bf
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
namespace Neos\Flow\Log\Utility; | ||
|
||
/* | ||
* This file is part of the Neos.Flow package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
|
||
use Neos\Flow\Core\Bootstrap; | ||
use Neos\Flow\Package\PackageInterface; | ||
use Neos\Flow\Package\PackageKeyAwareInterface; | ||
use Neos\Flow\Package\PackageManager; | ||
use Neos\Flow\Package\PackageManagerInterface; | ||
use Neos\Utility\Arrays; | ||
|
||
abstract class LogEnvironment | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected static $packageKeys = null; | ||
|
||
/** | ||
* Returns an array containing the log environment variables | ||
* under the key FLOW_LOG_ENVIRONMENT to be set as part of the additional data | ||
* in an log method call. | ||
* | ||
* @param string $methodName | ||
* @return array | ||
*/ | ||
public static function fromMethodName(string $methodName): array | ||
{ | ||
list($className, $functionName) = explode('::', $methodName); | ||
|
||
return [ | ||
'FLOW_LOG_ENVIRONMENT' => [ | ||
'packageKey' => self::getPackageKeyFromClassName($className), | ||
'className' => $className, | ||
'methodName' => $functionName | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $className | ||
* @return string | ||
*/ | ||
protected static function getPackageKeyFromClassName(string $className): string | ||
{ | ||
$packageKeys = self::getPackageKeys(); | ||
$classPathArray = explode('\\', $className); | ||
|
||
$determinedPackageKey = array_shift($classPathArray); | ||
$packageKeyCandidate = $determinedPackageKey; | ||
|
||
foreach ($classPathArray as $classPathSegment) { | ||
$packageKeyCandidate = $packageKeyCandidate . '.' . $classPathSegment; | ||
|
||
if (!isset($packageKeys[$packageKeyCandidate])) { | ||
continue; | ||
} | ||
|
||
$determinedPackageKey = $packageKeyCandidate; | ||
} | ||
|
||
return $determinedPackageKey; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected static function getPackageKeys(): array | ||
{ | ||
if (self::$packageKeys === null) { | ||
/** @var PackageManagerInterface $packageManager */ | ||
$packageManager = Bootstrap::$staticObjectManager->get(PackageManager::class); | ||
|
||
/** @var PackageInterface $package */ | ||
foreach ($packageManager->getAvailablePackages() as $package) { | ||
if ($package instanceof PackageKeyAwareInterface) { | ||
self::$packageKeys[$package->getPackageKey()] = true; | ||
} | ||
} | ||
} | ||
|
||
return self::$packageKeys; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
Neos.Flow/Tests/Functional/Log/Utility/LogEnvironmentTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
namespace Neos\Flow\Tests\Functional\Log\Utility; | ||
|
||
/* | ||
* This file is part of the Neos.Flow package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
|
||
use Neos\Flow\Log\Utility\LogEnvironment; | ||
use Neos\Flow\Tests\FunctionalTestCase; | ||
|
||
class LogEnvironmentTest extends FunctionalTestCase | ||
{ | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function fromMethodNameDataProvider(): array | ||
{ | ||
return [ | ||
'packageKeyCanBeDetermined' => [ | ||
'method' => __METHOD__, | ||
'expected' => [ | ||
'FLOW_LOG_ENVIRONMENT' => [ | ||
'packageKey' => 'Neos.Flow', | ||
'className' => 'Neos\Flow\Tests\Functional\Log\Utility\LogEnvironmentTest', | ||
'methodName' => 'fromMethodNameDataProvider' | ||
] | ||
] | ||
], | ||
'unknownPackageKeyReturnsFirstPart' => [ | ||
'method' => 'Some\Unknown\CLass\Path::methodName', | ||
'expected' => [ | ||
'FLOW_LOG_ENVIRONMENT' => [ | ||
'packageKey' => 'Some', | ||
'className' => 'Some\Unknown\CLass\Path', | ||
'methodName' => 'methodName' | ||
] | ||
] | ||
] | ||
]; | ||
} | ||
|
||
|
||
/** | ||
* @test | ||
* | ||
* @param $method | ||
* @param $expected | ||
* | ||
* @dataProvider fromMethodNameDataProvider | ||
*/ | ||
public function fromMethodName($method, $expected) | ||
{ | ||
$actual = LogEnvironment::fromMethodName($method); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
} |