From: Philipp Date: Fri, 29 Oct 2021 10:36:14 +0000 (+0200) Subject: Move Introspection to Logger package X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=22663c4ae5d6dcb61d5a1ff151b711e5e0b337cc;p=friendica.git Move Introspection to Logger package --- diff --git a/src/Core/Logger/Factory/Logger.php b/src/Core/Logger/Factory/Logger.php index f9b32d9328..8f555483c8 100644 --- a/src/Core/Logger/Factory/Logger.php +++ b/src/Core/Logger/Factory/Logger.php @@ -27,7 +27,7 @@ use Friendica\Core; use Friendica\Core\Logger\Exception\LogLevelException; use Friendica\Database\Database; use Friendica\Util\FileSystem; -use Friendica\Util\Introspection; +use Friendica\Core\Logger\Util\Introspection; use Friendica\Core\Logger\Type\Monolog\DevelopHandler; use Friendica\Core\Logger\Type\Monolog\IntrospectionProcessor; use Friendica\Core\Logger\Type\ProfilerLogger; diff --git a/src/Core/Logger/Type/AbstractLogger.php b/src/Core/Logger/Type/AbstractLogger.php index 0b6d9f38fd..e26f1705d4 100644 --- a/src/Core/Logger/Type/AbstractLogger.php +++ b/src/Core/Logger/Type/AbstractLogger.php @@ -22,7 +22,7 @@ namespace Friendica\Core\Logger\Type; use Friendica\Core\Logger\Exception\LoggerException; -use Friendica\Util\Introspection; +use Friendica\Core\Logger\Util\Introspection; use Friendica\Util\Strings; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; diff --git a/src/Core/Logger/Type/Monolog/IntrospectionProcessor.php b/src/Core/Logger/Type/Monolog/IntrospectionProcessor.php index 756331b22f..9da48ff36a 100644 --- a/src/Core/Logger/Type/Monolog/IntrospectionProcessor.php +++ b/src/Core/Logger/Type/Monolog/IntrospectionProcessor.php @@ -21,7 +21,7 @@ namespace Friendica\Core\Logger\Type\Monolog; -use Friendica\Util\Introspection; +use Friendica\Core\Logger\Util\Introspection; use Monolog\Logger; use Monolog\Processor\ProcessorInterface; diff --git a/src/Core/Logger/Type/StreamLogger.php b/src/Core/Logger/Type/StreamLogger.php index f67aef9a49..8d4d8b577e 100644 --- a/src/Core/Logger/Type/StreamLogger.php +++ b/src/Core/Logger/Type/StreamLogger.php @@ -26,7 +26,7 @@ use Friendica\Core\Logger\Exception\LoggerException; use Friendica\Core\Logger\Exception\LogLevelException; use Friendica\Util\DateTimeFormat; use Friendica\Util\FileSystem; -use Friendica\Util\Introspection; +use Friendica\Core\Logger\Util\Introspection; use Psr\Log\LogLevel; /** diff --git a/src/Core/Logger/Type/SyslogLogger.php b/src/Core/Logger/Type/SyslogLogger.php index 024d47b312..54cb327951 100644 --- a/src/Core/Logger/Type/SyslogLogger.php +++ b/src/Core/Logger/Type/SyslogLogger.php @@ -23,7 +23,7 @@ namespace Friendica\Core\Logger\Type; use Friendica\Core\Logger\Exception\LoggerException; use Friendica\Core\Logger\Exception\LogLevelException; -use Friendica\Util\Introspection; +use Friendica\Core\Logger\Util\Introspection; use Psr\Log\LogLevel; /** diff --git a/src/Core/Logger/Util/Introspection.php b/src/Core/Logger/Util/Introspection.php new file mode 100644 index 0000000000..7c2040bbf0 --- /dev/null +++ b/src/Core/Logger/Util/Introspection.php @@ -0,0 +1,110 @@ +. + * + */ + +namespace Friendica\Core\Logger\Util; + +/** + * Get Introspection information about the current call + */ +class Introspection +{ + /** @var int */ + private $skipStackFramesCount; + + /** @var string[] */ + private $skipClassesPartials; + + private $skipFunctions = [ + 'call_user_func', + 'call_user_func_array', + ]; + + /** + * @param string[] $skipClassesPartials An array of classes to skip during logging + * @param int $skipStackFramesCount If the logger should use information from other hierarchy levels of the call + */ + public function __construct(array $skipClassesPartials = [], int $skipStackFramesCount = 0) + { + $this->skipClassesPartials = $skipClassesPartials; + $this->skipStackFramesCount = $skipStackFramesCount; + } + + /** + * Adds new classes to get skipped + * + * @param array $classNames + */ + public function addClasses(array $classNames) + { + $this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames); + } + + /** + * Returns the introspection record of the current call + * + * @return array + */ + public function getRecord(): array + { + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + + $i = 1; + + while ($this->isTraceClassOrSkippedFunction($trace, $i)) { + $i++; + } + + $i += $this->skipStackFramesCount; + + return [ + 'file' => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null, + 'line' => $trace[$i - 1]['line'] ?? null, + 'function' => $trace[$i]['function'] ?? null, + ]; + } + + /** + * Checks if the current trace class or function has to be skipped + * + * @param array $trace The current trace array + * @param int $index The index of the current hierarchy level + * + * @return bool True if the class or function should get skipped, otherwise false + */ + private function isTraceClassOrSkippedFunction(array $trace, int $index): bool + { + if (!isset($trace[$index])) { + return false; + } + + if (isset($trace[$index]['class'])) { + foreach ($this->skipClassesPartials as $part) { + if (strpos($trace[$index]['class'], $part) !== false) { + return true; + } + } + } elseif (in_array($trace[$index]['function'], $this->skipFunctions)) { + return true; + } + + return false; + } +} diff --git a/src/Util/Introspection.php b/src/Util/Introspection.php deleted file mode 100644 index 540bfea728..0000000000 --- a/src/Util/Introspection.php +++ /dev/null @@ -1,107 +0,0 @@ -. - * - */ - -namespace Friendica\Util; - -/** - * Get Introspection information about the current call - */ -class Introspection -{ - private $skipStackFramesCount; - - private $skipClassesPartials; - - private $skipFunctions = [ - 'call_user_func', - 'call_user_func_array', - ]; - - /** - * @param array $skipClassesPartials An array of classes to skip during logging - * @param int $skipStackFramesCount If the logger should use information from other hierarchy levels of the call - */ - public function __construct($skipClassesPartials = array(), $skipStackFramesCount = 0) - { - $this->skipClassesPartials = $skipClassesPartials; - $this->skipStackFramesCount = $skipStackFramesCount; - } - - /** - * Adds new classes to get skipped - * @param array $classNames - */ - public function addClasses(array $classNames) - { - $this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames); - } - - /** - * Returns the introspection record of the current call - * - * @return array - */ - public function getRecord() - { - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - - $i = 1; - - while ($this->isTraceClassOrSkippedFunction($trace, $i)) { - $i++; - } - - $i += $this->skipStackFramesCount; - - return [ - 'file' => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null, - 'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null, - 'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null, - ]; - } - - /** - * Checks if the current trace class or function has to be skipped - * - * @param array $trace The current trace array - * @param int $index The index of the current hierarchy level - * - * @return bool True if the class or function should get skipped, otherwise false - */ - private function isTraceClassOrSkippedFunction(array $trace, $index) - { - if (!isset($trace[$index])) { - return false; - } - - if (isset($trace[$index]['class'])) { - foreach ($this->skipClassesPartials as $part) { - if (strpos($trace[$index]['class'], $part) !== false) { - return true; - } - } - } elseif (in_array($trace[$index]['function'], $this->skipFunctions)) { - return true; - } - - return false; - } -} diff --git a/tests/src/Core/Logger/AbstractLoggerTest.php b/tests/src/Core/Logger/AbstractLoggerTest.php index f1a0553102..2734158b5c 100644 --- a/tests/src/Core/Logger/AbstractLoggerTest.php +++ b/tests/src/Core/Logger/AbstractLoggerTest.php @@ -22,7 +22,7 @@ namespace Friendica\Test\src\Core\Logger; use Friendica\Test\MockedTest; -use Friendica\Util\Introspection; +use Friendica\Core\Logger\Util\Introspection; use Mockery\MockInterface; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; diff --git a/tests/src/Core/Logger/SyslogLoggerWrapper.php b/tests/src/Core/Logger/SyslogLoggerWrapper.php index 05dcbd6bc5..92d71f80a1 100644 --- a/tests/src/Core/Logger/SyslogLoggerWrapper.php +++ b/tests/src/Core/Logger/SyslogLoggerWrapper.php @@ -22,7 +22,7 @@ namespace Friendica\Test\src\Core\Logger; use Friendica\Core\Logger\Type\SyslogLogger; -use Friendica\Util\Introspection; +use Friendica\Core\Logger\Util\Introspection; use Psr\Log\LogLevel; /**