From e09f4bfa8f33b2fba14124f93762a197b500209c Mon Sep 17 00:00:00 2001 From: Daniel Lienert Date: Wed, 3 Oct 2018 14:18:23 +0200 Subject: [PATCH] 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__)); ``` --- .../Classes/Log/Utility/LogEnvironment.php | 94 +++++++++++++++++++ .../Log/Utility/LogEnvironmentTest.php | 63 +++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 Neos.Flow/Classes/Log/Utility/LogEnvironment.php create mode 100644 Neos.Flow/Tests/Functional/Log/Utility/LogEnvironmentTest.php diff --git a/Neos.Flow/Classes/Log/Utility/LogEnvironment.php b/Neos.Flow/Classes/Log/Utility/LogEnvironment.php new file mode 100644 index 0000000000..94abb0d249 --- /dev/null +++ b/Neos.Flow/Classes/Log/Utility/LogEnvironment.php @@ -0,0 +1,94 @@ + [ + '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; + } +} diff --git a/Neos.Flow/Tests/Functional/Log/Utility/LogEnvironmentTest.php b/Neos.Flow/Tests/Functional/Log/Utility/LogEnvironmentTest.php new file mode 100644 index 0000000000..ce7f226624 --- /dev/null +++ b/Neos.Flow/Tests/Functional/Log/Utility/LogEnvironmentTest.php @@ -0,0 +1,63 @@ + [ + '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); + } +}