]> git.mxchange.org Git - friendica.git/commitdiff
cleanup sysloglogger
authorPhilipp Holzer <admin@philipp.info>
Thu, 28 Feb 2019 08:48:55 +0000 (09:48 +0100)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sat, 23 Mar 2019 03:11:14 +0000 (23:11 -0400)
config/defaults.config.php
src/Factory/LoggerFactory.php
src/Util/Logger/AbstractFriendicaLogger.php
src/Util/Logger/StreamLogger.php
src/Util/Logger/SyslogLogger.php

index ae5bd361f4c251e0ab0580137cf4edb52fb2f15c..5ec4b625720ef86880c6400641fe13339bd9baef 100644 (file)
@@ -215,7 +215,7 @@ return [
                'local_tags' => false,
 
                // logger_adapter (String)
-               // Sets the logging adapter of Friendica globally (monolog, syslog)
+               // Sets the logging adapter of Friendica globally (monolog, syslog, stream)
                'logger_adapter' => 'monolog',
 
                // max_batch_queue (Integer)
index ebcb3bd2b0bfe5e334232251d6e0e58c10379190..58efd461feaecdb68be147f4eaa7ea8479f1a05f 100644 (file)
@@ -8,6 +8,7 @@ use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Util\Introspection;
 use Friendica\Util\Logger\Monolog\FriendicaDevelopHandler;
 use Friendica\Util\Logger\Monolog\FriendicaIntrospectionProcessor;
+use Friendica\Util\Logger\StreamLogger;
 use Friendica\Util\Logger\SyslogLogger;
 use Friendica\Util\Logger\VoidLogger;
 use Friendica\Util\Logger\WorkerLogger;
@@ -54,13 +55,18 @@ class LoggerFactory
                }
 
                $introspection = new Introspection(self::$ignoreClassList);
+               $level = $config->get('system', 'loglevel');
 
                switch ($config->get('system', 'logger_adapter', 'monolog')) {
-                       case 'syslog':
-                               $level = $config->get('system', 'loglevel');
 
+                       case 'syslog':
                                $logger = new SyslogLogger($channel, $introspection, $profiler, $level);
                                break;
+
+                       case 'stream':
+                               $logger = new StreamLogger($channel, $introspection, $profiler, $level);
+                               break;
+
                        case 'monolog':
                        default:
                                $loggerTimeZone = new \DateTimeZone('UTC');
@@ -73,7 +79,6 @@ class LoggerFactory
                                $logger->pushProcessor(new FriendicaIntrospectionProcessor($introspection, LogLevel::DEBUG));
 
                                $stream = $config->get('system', 'logfile');
-                               $level  = $config->get('system', 'loglevel');
 
                                $loglevel = self::mapLegacyConfigDebugLevel((string)$level);
                                static::addStreamHandler($logger, $stream, $loglevel);
index 3f851f42de372ec7d7ecec728c920284fd68794c..326ee050046f894febebc93f8c2af9dcddaa9ecb 100644 (file)
@@ -99,7 +99,7 @@ abstract class AbstractFriendicaLogger implements LoggerInterface
        public function emergency($message, array $context = array())
        {
                $stamp1 = microtime(true);
-               $this->addEntry(LogLevel::EMERGENCY, $message, $context);
+               $this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
                $this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
        }
 
@@ -109,7 +109,7 @@ abstract class AbstractFriendicaLogger implements LoggerInterface
        public function alert($message, array $context = array())
        {
                $stamp1 = microtime(true);
-               $this->addEntry(LogLevel::ALERT, $message, $context);
+               $this->addEntry(LogLevel::ALERT, (string) $message, $context);
                $this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
        }
 
@@ -119,7 +119,7 @@ abstract class AbstractFriendicaLogger implements LoggerInterface
        public function critical($message, array $context = array())
        {
                $stamp1 = microtime(true);
-               $this->addEntry(LogLevel::CRITICAL, $message, $context);
+               $this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
                $this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
        }
 
@@ -129,7 +129,7 @@ abstract class AbstractFriendicaLogger implements LoggerInterface
        public function error($message, array $context = array())
        {
                $stamp1 = microtime(true);
-               $this->addEntry(LogLevel::ERROR, $message, $context);
+               $this->addEntry(LogLevel::ERROR, (string) $message, $context);
                $this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
        }
 
@@ -139,7 +139,7 @@ abstract class AbstractFriendicaLogger implements LoggerInterface
        public function warning($message, array $context = array())
        {
                $stamp1 = microtime(true);
-               $this->addEntry(LogLevel::WARNING, $message, $context);
+               $this->addEntry(LogLevel::WARNING, (string) $message, $context);
                $this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
        }
 
@@ -149,7 +149,7 @@ abstract class AbstractFriendicaLogger implements LoggerInterface
        public function notice($message, array $context = array())
        {
                $stamp1 = microtime(true);
-               $this->addEntry(LogLevel::NOTICE, $message, $context);
+               $this->addEntry(LogLevel::NOTICE, (string) $message, $context);
                $this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
        }
 
@@ -159,7 +159,7 @@ abstract class AbstractFriendicaLogger implements LoggerInterface
        public function info($message, array $context = array())
        {
                $stamp1 = microtime(true);
-               $this->addEntry(LogLevel::INFO, $message, $context);
+               $this->addEntry(LogLevel::INFO, (string) $message, $context);
                $this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
        }
 
@@ -169,7 +169,7 @@ abstract class AbstractFriendicaLogger implements LoggerInterface
        public function debug($message, array $context = array())
        {
                $stamp1 = microtime(true);
-               $this->addEntry(LogLevel::DEBUG, $message, $context);
+               $this->addEntry(LogLevel::DEBUG, (string) $message, $context);
                $this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
        }
 
@@ -179,7 +179,7 @@ abstract class AbstractFriendicaLogger implements LoggerInterface
        public function log($level, $message, array $context = array())
        {
                $stamp1 = microtime(true);
-               $this->addEntry($level, $message, $context);
+               $this->addEntry($level, (string) $message, $context);
                $this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
        }
 }
index 4ccbc8283b8760056114e2d64db22d2565da6f8e..8d9ffec3873c4fe0126d5b038f1455bf254ef044 100644 (file)
@@ -6,13 +6,20 @@ use Friendica\Util\Introspection;
 use Friendica\Util\Profiler;
 
 /**
- * A Logger instance for logging into a stream
+ * A Logger instance for logging into a stream (file, stdout, stderr)
  */
 class StreamLogger extends AbstractFriendicaLogger
 {
-       public function __construct($channel, Introspection $introspection, Profiler $profiler)
+       /**
+        * The minimum loglevel at which this logger will be triggered
+        * @var string
+        */
+       private $logLevel;
+
+       public function __construct($channel, Introspection $introspection, Profiler $profiler, $level)
        {
                parent::__construct($channel, $introspection, $profiler);
+               $this->logLevel = $level;
        }
 
        /**
index 25d8f2f5e2e7e112f1af9919f9d6d8571823904d..cff7125e5bcc47f23bed3ed034a5c4e0d84e7bc6 100644 (file)
@@ -85,6 +85,27 @@ class SyslogLogger extends AbstractFriendicaLogger
                $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);
+       }
+
        /**
         * Maps the LogLevel (@see LogLevel ) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters )
         *
@@ -103,6 +124,14 @@ class SyslogLogger extends AbstractFriendicaLogger
                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
@@ -121,14 +150,6 @@ class SyslogLogger extends AbstractFriendicaLogger
                syslog($priority, $message);
        }
 
-       /**
-        * Closes the Syslog
-        */
-       public function close()
-       {
-               closelog();
-       }
-
        /**
         * Formats a log record for the syslog output
         *
@@ -152,25 +173,4 @@ class SyslogLogger extends AbstractFriendicaLogger
 
                return $logMessage;
        }
-
-       /**
-        * 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($level, $message, $context);
-               $this->write($level, $formattedLog);
-       }
 }