Skip to content

Commit

Permalink
FEATURE: Introduce an utility to add log environment from method name
Browse files Browse the repository at this point in the history
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
daniellienert committed Oct 3, 2018
1 parent ef04d42 commit e09f4bf
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
94 changes: 94 additions & 0 deletions Neos.Flow/Classes/Log/Utility/LogEnvironment.php
@@ -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 Neos.Flow/Tests/Functional/Log/Utility/LogEnvironmentTest.php
@@ -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);
}
}

0 comments on commit e09f4bf

Please sign in to comment.