]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Logger/SyslogLogger.php
Refactored Logging environment (cleaned up)
[friendica.git] / src / Util / Logger / SyslogLogger.php
index 994c0a1e24b87f493dab060d06e7e26676b213ae..5cb1f8c9e69b13a4eca3016d69099deca7f54f09 100644 (file)
@@ -3,6 +3,8 @@
 namespace Friendica\Util\Logger;
 
 use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Util\Introspection;
+use Friendica\Util\Strings;
 use Psr\Log\InvalidArgumentException;
 use Psr\Log\LoggerInterface;
 use Psr\Log\LogLevel;
@@ -17,18 +19,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
@@ -58,19 +76,29 @@ class SyslogLogger implements LoggerInterface
        private $logLevel;
 
        /**
-        * The Introspector for the current call
+        * The Introspection for the current call
         * @var Introspection
         */
        private $introspection;
 
        /**
-        * @param string $channel     The output channel
-        * @param string $level       The minimum loglevel at which this logger will be triggered
-        * @param int    $logOpts     Indicates what logging options will be used when generating a log message
-        * @param int    $logFacility Used to specify what type of program is logging the message
+        * The UID of the current call
+        * @var string
+        */
+       private $logUid;
+
+       /**
+        * @param string        $channel       The output channel
+        * @param Introspection $introspection The introspection of the current call
+        * @param string        $level         The minimum loglevel at which this logger will be triggered
+        * @param int           $logOpts       Indicates what logging options will be used when generating a log message
+        * @param int           $logFacility   Used to specify what type of program is logging the message
+        *
+        * @throws \Exception
         */
        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 +161,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