]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Logger/Util/Introspection.php
Use the owner, not the author
[friendica.git] / src / Core / Logger / Util / Introspection.php
index 7c2040bbf0c453e6659f233411ffb68432253e85..0b703da825514b22558910029589f63479eb3721 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Core\Logger\Util;
 
+use Friendica\App\Request;
+use Friendica\Core\Logger\Capability\IHaveCallIntrospections;
+
 /**
  * Get Introspection information about the current call
  */
-class Introspection
+class Introspection implements IHaveCallIntrospections
 {
+       /** @var string */
+       private $requestId;
+
        /** @var int  */
        private $skipStackFramesCount;
 
@@ -41,8 +47,9 @@ class Introspection
         * @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)
+       public function __construct(Request $request, array $skipClassesPartials = [], int $skipStackFramesCount = 0)
        {
+               $this->requestId            = $request->getRequestId();
                $this->skipClassesPartials  = $skipClassesPartials;
                $this->skipStackFramesCount = $skipStackFramesCount;
        }
@@ -52,7 +59,7 @@ class Introspection
         *
         * @param array $classNames
         */
-       public function addClasses(array $classNames)
+       public function addClasses(array $classNames): void
        {
                $this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames);
        }
@@ -68,40 +75,40 @@ class Introspection
 
                $i = 1;
 
-               while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
+               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,
+                       'file'       => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
+                       'line'       => $trace[$i - 1]['line'] ?? null,
+                       'function'   => $trace[$i]['function'] ?? null,
+                       'request-id' => $this->requestId,
                ];
        }
 
        /**
         * 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
+        * @param array $traceItem The current trace item
         *
         * @return bool True if the class or function should get skipped, otherwise false
         */
-       private function isTraceClassOrSkippedFunction(array $trace, int $index): bool
+       private function isTraceClassOrSkippedFunction(array $traceItem): bool
        {
-               if (!isset($trace[$index])) {
+               if (!$traceItem) {
                        return false;
                }
 
-               if (isset($trace[$index]['class'])) {
+               if (isset($traceItem['class'])) {
                        foreach ($this->skipClassesPartials as $part) {
-                               if (strpos($trace[$index]['class'], $part) !== false) {
+                               if (strpos($traceItem['class'], $part) === 0) {
                                        return true;
                                }
                        }
-               } elseif (in_array($trace[$index]['function'], $this->skipFunctions)) {
+               } elseif (in_array($traceItem['function'], $this->skipFunctions)) {
                        return true;
                }