]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Logger/SyslogLogger.php
Remove uneeded variable.
[friendica.git] / src / Util / Logger / SyslogLogger.php
index 19395157d295e12b46fb65ed8e625a904092b84a..83c3fc3ce57e8707ebc4891fb7e564fba199eb65 100644 (file)
@@ -3,16 +3,14 @@
 namespace Friendica\Util\Logger;
 
 use Friendica\Network\HTTPException\InternalServerErrorException;
-use Friendica\Util\Strings;
-use Psr\Log\InvalidArgumentException;
-use Psr\Log\LoggerInterface;
+use Friendica\Util\Introspection;
 use Psr\Log\LogLevel;
 
 /**
  * A Logger instance for syslogging (fast, but simple)
  * @see http://php.net/manual/en/function.syslog.php
  */
-class SyslogLogger implements LoggerInterface
+class SyslogLogger extends AbstractLogger
 {
        const IDENT = 'Friendica';
 
@@ -46,12 +44,6 @@ class SyslogLogger implements LoggerInterface
                LOG_EMERG   => 'EMERGENCY'
        ];
 
-       /**
-        * The channel of the current process (added to each message)
-        * @var string
-        */
-       private $channel;
-
        /**
         * Indicates what logging options will be used when generating a log message
         * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
@@ -75,31 +67,47 @@ class SyslogLogger implements LoggerInterface
        private $logLevel;
 
        /**
-        * The Introspector for the current call
-        * @var Introspection
-        */
-       private $introspection;
-
-       /**
-        * The UID of the current call
+        * A error message of the current operation
         * @var string
         */
-       private $logUid;
+       private $errorMessage;
 
        /**
-        * @param string $channel     The output channel
+        * {@inheritdoc}
         * @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;
+               parent::__construct($channel, $introspection);
                $this->logOpts = $logOpts;
                $this->logFacility = $logFacility;
                $this->logLevel = $this->mapLevelToPriority($level);
-               $this->introspection = $introspection;
+               $this->introspection->addClasses(array(self::class));
+       }
+
+       /**
+        * Adds a new entry to the syslog
+        *
+        * @param int    $level
+        * @param string $message
+        * @param array  $context
+        *
+        * @throws InternalServerErrorException if the syslog isn't available
+        */
+       protected function addEntry($level, $message, $context = [])
+       {
+               $logLevel = $this->mapLevelToPriority($level);
+
+               if ($logLevel > $this->logLevel) {
+                       return;
+               }
+
+               $formattedLog = $this->formatLog($logLevel, $message, $context);
+               $this->write($logLevel, $formattedLog);
        }
 
        /**
@@ -114,12 +122,20 @@ class SyslogLogger implements LoggerInterface
        public function mapLevelToPriority($level)
        {
                if (!array_key_exists($level, $this->logLevels)) {
-                       throw new InvalidArgumentException('LogLevel \'' . $level . '\' isn\'t valid.');
+                       throw new \InvalidArgumentException(sprintf('The level "%s" is not valid.', $level));
                }
 
                return $this->logLevels[$level];
        }
 
+       /**
+        * Closes the Syslog
+        */
+       public function close()
+       {
+               closelog();
+       }
+
        /**
         * Writes a message to the syslog
         * @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters
@@ -131,19 +147,15 @@ class SyslogLogger implements LoggerInterface
         */
        private function write($priority, $message)
        {
-               if (!openlog(self::IDENT, $this->logOpts, $this->logFacility)) {
-                       throw new InternalServerErrorException('Can\'t open syslog for ident "' . $this->channel . '" and facility "' . $this->logFacility . '""');
-               }
+               set_error_handler([$this, 'customErrorHandler']);
+               $opened = openlog(self::IDENT, $this->logOpts, $this->logFacility);
+               restore_error_handler();
 
-               syslog($priority, $message);
-       }
+               if (!$opened) {
+                       throw new \UnexpectedValueException(sprintf('Can\'t open syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
+               }
 
-       /**
-        * Closes the Syslog
-        */
-       public function close()
-       {
-               closelog();
+               $this->syslogWrapper($priority, $message);
        }
 
        /**
@@ -170,128 +182,25 @@ class SyslogLogger implements LoggerInterface
                return $logMessage;
        }
 
-       /**
-        * Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
-        * @see https://www.php-fig.org/psr/psr-3/#12-message
-        *
-        * @param string $message
-        * @param array  $context
-        *
-        * @return string the interpolated message
-        */
-       private function psrInterpolate($message, array $context = array())
+       private function customErrorHandler($code, $msg)
        {
-               $replace = [];
-               foreach ($context as $key => $value) {
-                       // check that the value can be casted to string
-                       if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
-                               $replace['{' . $key . '}'] = $value;
-                       } elseif (is_array($value)) {
-                               $replace['{' . $key . '}'] = @json_encode($value);
-                       }
-               }
-
-               return strtr($message, $replace);
+               $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
        }
 
        /**
-        * Adds a new entry to the syslog
+        * A syslog wrapper to make syslog functionality testable
         *
-        * @param int    $level
-        * @param string $message
-        * @param array  $context
-        *
-        * @throws InternalServerErrorException if the syslog isn't available
-        */
-       private function addEntry($level, $message, $context = [])
-       {
-               if ($level >= $this->logLevel) {
-                       return;
-               }
-
-               $formattedLog = $this->formatLog($level, $message, $context);
-               $this->write($level, $formattedLog);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @throws InternalServerErrorException if the syslog isn't available
-        */
-       public function emergency($message, array $context = array())
-       {
-               $this->addEntry(LOG_EMERG, $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @throws InternalServerErrorException if the syslog isn't available
-        */
-       public function alert($message, array $context = array())
-       {
-               $this->addEntry(LOG_ALERT, $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @throws InternalServerErrorException if the syslog isn't available
-        */
-       public function critical($message, array $context = array())
-       {
-               $this->addEntry(LOG_CRIT, $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @throws InternalServerErrorException if the syslog isn't available
-        */
-       public function error($message, array $context = array())
-       {
-               $this->addEntry(LOG_ERR, $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @throws InternalServerErrorException if the syslog isn't available
-        */
-       public function warning($message, array $context = array())
-       {
-               $this->addEntry(LOG_WARNING, $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @throws InternalServerErrorException if the syslog isn't available
+        * @param int    $level The syslog priority
+        * @param string $entry The message to send to the syslog function
         */
-       public function notice($message, array $context = array())
+       protected function syslogWrapper($level, $entry)
        {
-               $this->addEntry(LOG_NOTICE, $message, $context);
-       }
+               set_error_handler([$this, 'customErrorHandler']);
+               $written = syslog($level, $entry);
+               restore_error_handler();
 
-       /**
-        * {@inheritdoc}
-        * @throws InternalServerErrorException if the syslog isn't available
-        */
-       public function info($message, array $context = array())
-       {
-               $this->addEntry(LOG_INFO, $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @throws InternalServerErrorException if the syslog isn't available
-        */
-       public function debug($message, array $context = array())
-       {
-               $this->addEntry(LOG_DEBUG, $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @throws InternalServerErrorException if the syslog isn't available
-        */
-       public function log($level, $message, array $context = array())
-       {
-               $logLevel = $this->mapLevelToPriority($level);
-               $this->addEntry($logLevel, $message, $context);
+               if (!$written) {
+                       throw new \UnexpectedValueException(sprintf('Can\'t write into syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
+               }
        }
 }