]> git.mxchange.org Git - friendica.git/commitdiff
Refactoring Logging to use Configuration
authorPhilipp Holzer <admin@philipp.info>
Mon, 11 Feb 2019 20:13:53 +0000 (21:13 +0100)
committerPhilipp Holzer <admin@philipp.info>
Sun, 17 Feb 2019 19:43:52 +0000 (20:43 +0100)
13 files changed:
bin/auth_ejabberd.php
bin/console.php
bin/daemon.php
bin/worker.php
index.php
src/App.php
src/Core/Config.php
src/Core/Config/Cache/ConfigCache.php
src/Core/Logger.php
src/Factory/DBFactory.php
src/Factory/LoggerFactory.php
tests/include/ApiTest.php
tests/src/Core/Config/Cache/ConfigCacheTest.php

index 7682674d7cf655f627e0cc603de86888486e69ac..003faae1f596d07ae6e9bf08dd5802d694f122ba 100755 (executable)
@@ -59,7 +59,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
 $configCache = Factory\ConfigFactory::createCache($configLoader);
 Factory\DBFactory::init($configCache, $_SERVER);
 $config = Factory\ConfigFactory::createConfig($configCache);
-$pconfig = Factory\ConfigFactory::createPConfig($configCache);
+// needed to call PConfig::init()
+Factory\ConfigFactory::createPConfig($configCache);
 $logger = Factory\LoggerFactory::create('auth_ejabberd', $config);
 $profiler = Factory\ProfilerFactory::create($logger, $config);
 
index 9fee605b2fc2ad44cb945cfc7332a92057abc30b..2b0d5882108187143bff764c4ff0b12cc6d47cd7 100755 (executable)
@@ -12,7 +12,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
 $configCache = Factory\ConfigFactory::createCache($configLoader);
 Factory\DBFactory::init($configCache, $_SERVER);
 $config = Factory\ConfigFactory::createConfig($configCache);
-$pconfig = Factory\ConfigFactory::createPConfig($configCache);
+// needed to call PConfig::init()
+Factory\ConfigFactory::createPConfig($configCache);
 $logger = Factory\LoggerFactory::create('console', $config);
 $profiler = Factory\ProfilerFactory::create($logger, $config);
 
index 2b106b62d8cffb72a3db03eeb9c588ccd8235776..f2970a5180261163227113c4dd0811f94ff1df34 100755 (executable)
@@ -39,7 +39,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
 $configCache = Factory\ConfigFactory::createCache($configLoader);
 Factory\DBFactory::init($configCache, $_SERVER);
 $config = Factory\ConfigFactory::createConfig($configCache);
-$pconfig = Factory\ConfigFactory::createPConfig($configCache);
+// needed to call PConfig::init()
+Factory\ConfigFactory::createPConfig($configCache);
 $logger = Factory\LoggerFactory::create('daemon', $config);
 $profiler = Factory\ProfilerFactory::create($logger, $config);
 
index 0a9b4cb38be4c4ac5f34a0e0e3fa56460d1d4dcb..53033312769a41992a6ce1f2794bbfeadba249f6 100755 (executable)
@@ -37,7 +37,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
 $configCache = Factory\ConfigFactory::createCache($configLoader);
 Factory\DBFactory::init($configCache, $_SERVER);
 $config = Factory\ConfigFactory::createConfig($configCache);
-$pconfig = Factory\ConfigFactory::createPConfig($configCache);
+// needed to call PConfig::init()
+Factory\ConfigFactory::createPConfig($configCache);
 $logger = Factory\LoggerFactory::create('worker', $config);
 $profiler = Factory\ProfilerFactory::create($logger, $config);
 
index 9ef9490b37590ed3ebb45104ef0c8f81941e4662..66f02396540e170e62b1542fe5a8dff143743b59 100644 (file)
--- a/index.php
+++ b/index.php
@@ -20,7 +20,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
 $configCache = Factory\ConfigFactory::createCache($configLoader);
 Factory\DBFactory::init($configCache, $_SERVER);
 $config = Factory\ConfigFactory::createConfig($configCache);
-$pconfig = Factory\ConfigFactory::createPConfig($configCache);
+// needed to call PConfig::init()
+Factory\ConfigFactory::createPConfig($configCache);
 $logger = Factory\LoggerFactory::create('index', $config);
 $profiler = Factory\ProfilerFactory::create($logger, $config);
 
index e715e6ced6c7064f4c10f2ead033965652e53f4f..3189b2da7b44949e7215d6b1f67d848e0c1f774f 100644 (file)
@@ -109,11 +109,6 @@ class App
         */
        public $mobileDetect;
 
-       /**
-        * @var LoggerInterface The current logger of this App
-        */
-       private $logger;
-
        /**
         * @var Configuration The config
         */
@@ -339,35 +334,13 @@ class App
                return $this->mode;
        }
 
-       /**
-        * Returns the Logger of the Application
-        *
-        * @return LoggerInterface The Logger
-        * @throws InternalServerErrorException when the logger isn't created
-        */
-       public function getLogger()
-       {
-               if (empty($this->logger)) {
-                       throw new InternalServerErrorException('Logger of the Application is not defined');
-               }
-
-               return $this->logger;
-       }
-
        /**
         * Reloads the whole app instance
         */
        public function reload()
        {
-               $this->getMode()->determine($this->basePath);
-
                $this->determineURLPath();
 
-               if ($this->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
-                       Core\Config::load();
-               }
-
-               // again because DB-config could change the config
                $this->getMode()->determine($this->basePath);
 
                if ($this->getMode()->has(App\Mode::DBAVAILABLE)) {
@@ -382,8 +355,6 @@ class App
                Core\L10n::init();
 
                $this->process_id = Core\System::processID('log');
-
-               Core\Logger::setLogger($this->logger);
        }
 
        /**
index 405d14d126e6749426407a93fb4f1af65da723ca..4bf9c5b11d2d4aa96495516edfc38dc94c098f25 100644 (file)
@@ -8,10 +8,6 @@
  */
 namespace Friendica\Core;
 
-use Friendica\Core\Config\ConfigCache;
-use Friendica\Core\Config\IConfigAdapter;
-use Friendica\Core\Config\IConfigCache;
-
 /**
  * @brief Arbitrary system configuration storage
  *
index b1172c0c82639c8783ba9d260158dc6b96a92d91..54da327dba9e55b3d5c719a195d7a179ced2a4fe 100644 (file)
@@ -55,6 +55,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
        {
                if (isset($this->config[$cat][$key])) {
                        return $this->config[$cat][$key];
+               } elseif ($key == null && isset($this->config[$cat])) {
+                       return $this->config[$cat];
                } else {
                        return '!<unset>!';
                }
@@ -65,8 +67,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
         */
        public function has($cat, $key = null)
        {
-               return isset($this->config[$cat][$key])
-                       && $this->config[$cat][$key] !== '!<unset>!';
+               return (isset($this->config[$cat][$key]) && $this->config[$cat][$key] !== '!<unset>!') ||
+               ($key == null && isset($this->config[$cat]) && $this->config[$cat] !== '!<unset>!' && is_array($this->config[$cat]));
        }
 
        /**
@@ -105,8 +107,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
         */
        public function hasP($uid, $cat, $key = null)
        {
-               return isset($this->config[$uid][$cat][$key])
-                       && $this->config[$uid][$cat][$key] !== '!<unset>!';
+               return (isset($this->config[$uid][$cat][$key]) && $this->config[$uid][$cat][$key] !== '!<unset>!') ||
+                       ($key == null && isset($this->config[$uid][$cat]) && $this->config[$uid][$cat] !== '!<unset>!' && is_array($this->config[$uid][$cat]));
        }
 
        /**
@@ -144,6 +146,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
        {
                if (isset($this->config[$uid][$cat][$key])) {
                        return $this->config[$uid][$cat][$key];
+               } elseif ($key == null && isset($this->config[$uid][$cat])) {
+                       return $this->config[$uid][$cat];
                } else {
                        return '!<unset>!';
                }
index e42f2f033a2b8bb864db607af9aee0d1815949a8..a2e587342d528d1734a4984b0ae6ef33148b03d4 100644 (file)
@@ -5,8 +5,6 @@
 namespace Friendica\Core;
 
 use Friendica\BaseObject;
-use Friendica\Factory\LoggerFactory;
-use Friendica\Network\HTTPException\InternalServerErrorException;
 use Psr\Log\LoggerInterface;
 use Psr\Log\LogLevel;
 
@@ -67,73 +65,22 @@ class Logger extends BaseObject
 
        /**
         * Sets the default logging handler for Friendica.
-        * @todo Can be combined with other handlers too if necessary, could be configurable.
         *
         * @param LoggerInterface $logger The Logger instance of this Application
-        *
-        * @throws InternalServerErrorException if the logger factory is incompatible to this logger
         */
        public static function setLogger($logger)
        {
-               $debugging = Config::get('system', 'debugging');
-               $logfile = Config::get('system', 'logfile');
-               $loglevel = Config::get('system', 'loglevel');
-
-               if (!$debugging || !$logfile) {
-                       return;
-               }
-
-               $loglevel = self::mapLegacyConfigDebugLevel((string)$loglevel);
-
-               LoggerFactory::addStreamHandler($logger, $logfile, $loglevel);
-
                self::$logger = $logger;
-
-               $logfile = Config::get('system', 'dlogfile');
-
-               if (!$logfile) {
-                       return;
-               }
-
-               $developIp = Config::get('system', 'dlogip');
-
-               self::$devLogger = LoggerFactory::createDev('develop', $developIp);
-               LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
        }
 
        /**
-        * Mapping a legacy level to the PSR-3 compliant levels
-        * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
-        *
-        * @param string $level the level to be mapped
+        * Sets the default dev-logging handler for Friendica.
         *
-        * @return string the PSR-3 compliant level
+        * @param LoggerInterface $logger The Logger instance of this Application
         */
-       private static function mapLegacyConfigDebugLevel($level)
+       public static function setDevLogger($logger)
        {
-               switch ($level) {
-                       // legacy WARNING
-                       case "0":
-                               return LogLevel::ERROR;
-                       // legacy INFO
-                       case "1":
-                               return LogLevel::WARNING;
-                       // legacy TRACE
-                       case "2":
-                               return LogLevel::NOTICE;
-                       // legacy DEBUG
-                       case "3":
-                               return LogLevel::INFO;
-                       // legacy DATA
-                       case "4":
-                               return LogLevel::DEBUG;
-                       // legacy ALL
-                       case "5":
-                               return LogLevel::DEBUG;
-                       // default if nothing set
-                       default:
-                               return $level;
-               }
+               self::$devLogger = $logger;
        }
 
        /**
index 5970541c105d19eb544cd11c5e01816f63c96c0f..abfe6f99d0ed5c18f02ad1f59a209f91889aca97 100644 (file)
@@ -7,6 +7,14 @@ use Friendica\Database;
 
 class DBFactory
 {
+       /**
+        * Initialize the DBA connection
+        *
+        * @param Cache\ConfigCache $configCache The configuration cache
+        * @param array             $server      The $_SERVER variables
+        *
+        * @throws \Exception if connection went bad
+        */
        public static function init(Cache\ConfigCache $configCache, array $server)
        {
                if (Database\DBA::connected()) {
index e9bacb38e8f5e55d1aeee1ab961e9e1b61c141cc..74f55e637f60e2bc7d7366842e1211e5a6842c98 100644 (file)
@@ -35,16 +35,17 @@ class LoggerFactory
                $logger->pushProcessor(new Monolog\Processor\UidProcessor());
                $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, [Logger::class, Profiler::class]));
 
-               if (isset($config)) {
-                       $debugging = $config->get('system', 'debugging');
-                       $stream = $config->get('system', 'logfile');
-                       $level = $config->get('system', 'loglevel');
+               $debugging = $config->get('system', 'debugging');
+               $stream    = $config->get('system', 'logfile');
+               $level     = $config->get('system', 'loglevel');
 
-                       if ($debugging) {
-                               static::addStreamHandler($logger, $stream, $level);
-                       }
+               if ($debugging) {
+                       $loglevel = self::mapLegacyConfigDebugLevel((string)$level);
+                       static::addStreamHandler($logger, $stream, $loglevel);
                }
 
+               Logger::setLogger($logger);
+
                return $logger;
        }
 
@@ -56,25 +57,71 @@ class LoggerFactory
         *
         * It should never get filled during normal usage of Friendica
         *
-        * @param string $channel      The channel of the logger instance
-        * @param string $developerIp  The IP of the developer who wants to use the logger
+        * @param string        $channel The channel of the logger instance
+        * @param Configuration $config  The config
         *
         * @return LoggerInterface The PSR-3 compliant logger instance
         */
-       public static function createDev($channel, $developerIp)
+       public static function createDev($channel, Configuration $config)
        {
+               $debugging   = $config->get('system', 'debugging');
+               $stream      = $config->get('system', 'dlogfile');
+               $developerIp = $config->get('system', 'dlogip');
+
+               if (!isset($developerIp) || !$debugging) {
+                       return null;
+               }
+
                $logger = new Monolog\Logger($channel);
                $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
                $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
                $logger->pushProcessor(new Monolog\Processor\UidProcessor());
                $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger']));
 
-
                $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
 
+               static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
+
+               Logger::setDevLogger($logger);
+
                return $logger;
        }
 
+       /**
+        * Mapping a legacy level to the PSR-3 compliant levels
+        * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
+        *
+        * @param string $level the level to be mapped
+        *
+        * @return string the PSR-3 compliant level
+        */
+       private static function mapLegacyConfigDebugLevel($level)
+       {
+               switch ($level) {
+                       // legacy WARNING
+                       case "0":
+                               return LogLevel::ERROR;
+                       // legacy INFO
+                       case "1":
+                               return LogLevel::WARNING;
+                       // legacy TRACE
+                       case "2":
+                               return LogLevel::NOTICE;
+                       // legacy DEBUG
+                       case "3":
+                               return LogLevel::INFO;
+                       // legacy DATA
+                       case "4":
+                               return LogLevel::DEBUG;
+                       // legacy ALL
+                       case "5":
+                               return LogLevel::DEBUG;
+                       // default if nothing set
+                       default:
+                               return $level;
+               }
+       }
+
        /**
         * Adding a handler to a given logger instance
         *
@@ -106,32 +153,4 @@ class LoggerFactory
                        throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
                }
        }
-
-       /**
-        * This method enables the test mode of a given logger
-        *
-        * @param LoggerInterface $logger The logger
-        *
-        * @return Monolog\Handler\TestHandler the Handling for tests
-        *
-        * @throws InternalServerErrorException if the logger is incompatible to the logger factory
-        */
-       public static function enableTest($logger)
-       {
-               if ($logger instanceof Monolog\Logger) {
-                       // disable every handler so far
-                       $logger->pushHandler(new Monolog\Handler\NullHandler());
-
-                       // enable the test handler
-                       $fileHandler = new Monolog\Handler\TestHandler();
-                       $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
-                       $fileHandler->setFormatter($formatter);
-
-                       $logger->pushHandler($fileHandler);
-
-                       return $fileHandler;
-               } else {
-                       throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
-               }
-       }
 }
index 7f2299d51932d5b4204c6454112a3c5c9ec7012d..59096f5e5b1aea44c447cee28bef41eb5c117b17 100644 (file)
@@ -41,7 +41,7 @@ class ApiTest extends DatabaseTest
                $configCache = Factory\ConfigFactory::createCache($configLoader);
                Factory\DBFactory::init($configCache, $_SERVER);
                $config = Factory\ConfigFactory::createConfig($configCache);
-               $pconfig = Factory\ConfigFactory::createPConfig($configCache);
+               Factory\ConfigFactory::createPConfig($configCache);
                $logger = Factory\LoggerFactory::create('test', $config);
                $profiler = Factory\ProfilerFactory::create($logger, $config);
                $this->app = new App($config, $logger, $profiler, false);
index 7b56ca6469a95dd3de33797f4954e27f853a8a83..ac9fae540b8c45ab89eb0c977d28408b21955f6b 100644 (file)
@@ -149,13 +149,34 @@ class ConfigCacheTest extends MockedTest
                $configCache = new ConfigCache();
 
                $this->assertFalse($configCache->has('system', 'test'));
+               $this->assertFalse($configCache->has('system'));
 
                $configCache->set('system', 'test', 'it');
                $this->assertTrue($configCache->has('system', 'test'));
+               $this->assertTrue($configCache->has('system'));
+       }
 
-               $this->assertFalse($configCache->has('system', null));
-               $configCache->set('system', null, 'it');
-               $this->assertTrue($configCache->has('system', null));
+       /**
+        * Test the get() method with a category
+        */
+       public function testGetCat()
+       {
+               $configCache = new ConfigCache([
+                       'system' => [
+                               'key1' => 'value1',
+                               'key2' => 'value2',
+                       ],
+                       'config' => [
+                               'key3' => 'value3',
+                       ],
+               ]);
+
+               $this->assertTrue($configCache->has('system'));
+
+               $this->assertEquals([
+                       'key1' => 'value1',
+                       'key2' => 'value2',
+               ], $configCache->get('system'));
        }
 
        /**
@@ -194,6 +215,32 @@ class ConfigCacheTest extends MockedTest
        }
 
 
+       /**
+        * Test the getP() method with a category
+        */
+       public function testGetPCat()
+       {
+               $configCache = new ConfigCache();
+               $uid = 345;
+
+               $configCache->loadP($uid, [
+                       'system' => [
+                               'key1' => 'value1',
+                               'key2' => 'value2',
+                       ],
+                       'config' => [
+                               'key3' => 'value3',
+                       ],
+               ]);
+
+               $this->assertTrue($configCache->hasP($uid,'system'));
+
+               $this->assertEquals([
+                       'key1' => 'value1',
+                       'key2' => 'value2',
+               ], $configCache->get($uid, 'system'));
+       }
+
        /**
         * Test the deleteP() method
         * @dataProvider dataTests
@@ -227,12 +274,10 @@ class ConfigCacheTest extends MockedTest
                $uid = 345;
 
                $this->assertFalse($configCache->hasP($uid, 'system', 'test'));
+               $this->assertFalse($configCache->hasP($uid, 'system'));
 
                $configCache->setP($uid, 'system', 'test', 'it');
                $this->assertTrue($configCache->hasP($uid, 'system', 'test'));
-
-               $this->assertFalse($configCache->hasP($uid, 'system', null));
-               $configCache->setP($uid, 'system', null, 'it');
-               $this->assertTrue($configCache->hasP($uid, 'system', null));
+               $this->assertTrue($configCache->hasP($uid, 'system'));
        }
 }