]> git.mxchange.org Git - friendica.git/commitdiff
Rename & Testfix
authorPhilipp Holzer <admin@philipp.info>
Mon, 4 Mar 2019 07:42:08 +0000 (08:42 +0100)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sat, 23 Mar 2019 03:13:25 +0000 (23:13 -0400)
src/Factory/LoggerFactory.php
src/Util/Logger/AbstractFriendicaLogger.php [deleted file]
src/Util/Logger/AbstractLogger.php [new file with mode: 0644]
src/Util/Logger/Monolog/DevelopHandler.php [new file with mode: 0644]
src/Util/Logger/Monolog/FriendicaDevelopHandler.php [deleted file]
src/Util/Logger/Monolog/FriendicaIntrospectionProcessor.php [deleted file]
src/Util/Logger/Monolog/IntrospectionProcessor.php [new file with mode: 0644]
src/Util/Logger/README.md
src/Util/Logger/StreamLogger.php
src/Util/Logger/SyslogLogger.php
tests/src/Util/Logger/StreamLoggerTest.php

index 0ab4c2ac20aaf74095c709f2a66eae9d0baca757..32a338fb0bd8b2da4d0291c4eaeb8a431462534b 100644 (file)
@@ -6,8 +6,8 @@ use Friendica\Core\Config\Configuration;
 use Friendica\Core\Logger;
 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\Monolog\DevelopHandler;
+use Friendica\Util\Logger\Monolog\IntrospectionProcessor;
 use Friendica\Util\Logger\ProfilerLogger;
 use Friendica\Util\Logger\StreamLogger;
 use Friendica\Util\Logger\SyslogLogger;
@@ -67,7 +67,7 @@ class LoggerFactory
                                $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
                                $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
                                $logger->pushProcessor(new Monolog\Processor\UidProcessor());
-                               $logger->pushProcessor(new FriendicaIntrospectionProcessor($introspection, LogLevel::DEBUG));
+                               $logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG));
 
                                $stream = $config->get('system', 'logfile');
 
@@ -139,9 +139,9 @@ class LoggerFactory
                                $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
                                $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
                                $logger->pushProcessor(new Monolog\Processor\UidProcessor());
-                               $logger->pushProcessor(new FriendicaIntrospectionProcessor($introspection, LogLevel::DEBUG));
+                               $logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG));
 
-                               $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
+                               $logger->pushHandler(new DevelopHandler($developerIp));
 
                                static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
                                break;
diff --git a/src/Util/Logger/AbstractFriendicaLogger.php b/src/Util/Logger/AbstractFriendicaLogger.php
deleted file mode 100644 (file)
index 82f2f56..0000000
+++ /dev/null
@@ -1,158 +0,0 @@
-<?php
-
-namespace Friendica\Util\Logger;
-
-use Friendica\Util\Introspection;
-use Friendica\Util\Strings;
-use Psr\Log\LoggerInterface;
-use Psr\Log\LogLevel;
-
-/**
- * This class contains all necessary dependencies and calls for Friendica
- * Every new Logger should extend this class and define, how addEntry() works
- *
- * Additional information for each Logger, who extends this class:
- * - Introspection
- * - UID for each call
- * - Channel of the current call (i.e. index, worker, daemon, ...)
- */
-abstract class AbstractFriendicaLogger implements LoggerInterface
-{
-       /**
-        * The output channel of this logger
-        * @var string
-        */
-       protected $channel;
-
-       /**
-        * The Introspection for the current call
-        * @var Introspection
-        */
-       protected $introspection;
-
-       /**
-        * The UID of the current call
-        * @var string
-        */
-       protected $logUid;
-
-       /**
-        * Adds a new entry to the log
-        *
-        * @param int    $level
-        * @param string $message
-        * @param array  $context
-        *
-        * @return void
-        */
-       abstract protected function addEntry($level, $message, $context = []);
-
-       /**
-        * @param string        $channel       The output channel
-        * @param Introspection $introspection The introspection of the current call
-        *
-        * @throws \Exception
-        */
-       public function __construct($channel, Introspection $introspection)
-       {
-               $this->channel       = $channel;
-               $this->introspection = $introspection;
-               $this->logUid        = Strings::getRandomHex(6);
-       }
-
-       /**
-        * 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
-        */
-       protected function psrInterpolate($message, array $context = array())
-       {
-               $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);
-       }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function emergency($message, array $context = array())
-       {
-               $this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function alert($message, array $context = array())
-       {
-               $this->addEntry(LogLevel::ALERT, (string) $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function critical($message, array $context = array())
-       {
-               $this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function error($message, array $context = array())
-       {
-               $this->addEntry(LogLevel::ERROR, (string) $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function warning($message, array $context = array())
-       {
-               $this->addEntry(LogLevel::WARNING, (string) $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function notice($message, array $context = array())
-       {
-               $this->addEntry(LogLevel::NOTICE, (string) $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function info($message, array $context = array())
-       {
-               $this->addEntry(LogLevel::INFO, (string) $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function debug($message, array $context = array())
-       {
-               $this->addEntry(LogLevel::DEBUG, (string) $message, $context);
-       }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function log($level, $message, array $context = array())
-       {
-               $this->addEntry($level, (string) $message, $context);
-       }
-}
diff --git a/src/Util/Logger/AbstractLogger.php b/src/Util/Logger/AbstractLogger.php
new file mode 100644 (file)
index 0000000..576f4bf
--- /dev/null
@@ -0,0 +1,158 @@
+<?php
+
+namespace Friendica\Util\Logger;
+
+use Friendica\Util\Introspection;
+use Friendica\Util\Strings;
+use Psr\Log\LoggerInterface;
+use Psr\Log\LogLevel;
+
+/**
+ * This class contains all necessary dependencies and calls for Friendica
+ * Every new Logger should extend this class and define, how addEntry() works
+ *
+ * Additional information for each Logger, who extends this class:
+ * - Introspection
+ * - UID for each call
+ * - Channel of the current call (i.e. index, worker, daemon, ...)
+ */
+abstract class AbstractLogger implements LoggerInterface
+{
+       /**
+        * The output channel of this logger
+        * @var string
+        */
+       protected $channel;
+
+       /**
+        * The Introspection for the current call
+        * @var Introspection
+        */
+       protected $introspection;
+
+       /**
+        * The UID of the current call
+        * @var string
+        */
+       protected $logUid;
+
+       /**
+        * Adds a new entry to the log
+        *
+        * @param int    $level
+        * @param string $message
+        * @param array  $context
+        *
+        * @return void
+        */
+       abstract protected function addEntry($level, $message, $context = []);
+
+       /**
+        * @param string        $channel       The output channel
+        * @param Introspection $introspection The introspection of the current call
+        *
+        * @throws \Exception
+        */
+       public function __construct($channel, Introspection $introspection)
+       {
+               $this->channel       = $channel;
+               $this->introspection = $introspection;
+               $this->logUid        = Strings::getRandomHex(6);
+       }
+
+       /**
+        * 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
+        */
+       protected function psrInterpolate($message, array $context = array())
+       {
+               $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);
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function emergency($message, array $context = array())
+       {
+               $this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function alert($message, array $context = array())
+       {
+               $this->addEntry(LogLevel::ALERT, (string) $message, $context);
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function critical($message, array $context = array())
+       {
+               $this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function error($message, array $context = array())
+       {
+               $this->addEntry(LogLevel::ERROR, (string) $message, $context);
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function warning($message, array $context = array())
+       {
+               $this->addEntry(LogLevel::WARNING, (string) $message, $context);
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function notice($message, array $context = array())
+       {
+               $this->addEntry(LogLevel::NOTICE, (string) $message, $context);
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function info($message, array $context = array())
+       {
+               $this->addEntry(LogLevel::INFO, (string) $message, $context);
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function debug($message, array $context = array())
+       {
+               $this->addEntry(LogLevel::DEBUG, (string) $message, $context);
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function log($level, $message, array $context = array())
+       {
+               $this->addEntry($level, (string) $message, $context);
+       }
+}
diff --git a/src/Util/Logger/Monolog/DevelopHandler.php b/src/Util/Logger/Monolog/DevelopHandler.php
new file mode 100644 (file)
index 0000000..07a8393
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+namespace Friendica\Util\Logger\Monolog;
+
+use Monolog\Handler;
+use Monolog\Logger;
+
+/**
+ * Simple handler for Friendica developers to use for deeper logging
+ *
+ * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
+ * you'll use Logger::develop() for the duration of your work, and you clean it up when you're done before submitting your PR.
+ */
+class DevelopHandler extends Handler\AbstractHandler
+{
+       /**
+        * @var string The IP of the developer who wants to debug
+        */
+       private $developerIp;
+
+       /**
+        * @param string $developerIp  The IP of the developer who wants to debug
+        * @param int    $level        The minimum logging level at which this handler will be triggered
+        * @param bool   $bubble       Whether the messages that are handled can bubble up the stack or not
+        */
+       public function __construct($developerIp, $level = Logger::DEBUG, $bubble = true)
+       {
+               parent::__construct($level, $bubble);
+
+               $this->developerIp = $developerIp;
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function handle(array $record)
+       {
+               if (!$this->isHandling($record)) {
+                       return false;
+               }
+
+               /// Just in case the remote IP is the same as the developer IP log the output
+               if (!is_null($this->developerIp) && $_SERVER['REMOTE_ADDR'] != $this->developerIp)
+               {
+                       return false;
+               }
+
+               return false === $this->bubble;
+       }
+}
diff --git a/src/Util/Logger/Monolog/FriendicaDevelopHandler.php b/src/Util/Logger/Monolog/FriendicaDevelopHandler.php
deleted file mode 100644 (file)
index 13a4645..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-<?php
-
-namespace Friendica\Util\Logger\Monolog;
-
-use Monolog\Handler;
-use Monolog\Logger;
-
-/**
- * Simple handler for Friendica developers to use for deeper logging
- *
- * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
- * you'll use Logger::develop() for the duration of your work, and you clean it up when you're done before submitting your PR.
- */
-class FriendicaDevelopHandler extends Handler\AbstractHandler
-{
-       /**
-        * @var string The IP of the developer who wants to debug
-        */
-       private $developerIp;
-
-       /**
-        * @param string $developerIp  The IP of the developer who wants to debug
-        * @param int    $level        The minimum logging level at which this handler will be triggered
-        * @param bool   $bubble       Whether the messages that are handled can bubble up the stack or not
-        */
-       public function __construct($developerIp, $level = Logger::DEBUG, $bubble = true)
-       {
-               parent::__construct($level, $bubble);
-
-               $this->developerIp = $developerIp;
-       }
-
-       /**
-        * {@inheritdoc}
-        */
-       public function handle(array $record)
-       {
-               if (!$this->isHandling($record)) {
-                       return false;
-               }
-
-               /// Just in case the remote IP is the same as the developer IP log the output
-               if (!is_null($this->developerIp) && $_SERVER['REMOTE_ADDR'] != $this->developerIp)
-               {
-                       return false;
-               }
-
-               return false === $this->bubble;
-       }
-}
diff --git a/src/Util/Logger/Monolog/FriendicaIntrospectionProcessor.php b/src/Util/Logger/Monolog/FriendicaIntrospectionProcessor.php
deleted file mode 100644 (file)
index b8e7f5e..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-<?php
-
-namespace Friendica\Util\Logger\Monolog;
-
-use Friendica\Util\Introspection;
-use Monolog\Logger;
-use Monolog\Processor\ProcessorInterface;
-
-/**
- * Injects line/file//function where the log message came from
- */
-class FriendicaIntrospectionProcessor implements ProcessorInterface
-{
-       private $level;
-
-       private $introspection;
-
-       /**
-        * @param Introspection $introspection Holds the Introspection of the current call
-        * @param string|int    $level         The minimum logging level at which this Processor will be triggered
-        */
-       public function __construct(Introspection $introspection, $level = Logger::DEBUG)
-       {
-               $this->level = Logger::toMonologLevel($level);
-               $introspection->addClasses(array('Monolog\\'));
-               $this->introspection = $introspection;
-       }
-
-       public function __invoke(array $record)
-       {
-               // return if the level is not high enough
-               if ($record['level'] < $this->level) {
-                       return $record;
-               }
-               // we should have the call source now
-               $record['extra'] = array_merge(
-                       $record['extra'],
-                       $this->introspection->getRecord()
-               );
-
-               return $record;
-       }
-}
diff --git a/src/Util/Logger/Monolog/IntrospectionProcessor.php b/src/Util/Logger/Monolog/IntrospectionProcessor.php
new file mode 100644 (file)
index 0000000..18ea846
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+namespace Friendica\Util\Logger\Monolog;
+
+use Friendica\Util\Introspection;
+use Monolog\Logger;
+use Monolog\Processor\ProcessorInterface;
+
+/**
+ * Injects line/file//function where the log message came from
+ */
+class IntrospectionProcessor implements ProcessorInterface
+{
+       private $level;
+
+       private $introspection;
+
+       /**
+        * @param Introspection $introspection Holds the Introspection of the current call
+        * @param string|int    $level         The minimum logging level at which this Processor will be triggered
+        */
+       public function __construct(Introspection $introspection, $level = Logger::DEBUG)
+       {
+               $this->level = Logger::toMonologLevel($level);
+               $introspection->addClasses(array('Monolog\\'));
+               $this->introspection = $introspection;
+       }
+
+       public function __invoke(array $record)
+       {
+               // return if the level is not high enough
+               if ($record['level'] < $this->level) {
+                       return $record;
+               }
+               // we should have the call source now
+               $record['extra'] = array_merge(
+                       $record['extra'],
+                       $this->introspection->getRecord()
+               );
+
+               return $record;
+       }
+}
index 381bd94ba7c1ba7d6070e15de58786faa6e0e8f7..449403194d8d3d26de5cf394502e28d10c773c77 100644 (file)
@@ -24,6 +24,4 @@ Each logging implementation should pe capable of printing at least the following
 -      A log message
 -      A context of the log message (f.e which user)
 
-If possible, a Logger should extend [`AbstractFriendicaLogger`](AbstractFriendicaLogger.php), because it contains additional, Friendica specific business logic for each logging call.
-
-Using AbstractFriendicaLogger makes the logger capable of adding profiling data for each log call.
+If possible, a Logger should extend [`AbstractLogger`](AbstractLogger.php), because it contains additional, Friendica specific business logic for each logging call.
index 10ad0a0976b5a717b5e73efad09534562737d992..7e52df80f129ddf7413688efdc1b8f166d616efa 100644 (file)
@@ -9,7 +9,7 @@ use Psr\Log\LogLevel;
 /**
  * A Logger instance for logging into a stream (file, stdout, stderr)
  */
-class StreamLogger extends AbstractFriendicaLogger
+class StreamLogger extends AbstractLogger
 {
        /**
         * The minimum loglevel at which this logger will be triggered
index 2d4a5fe311622dcf9a1680b7ed21850042df587f..e21e953ac9554b63c2e9e88455aec81af82a5bce 100644 (file)
@@ -11,7 +11,7 @@ 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';
 
index a2e81441b264ecbb911c260b91b9efe36aed6f16..38706231ce1c18f839518e600ad9754f55c0e53e 100644 (file)
@@ -124,22 +124,6 @@ class StreamLoggerTest extends MockedTest
                $this->assertLoglineNums(5, $text);
        }
 
-
-       /**
-        * Test if a file cannot get opened
-        * @expectedException \UnexpectedValueException
-        */
-       public function testNoFile()
-       {
-               $logfile = vfsStream::newFile('friendica.log')
-                       ->at($this->root)
-                       ->chmod(0);
-
-               $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
-
-               $logger->emergency('not working');
-       }
-
        /**
         * Test when a file isn't set
         * @expectedException \LogicException
@@ -153,13 +137,16 @@ class StreamLoggerTest extends MockedTest
        }
 
        /**
-        * Test when a file doesn't exist
+        * Test when a file cannot be opened
         * @expectedException \UnexpectedValueException
         * @expectedExceptionMessageRegExp /The stream or file .* could not be opened: .* /
         */
        public function testWrongUrl()
        {
-               $logger = new StreamLogger('test', 'wrongfile', $this->introspection);
+               $logfile = vfsStream::newFile('friendica.log')
+                       ->at($this->root)->chmod(0);
+
+               $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
 
                $logger->emergency('not working');
        }
@@ -171,7 +158,7 @@ class StreamLoggerTest extends MockedTest
         */
        public function testWrongDir()
        {
-               $logger = new StreamLogger('test', 'a/wrong/directory/file.txt', $this->introspection);
+               $logger = new StreamLogger('test', '/a/wrong/directory/file.txt', $this->introspection);
 
                $logger->emergency('not working');
        }