]> git.mxchange.org Git - friendica.git/commitdiff
Adding UID and Level To String mapping
authorPhilipp Holzer <admin@philipp.info>
Wed, 27 Feb 2019 16:29:32 +0000 (17:29 +0100)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sat, 23 Mar 2019 02:52:46 +0000 (22:52 -0400)
src/Util/Logger/Introspection.php
src/Util/Logger/SyslogLogger.php

index 83fec6c50caedc0deb182bdae4567273c8625bfc..f99225f9a2bf08b5f318f99767726f75492bbf5f 100644 (file)
@@ -69,8 +69,8 @@ class Introspection implements ProcessorInterface
                $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,
+                       '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,
                ];
        }
index 994c0a1e24b87f493dab060d06e7e26676b213ae..19395157d295e12b46fb65ed8e625a904092b84a 100644 (file)
@@ -3,6 +3,7 @@
 namespace Friendica\Util\Logger;
 
 use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Util\Strings;
 use Psr\Log\InvalidArgumentException;
 use Psr\Log\LoggerInterface;
 use Psr\Log\LogLevel;
@@ -17,18 +18,34 @@ class SyslogLogger implements LoggerInterface
 
        /**
         * Translates LogLevel log levels to syslog log priorities.
+        * @var array
         */
        private $logLevels = [
-               LogLevel::DEBUG => LOG_DEBUG,
-               LogLevel::INFO => LOG_INFO,
-               LogLevel::NOTICE => LOG_NOTICE,
-               LogLevel::WARNING => LOG_WARNING,
-               LogLevel::ERROR => LOG_ERR,
-               LogLevel::CRITICAL => LOG_CRIT,
-               LogLevel::ALERT => LOG_ALERT,
+               LogLevel::DEBUG     => LOG_DEBUG,
+               LogLevel::INFO      => LOG_INFO,
+               LogLevel::NOTICE    => LOG_NOTICE,
+               LogLevel::WARNING   => LOG_WARNING,
+               LogLevel::ERROR     => LOG_ERR,
+               LogLevel::CRITICAL  => LOG_CRIT,
+               LogLevel::ALERT     => LOG_ALERT,
                LogLevel::EMERGENCY => LOG_EMERG,
        ];
 
+       /**
+        * Translates log priorities to string outputs
+        * @var array
+        */
+       private $logToString = [
+               LOG_DEBUG   => 'DEBUG',
+               LOG_INFO    => 'INFO',
+               LOG_NOTICE  => 'NOTICE',
+               LOG_WARNING => 'WARNING',
+               LOG_ERR     => 'ERROR',
+               LOG_CRIT    => 'CRITICAL',
+               LOG_ALERT   => 'ALERT',
+               LOG_EMERG   => 'EMERGENCY'
+       ];
+
        /**
         * The channel of the current process (added to each message)
         * @var string
@@ -63,6 +80,12 @@ class SyslogLogger implements LoggerInterface
         */
        private $introspection;
 
+       /**
+        * The UID of the current call
+        * @var string
+        */
+       private $logUid;
+
        /**
         * @param string $channel     The output channel
         * @param string $level       The minimum loglevel at which this logger will be triggered
@@ -71,6 +94,7 @@ class SyslogLogger implements LoggerInterface
         */
        public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
        {
+               $this->logUid = Strings::getRandomHex(6);
                $this->channel = $channel;
                $this->logOpts = $logOpts;
                $this->logFacility = $logFacility;
@@ -133,19 +157,21 @@ class SyslogLogger implements LoggerInterface
         */
        private function formatLog($level, $message, $context = [])
        {
+               $record = $this->introspection->getRecord();
+               $record = array_merge($record, ['uid' => $this->logUid]);
                $logMessage = '';
 
                $logMessage .= $this->channel . ' ';
-               $logMessage .= '[' . $level . ']: ';
+               $logMessage .= '[' . $this->logToString[$level] . ']: ';
                $logMessage .= $this->psrInterpolate($message, $context) . ' ';
                $logMessage .= @json_encode($context) . ' - ';
-               $logMessage .= @json_encode($this->introspection->getRecord());
+               $logMessage .= @json_encode($record);
 
                return $logMessage;
        }
 
        /**
-        * Simple interpolation of PSR-3 compliant replacements ( between '{' and '}' )
+        * Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
         * @see https://www.php-fig.org/psr/psr-3/#12-message
         *
         * @param string $message