]> git.mxchange.org Git - friendica.git/commitdiff
Adding Logger Tests
authorPhilipp Holzer <admin@philipp.info>
Mon, 4 Mar 2019 22:49:37 +0000 (23:49 +0100)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sat, 23 Mar 2019 03:13:26 +0000 (23:13 -0400)
src/Util/Logger/SyslogLogger.php
tests/src/Util/Logger/SyslogLoggerTest.php
tests/src/Util/Logger/SyslogLoggerWrapper.php

index b1abd5bd5e25577477ac5795454d85cabfb9171c..83c3fc3ce57e8707ebc4891fb7e564fba199eb65 100644 (file)
@@ -66,6 +66,12 @@ class SyslogLogger extends AbstractLogger
         */
        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
@@ -141,8 +147,12 @@ class SyslogLogger extends AbstractLogger
         */
        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));
                }
 
                $this->syslogWrapper($priority, $message);
@@ -172,6 +182,11 @@ class SyslogLogger extends AbstractLogger
                return $logMessage;
        }
 
+       private function customErrorHandler($code, $msg)
+       {
+               $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
+       }
+
        /**
         * A syslog wrapper to make syslog functionality testable
         *
@@ -180,6 +195,12 @@ class SyslogLogger extends AbstractLogger
         */
        protected function syslogWrapper($level, $entry)
        {
-               syslog($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));
+               }
        }
 }
index 545f76b32ff15106dd4670f5108a942cfcf0c11e..fbd691d5eb0f1f23b481e424c19005b738332bee 100644 (file)
@@ -59,4 +59,15 @@ class SyslogLoggerTest extends AbstractLoggerTest
 
                $logger->log('NOPE', 'a test');
        }
+
+       /**
+        * Test when the logfacility is wrong (string)
+        * @expectedException \UnexpectedValueException
+        * @expectedExceptionMessageRegExp /Can\'t open syslog for ident ".*" and facility ".*": .* /
+        */
+       public function testServerException()
+       {
+               $logger = new SyslogLoggerWrapper('test', $this->introspection, LogLevel::DEBUG, null, 'a string');
+               $logger->emergency('not working');
+       }
 }
index 63ad53690e772724989ba7f870fbc008d7378704..779067e28ec2b4da1e8c962a8bc5ab1a445c8570 100644 (file)
@@ -6,6 +6,9 @@ use Friendica\Util\Introspection;
 use Friendica\Util\Logger\SyslogLogger;
 use Psr\Log\LogLevel;
 
+/**
+ * Wraps the SyslogLogger for replacing the syslog call with a string field.
+ */
 class SyslogLoggerWrapper extends SyslogLogger
 {
        private $content;