Refactoring Logging to use Configuration

This commit is contained in:
Philipp Holzer 2019-02-11 21:13:53 +01:00
parent eafcf3592d
commit 80f1feabe5
No known key found for this signature in database
GPG Key ID: 517BE60E2CE5C8A5
13 changed files with 141 additions and 146 deletions

View File

@ -59,7 +59,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER); Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache); $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); $logger = Factory\LoggerFactory::create('auth_ejabberd', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config); $profiler = Factory\ProfilerFactory::create($logger, $config);

View File

@ -12,7 +12,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER); Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache); $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); $logger = Factory\LoggerFactory::create('console', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config); $profiler = Factory\ProfilerFactory::create($logger, $config);

View File

@ -39,7 +39,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER); Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache); $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); $logger = Factory\LoggerFactory::create('daemon', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config); $profiler = Factory\ProfilerFactory::create($logger, $config);

View File

@ -37,7 +37,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER); Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache); $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); $logger = Factory\LoggerFactory::create('worker', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config); $profiler = Factory\ProfilerFactory::create($logger, $config);

View File

@ -20,7 +20,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER); Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache); $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); $logger = Factory\LoggerFactory::create('index', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config); $profiler = Factory\ProfilerFactory::create($logger, $config);

View File

@ -109,11 +109,6 @@ class App
*/ */
public $mobileDetect; public $mobileDetect;
/**
* @var LoggerInterface The current logger of this App
*/
private $logger;
/** /**
* @var Configuration The config * @var Configuration The config
*/ */
@ -339,35 +334,13 @@ class App
return $this->mode; 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 * Reloads the whole app instance
*/ */
public function reload() public function reload()
{ {
$this->getMode()->determine($this->basePath);
$this->determineURLPath(); $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); $this->getMode()->determine($this->basePath);
if ($this->getMode()->has(App\Mode::DBAVAILABLE)) { if ($this->getMode()->has(App\Mode::DBAVAILABLE)) {
@ -382,8 +355,6 @@ class App
Core\L10n::init(); Core\L10n::init();
$this->process_id = Core\System::processID('log'); $this->process_id = Core\System::processID('log');
Core\Logger::setLogger($this->logger);
} }
/** /**

View File

@ -8,10 +8,6 @@
*/ */
namespace Friendica\Core; namespace Friendica\Core;
use Friendica\Core\Config\ConfigCache;
use Friendica\Core\Config\IConfigAdapter;
use Friendica\Core\Config\IConfigCache;
/** /**
* @brief Arbitrary system configuration storage * @brief Arbitrary system configuration storage
* *

View File

@ -55,6 +55,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
{ {
if (isset($this->config[$cat][$key])) { if (isset($this->config[$cat][$key])) {
return $this->config[$cat][$key]; return $this->config[$cat][$key];
} elseif ($key == null && isset($this->config[$cat])) {
return $this->config[$cat];
} else { } else {
return '!<unset>!'; return '!<unset>!';
} }
@ -65,8 +67,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
*/ */
public function has($cat, $key = null) public function has($cat, $key = null)
{ {
return isset($this->config[$cat][$key]) return (isset($this->config[$cat][$key]) && $this->config[$cat][$key] !== '!<unset>!') ||
&& $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) public function hasP($uid, $cat, $key = null)
{ {
return isset($this->config[$uid][$cat][$key]) return (isset($this->config[$uid][$cat][$key]) && $this->config[$uid][$cat][$key] !== '!<unset>!') ||
&& $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])) { if (isset($this->config[$uid][$cat][$key])) {
return $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 { } else {
return '!<unset>!'; return '!<unset>!';
} }

View File

@ -5,8 +5,6 @@
namespace Friendica\Core; namespace Friendica\Core;
use Friendica\BaseObject; use Friendica\BaseObject;
use Friendica\Factory\LoggerFactory;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel; use Psr\Log\LogLevel;
@ -67,73 +65,22 @@ class Logger extends BaseObject
/** /**
* Sets the default logging handler for Friendica. * 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 * @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) 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; 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 * Sets the default dev-logging handler for Friendica.
* @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 * @param LoggerInterface $logger The Logger instance of this Application
*
* @return string the PSR-3 compliant level
*/ */
private static function mapLegacyConfigDebugLevel($level) public static function setDevLogger($logger)
{ {
switch ($level) { self::$devLogger = $logger;
// 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;
}
} }
/** /**

View File

@ -7,6 +7,14 @@ use Friendica\Database;
class DBFactory 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) public static function init(Cache\ConfigCache $configCache, array $server)
{ {
if (Database\DBA::connected()) { if (Database\DBA::connected()) {

View File

@ -35,16 +35,17 @@ class LoggerFactory
$logger->pushProcessor(new Monolog\Processor\UidProcessor()); $logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, [Logger::class, Profiler::class])); $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, [Logger::class, Profiler::class]));
if (isset($config)) { $debugging = $config->get('system', 'debugging');
$debugging = $config->get('system', 'debugging'); $stream = $config->get('system', 'logfile');
$stream = $config->get('system', 'logfile'); $level = $config->get('system', 'loglevel');
$level = $config->get('system', 'loglevel');
if ($debugging) { if ($debugging) {
static::addStreamHandler($logger, $stream, $level); $loglevel = self::mapLegacyConfigDebugLevel((string)$level);
} static::addStreamHandler($logger, $stream, $loglevel);
} }
Logger::setLogger($logger);
return $logger; return $logger;
} }
@ -56,25 +57,71 @@ class LoggerFactory
* *
* It should never get filled during normal usage of Friendica * It should never get filled during normal usage of Friendica
* *
* @param string $channel The channel of the logger instance * @param string $channel The channel of the logger instance
* @param string $developerIp The IP of the developer who wants to use the logger * @param Configuration $config The config
* *
* @return LoggerInterface The PSR-3 compliant logger instance * @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 = new Monolog\Logger($channel);
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor()); $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor()); $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
$logger->pushProcessor(new Monolog\Processor\UidProcessor()); $logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger'])); $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger']));
$logger->pushHandler(new FriendicaDevelopHandler($developerIp)); $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
Logger::setDevLogger($logger);
return $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 * Adding a handler to a given logger instance
* *
@ -106,32 +153,4 @@ class LoggerFactory
throw new InternalServerErrorException('Logger instance incompatible for MonologFactory'); 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');
}
}
} }

View File

@ -41,7 +41,7 @@ class ApiTest extends DatabaseTest
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER); Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache); $config = Factory\ConfigFactory::createConfig($configCache);
$pconfig = Factory\ConfigFactory::createPConfig($configCache); Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('test', $config); $logger = Factory\LoggerFactory::create('test', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config); $profiler = Factory\ProfilerFactory::create($logger, $config);
$this->app = new App($config, $logger, $profiler, false); $this->app = new App($config, $logger, $profiler, false);

View File

@ -149,13 +149,34 @@ class ConfigCacheTest extends MockedTest
$configCache = new ConfigCache(); $configCache = new ConfigCache();
$this->assertFalse($configCache->has('system', 'test')); $this->assertFalse($configCache->has('system', 'test'));
$this->assertFalse($configCache->has('system'));
$configCache->set('system', 'test', 'it'); $configCache->set('system', 'test', 'it');
$this->assertTrue($configCache->has('system', 'test')); $this->assertTrue($configCache->has('system', 'test'));
$this->assertTrue($configCache->has('system'));
}
$this->assertFalse($configCache->has('system', null)); /**
$configCache->set('system', null, 'it'); * Test the get() method with a category
$this->assertTrue($configCache->has('system', null)); */
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 * Test the deleteP() method
* @dataProvider dataTests * @dataProvider dataTests
@ -227,12 +274,10 @@ class ConfigCacheTest extends MockedTest
$uid = 345; $uid = 345;
$this->assertFalse($configCache->hasP($uid, 'system', 'test')); $this->assertFalse($configCache->hasP($uid, 'system', 'test'));
$this->assertFalse($configCache->hasP($uid, 'system'));
$configCache->setP($uid, 'system', 'test', 'it'); $configCache->setP($uid, 'system', 'test', 'it');
$this->assertTrue($configCache->hasP($uid, 'system', 'test')); $this->assertTrue($configCache->hasP($uid, 'system', 'test'));
$this->assertTrue($configCache->hasP($uid, 'system'));
$this->assertFalse($configCache->hasP($uid, 'system', null));
$configCache->setP($uid, 'system', null, 'it');
$this->assertTrue($configCache->hasP($uid, 'system', null));
} }
} }