]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Logger/SyslogLogger.php
Remove uneeded variable.
[friendica.git] / src / Util / Logger / SyslogLogger.php
index cff7125e5bcc47f23bed3ed034a5c4e0d84e7bc6..83c3fc3ce57e8707ebc4891fb7e564fba199eb65 100644 (file)
@@ -4,15 +4,13 @@ namespace Friendica\Util\Logger;
 
 use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Util\Introspection;
-use Friendica\Util\Profiler;
-use Psr\Log\InvalidArgumentException;
 use Psr\Log\LogLevel;
 
 /**
  * A Logger instance for syslogging (fast, but simple)
  * @see http://php.net/manual/en/function.syslog.php
  */
-class SyslogLogger extends AbstractFriendicaLogger
+class SyslogLogger extends AbstractLogger
 {
        const IDENT = 'Friendica';
 
@@ -68,17 +66,23 @@ class SyslogLogger extends AbstractFriendicaLogger
         */
        private $logLevel;
 
+       /**
+        * A error message of the current operation
+        * @var string
+        */
+       private $errorMessage;
+
        /**
         * {@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
+        * @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, Profiler $profiler, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
+       public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
        {
-               parent::__construct($channel, $introspection, $profiler);
+               parent::__construct($channel, $introspection);
                $this->logOpts = $logOpts;
                $this->logFacility = $logFacility;
                $this->logLevel = $this->mapLevelToPriority($level);
@@ -98,7 +102,7 @@ class SyslogLogger extends AbstractFriendicaLogger
        {
                $logLevel = $this->mapLevelToPriority($level);
 
-               if ($logLevel >= $this->logLevel) {
+               if ($logLevel > $this->logLevel) {
                        return;
                }
 
@@ -118,7 +122,7 @@ class SyslogLogger extends AbstractFriendicaLogger
        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];
@@ -143,11 +147,15 @@ class SyslogLogger extends AbstractFriendicaLogger
         */
        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();
+
+               if (!$opened) {
+                       throw new \UnexpectedValueException(sprintf('Can\'t open syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
                }
 
-               syslog($priority, $message);
+               $this->syslogWrapper($priority, $message);
        }
 
        /**
@@ -173,4 +181,26 @@ class SyslogLogger extends AbstractFriendicaLogger
 
                return $logMessage;
        }
+
+       private function customErrorHandler($code, $msg)
+       {
+               $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
+       }
+
+       /**
+        * A syslog wrapper to make syslog functionality testable
+        *
+        * @param int    $level The syslog priority
+        * @param string $entry The message to send to the syslog function
+        */
+       protected function syslogWrapper($level, $entry)
+       {
+               set_error_handler([$this, 'customErrorHandler']);
+               $written = syslog($level, $entry);
+               restore_error_handler();
+
+               if (!$written) {
+                       throw new \UnexpectedValueException(sprintf('Can\'t write into syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
+               }
+       }
 }