Browse Source

Refactoring Logging to use Configuration

tags/2019.03^2
Philipp Holzer 7 months ago
parent
commit
80f1feabe5
No account linked to committer's email address

+ 2
- 1
bin/auth_ejabberd.php View File

@@ -59,7 +59,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
59 59
 $configCache = Factory\ConfigFactory::createCache($configLoader);
60 60
 Factory\DBFactory::init($configCache, $_SERVER);
61 61
 $config = Factory\ConfigFactory::createConfig($configCache);
62
-$pconfig = Factory\ConfigFactory::createPConfig($configCache);
62
+// needed to call PConfig::init()
63
+Factory\ConfigFactory::createPConfig($configCache);
63 64
 $logger = Factory\LoggerFactory::create('auth_ejabberd', $config);
64 65
 $profiler = Factory\ProfilerFactory::create($logger, $config);
65 66
 

+ 2
- 1
bin/console.php View File

@@ -12,7 +12,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
12 12
 $configCache = Factory\ConfigFactory::createCache($configLoader);
13 13
 Factory\DBFactory::init($configCache, $_SERVER);
14 14
 $config = Factory\ConfigFactory::createConfig($configCache);
15
-$pconfig = Factory\ConfigFactory::createPConfig($configCache);
15
+// needed to call PConfig::init()
16
+Factory\ConfigFactory::createPConfig($configCache);
16 17
 $logger = Factory\LoggerFactory::create('console', $config);
17 18
 $profiler = Factory\ProfilerFactory::create($logger, $config);
18 19
 

+ 2
- 1
bin/daemon.php View File

@@ -39,7 +39,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
39 39
 $configCache = Factory\ConfigFactory::createCache($configLoader);
40 40
 Factory\DBFactory::init($configCache, $_SERVER);
41 41
 $config = Factory\ConfigFactory::createConfig($configCache);
42
-$pconfig = Factory\ConfigFactory::createPConfig($configCache);
42
+// needed to call PConfig::init()
43
+Factory\ConfigFactory::createPConfig($configCache);
43 44
 $logger = Factory\LoggerFactory::create('daemon', $config);
44 45
 $profiler = Factory\ProfilerFactory::create($logger, $config);
45 46
 

+ 2
- 1
bin/worker.php View File

@@ -37,7 +37,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
37 37
 $configCache = Factory\ConfigFactory::createCache($configLoader);
38 38
 Factory\DBFactory::init($configCache, $_SERVER);
39 39
 $config = Factory\ConfigFactory::createConfig($configCache);
40
-$pconfig = Factory\ConfigFactory::createPConfig($configCache);
40
+// needed to call PConfig::init()
41
+Factory\ConfigFactory::createPConfig($configCache);
41 42
 $logger = Factory\LoggerFactory::create('worker', $config);
42 43
 $profiler = Factory\ProfilerFactory::create($logger, $config);
43 44
 

+ 2
- 1
index.php View File

@@ -20,7 +20,8 @@ $configLoader = new Cache\ConfigCacheLoader($basedir);
20 20
 $configCache = Factory\ConfigFactory::createCache($configLoader);
21 21
 Factory\DBFactory::init($configCache, $_SERVER);
22 22
 $config = Factory\ConfigFactory::createConfig($configCache);
23
-$pconfig = Factory\ConfigFactory::createPConfig($configCache);
23
+// needed to call PConfig::init()
24
+Factory\ConfigFactory::createPConfig($configCache);
24 25
 $logger = Factory\LoggerFactory::create('index', $config);
25 26
 $profiler = Factory\ProfilerFactory::create($logger, $config);
26 27
 

+ 0
- 29
src/App.php View File

@@ -109,11 +109,6 @@ class App
109 109
 	 */
110 110
 	public $mobileDetect;
111 111
 
112
-	/**
113
-	 * @var LoggerInterface The current logger of this App
114
-	 */
115
-	private $logger;
116
-
117 112
 	/**
118 113
 	 * @var Configuration The config
119 114
 	 */
@@ -339,35 +334,13 @@ class App
339 334
 		return $this->mode;
340 335
 	}
341 336
 
342
-	/**
343
-	 * Returns the Logger of the Application
344
-	 *
345
-	 * @return LoggerInterface The Logger
346
-	 * @throws InternalServerErrorException when the logger isn't created
347
-	 */
348
-	public function getLogger()
349
-	{
350
-		if (empty($this->logger)) {
351
-			throw new InternalServerErrorException('Logger of the Application is not defined');
352
-		}
353
-
354
-		return $this->logger;
355
-	}
356
-
357 337
 	/**
358 338
 	 * Reloads the whole app instance
359 339
 	 */
360 340
 	public function reload()
361 341
 	{
362
-		$this->getMode()->determine($this->basePath);
363
-
364 342
 		$this->determineURLPath();
365 343
 
366
-		if ($this->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
367
-			Core\Config::load();
368
-		}
369
-
370
-		// again because DB-config could change the config
371 344
 		$this->getMode()->determine($this->basePath);
372 345
 
373 346
 		if ($this->getMode()->has(App\Mode::DBAVAILABLE)) {
@@ -382,8 +355,6 @@ class App
382 355
 		Core\L10n::init();
383 356
 
384 357
 		$this->process_id = Core\System::processID('log');
385
-
386
-		Core\Logger::setLogger($this->logger);
387 358
 	}
388 359
 
389 360
 	/**

+ 0
- 4
src/Core/Config.php View File

@@ -8,10 +8,6 @@
8 8
  */
9 9
 namespace Friendica\Core;
10 10
 
11
-use Friendica\Core\Config\ConfigCache;
12
-use Friendica\Core\Config\IConfigAdapter;
13
-use Friendica\Core\Config\IConfigCache;
14
-
15 11
 /**
16 12
  * @brief Arbitrary system configuration storage
17 13
  *

+ 8
- 4
src/Core/Config/Cache/ConfigCache.php View File

@@ -55,6 +55,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
55 55
 	{
56 56
 		if (isset($this->config[$cat][$key])) {
57 57
 			return $this->config[$cat][$key];
58
+		} elseif ($key == null && isset($this->config[$cat])) {
59
+			return $this->config[$cat];
58 60
 		} else {
59 61
 			return '!<unset>!';
60 62
 		}
@@ -65,8 +67,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
65 67
 	 */
66 68
 	public function has($cat, $key = null)
67 69
 	{
68
-		return isset($this->config[$cat][$key])
69
-			&& $this->config[$cat][$key] !== '!<unset>!';
70
+		return (isset($this->config[$cat][$key]) && $this->config[$cat][$key] !== '!<unset>!') ||
71
+		($key == null && isset($this->config[$cat]) && $this->config[$cat] !== '!<unset>!' && is_array($this->config[$cat]));
70 72
 	}
71 73
 
72 74
 	/**
@@ -105,8 +107,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
105 107
 	 */
106 108
 	public function hasP($uid, $cat, $key = null)
107 109
 	{
108
-		return isset($this->config[$uid][$cat][$key])
109
-			&& $this->config[$uid][$cat][$key] !== '!<unset>!';
110
+		return (isset($this->config[$uid][$cat][$key]) && $this->config[$uid][$cat][$key] !== '!<unset>!') ||
111
+			($key == null && isset($this->config[$uid][$cat]) && $this->config[$uid][$cat] !== '!<unset>!' && is_array($this->config[$uid][$cat]));
110 112
 	}
111 113
 
112 114
 	/**
@@ -144,6 +146,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
144 146
 	{
145 147
 		if (isset($this->config[$uid][$cat][$key])) {
146 148
 			return $this->config[$uid][$cat][$key];
149
+		} elseif ($key == null && isset($this->config[$uid][$cat])) {
150
+			return $this->config[$uid][$cat];
147 151
 		} else {
148 152
 			return '!<unset>!';
149 153
 		}

+ 4
- 57
src/Core/Logger.php View File

@@ -5,8 +5,6 @@
5 5
 namespace Friendica\Core;
6 6
 
7 7
 use Friendica\BaseObject;
8
-use Friendica\Factory\LoggerFactory;
9
-use Friendica\Network\HTTPException\InternalServerErrorException;
10 8
 use Psr\Log\LoggerInterface;
11 9
 use Psr\Log\LogLevel;
12 10
 
@@ -67,73 +65,22 @@ class Logger extends BaseObject
67 65
 
68 66
 	/**
69 67
 	 * Sets the default logging handler for Friendica.
70
-	 * @todo Can be combined with other handlers too if necessary, could be configurable.
71 68
 	 *
72 69
 	 * @param LoggerInterface $logger The Logger instance of this Application
73
-	 *
74
-	 * @throws InternalServerErrorException if the logger factory is incompatible to this logger
75 70
 	 */
76 71
 	public static function setLogger($logger)
77 72
 	{
78
-		$debugging = Config::get('system', 'debugging');
79
-		$logfile = Config::get('system', 'logfile');
80
-		$loglevel = Config::get('system', 'loglevel');
81
-
82
-		if (!$debugging || !$logfile) {
83
-			return;
84
-		}
85
-
86
-		$loglevel = self::mapLegacyConfigDebugLevel((string)$loglevel);
87
-
88
-		LoggerFactory::addStreamHandler($logger, $logfile, $loglevel);
89
-
90 73
 		self::$logger = $logger;
91
-
92
-		$logfile = Config::get('system', 'dlogfile');
93
-
94
-		if (!$logfile) {
95
-			return;
96
-		}
97
-
98
-		$developIp = Config::get('system', 'dlogip');
99
-
100
-		self::$devLogger = LoggerFactory::createDev('develop', $developIp);
101
-		LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
102 74
 	}
103 75
 
104 76
 	/**
105
-	 * Mapping a legacy level to the PSR-3 compliant levels
106
-	 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
107
-	 *
108
-	 * @param string $level the level to be mapped
77
+	 * Sets the default dev-logging handler for Friendica.
109 78
 	 *
110
-	 * @return string the PSR-3 compliant level
79
+	 * @param LoggerInterface $logger The Logger instance of this Application
111 80
 	 */
112
-	private static function mapLegacyConfigDebugLevel($level)
81
+	public static function setDevLogger($logger)
113 82
 	{
114
-		switch ($level) {
115
-			// legacy WARNING
116
-			case "0":
117
-				return LogLevel::ERROR;
118
-			// legacy INFO
119
-			case "1":
120
-				return LogLevel::WARNING;
121
-			// legacy TRACE
122
-			case "2":
123
-				return LogLevel::NOTICE;
124
-			// legacy DEBUG
125
-			case "3":
126
-				return LogLevel::INFO;
127
-			// legacy DATA
128
-			case "4":
129
-				return LogLevel::DEBUG;
130
-			// legacy ALL
131
-			case "5":
132
-				return LogLevel::DEBUG;
133
-			// default if nothing set
134
-			default:
135
-				return $level;
136
-		}
83
+		self::$devLogger = $logger;
137 84
 	}
138 85
 
139 86
 	/**

+ 8
- 0
src/Factory/DBFactory.php View File

@@ -7,6 +7,14 @@ use Friendica\Database;
7 7
 
8 8
 class DBFactory
9 9
 {
10
+	/**
11
+	 * Initialize the DBA connection
12
+	 *
13
+	 * @param Cache\ConfigCache $configCache The configuration cache
14
+	 * @param array             $server      The $_SERVER variables
15
+	 *
16
+	 * @throws \Exception if connection went bad
17
+	 */
10 18
 	public static function init(Cache\ConfigCache $configCache, array $server)
11 19
 	{
12 20
 		if (Database\DBA::connected()) {

+ 58
- 39
src/Factory/LoggerFactory.php View File

@@ -35,16 +35,17 @@ class LoggerFactory
35 35
 		$logger->pushProcessor(new Monolog\Processor\UidProcessor());
36 36
 		$logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, [Logger::class, Profiler::class]));
37 37
 
38
-		if (isset($config)) {
39
-			$debugging = $config->get('system', 'debugging');
40
-			$stream = $config->get('system', 'logfile');
41
-			$level = $config->get('system', 'loglevel');
38
+		$debugging = $config->get('system', 'debugging');
39
+		$stream    = $config->get('system', 'logfile');
40
+		$level     = $config->get('system', 'loglevel');
42 41
 
43
-			if ($debugging) {
44
-				static::addStreamHandler($logger, $stream, $level);
45
-			}
42
+		if ($debugging) {
43
+			$loglevel = self::mapLegacyConfigDebugLevel((string)$level);
44
+			static::addStreamHandler($logger, $stream, $loglevel);
46 45
 		}
47 46
 
47
+		Logger::setLogger($logger);
48
+
48 49
 		return $logger;
49 50
 	}
50 51
 
@@ -56,25 +57,71 @@ class LoggerFactory
56 57
 	 *
57 58
 	 * It should never get filled during normal usage of Friendica
58 59
 	 *
59
-	 * @param string $channel      The channel of the logger instance
60
-	 * @param string $developerIp  The IP of the developer who wants to use the logger
60
+	 * @param string        $channel The channel of the logger instance
61
+	 * @param Configuration $config  The config
61 62
 	 *
62 63
 	 * @return LoggerInterface The PSR-3 compliant logger instance
63 64
 	 */
64
-	public static function createDev($channel, $developerIp)
65
+	public static function createDev($channel, Configuration $config)
65 66
 	{
67
+		$debugging   = $config->get('system', 'debugging');
68
+		$stream      = $config->get('system', 'dlogfile');
69
+		$developerIp = $config->get('system', 'dlogip');
70
+
71
+		if (!isset($developerIp) || !$debugging) {
72
+			return null;
73
+		}
74
+
66 75
 		$logger = new Monolog\Logger($channel);
67 76
 		$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
68 77
 		$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
69 78
 		$logger->pushProcessor(new Monolog\Processor\UidProcessor());
70 79
 		$logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger']));
71 80
 
72
-
73 81
 		$logger->pushHandler(new FriendicaDevelopHandler($developerIp));
74 82
 
83
+		static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
84
+
85
+		Logger::setDevLogger($logger);
86
+
75 87
 		return $logger;
76 88
 	}
77 89
 
90
+	/**
91
+	 * Mapping a legacy level to the PSR-3 compliant levels
92
+	 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
93
+	 *
94
+	 * @param string $level the level to be mapped
95
+	 *
96
+	 * @return string the PSR-3 compliant level
97
+	 */
98
+	private static function mapLegacyConfigDebugLevel($level)
99
+	{
100
+		switch ($level) {
101
+			// legacy WARNING
102
+			case "0":
103
+				return LogLevel::ERROR;
104
+			// legacy INFO
105
+			case "1":
106
+				return LogLevel::WARNING;
107
+			// legacy TRACE
108
+			case "2":
109
+				return LogLevel::NOTICE;
110
+			// legacy DEBUG
111
+			case "3":
112
+				return LogLevel::INFO;
113
+			// legacy DATA
114
+			case "4":
115
+				return LogLevel::DEBUG;
116
+			// legacy ALL
117
+			case "5":
118
+				return LogLevel::DEBUG;
119
+			// default if nothing set
120
+			default:
121
+				return $level;
122
+		}
123
+	}
124
+
78 125
 	/**
79 126
 	 * Adding a handler to a given logger instance
80 127
 	 *
@@ -106,32 +153,4 @@ class LoggerFactory
106 153
 			throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
107 154
 		}
108 155
 	}
109
-
110
-	/**
111
-	 * This method enables the test mode of a given logger
112
-	 *
113
-	 * @param LoggerInterface $logger The logger
114
-	 *
115
-	 * @return Monolog\Handler\TestHandler the Handling for tests
116
-	 *
117
-	 * @throws InternalServerErrorException if the logger is incompatible to the logger factory
118
-	 */
119
-	public static function enableTest($logger)
120
-	{
121
-		if ($logger instanceof Monolog\Logger) {
122
-			// disable every handler so far
123
-			$logger->pushHandler(new Monolog\Handler\NullHandler());
124
-
125
-			// enable the test handler
126
-			$fileHandler = new Monolog\Handler\TestHandler();
127
-			$formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
128
-			$fileHandler->setFormatter($formatter);
129
-
130
-			$logger->pushHandler($fileHandler);
131
-
132
-			return $fileHandler;
133
-		} else {
134
-			throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
135
-		}
136
-	}
137 156
 }

+ 1
- 1
tests/include/ApiTest.php View File

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

+ 52
- 7
tests/src/Core/Config/Cache/ConfigCacheTest.php View File

@@ -149,13 +149,34 @@ class ConfigCacheTest extends MockedTest
149 149
 		$configCache = new ConfigCache();
150 150
 
151 151
 		$this->assertFalse($configCache->has('system', 'test'));
152
+		$this->assertFalse($configCache->has('system'));
152 153
 
153 154
 		$configCache->set('system', 'test', 'it');
154 155
 		$this->assertTrue($configCache->has('system', 'test'));
156
+		$this->assertTrue($configCache->has('system'));
157
+	}
155 158
 
156
-		$this->assertFalse($configCache->has('system', null));
157
-		$configCache->set('system', null, 'it');
158
-		$this->assertTrue($configCache->has('system', null));
159
+	/**
160
+	 * Test the get() method with a category
161
+	 */
162
+	public function testGetCat()
163
+	{
164
+		$configCache = new ConfigCache([
165
+			'system' => [
166
+				'key1' => 'value1',
167
+				'key2' => 'value2',
168
+			],
169
+			'config' => [
170
+				'key3' => 'value3',
171
+			],
172
+		]);
173
+
174
+		$this->assertTrue($configCache->has('system'));
175
+
176
+		$this->assertEquals([
177
+			'key1' => 'value1',
178
+			'key2' => 'value2',
179
+		], $configCache->get('system'));
159 180
 	}
160 181
 
161 182
 	/**
@@ -194,6 +215,32 @@ class ConfigCacheTest extends MockedTest
194 215
 	}
195 216
 
196 217
 
218
+	/**
219
+	 * Test the getP() method with a category
220
+	 */
221
+	public function testGetPCat()
222
+	{
223
+		$configCache = new ConfigCache();
224
+		$uid = 345;
225
+
226
+		$configCache->loadP($uid, [
227
+			'system' => [
228
+				'key1' => 'value1',
229
+				'key2' => 'value2',
230
+			],
231
+			'config' => [
232
+				'key3' => 'value3',
233
+			],
234
+		]);
235
+
236
+		$this->assertTrue($configCache->hasP($uid,'system'));
237
+
238
+		$this->assertEquals([
239
+			'key1' => 'value1',
240
+			'key2' => 'value2',
241
+		], $configCache->get($uid, 'system'));
242
+	}
243
+
197 244
 	/**
198 245
 	 * Test the deleteP() method
199 246
 	 * @dataProvider dataTests
@@ -227,12 +274,10 @@ class ConfigCacheTest extends MockedTest
227 274
 		$uid = 345;
228 275
 
229 276
 		$this->assertFalse($configCache->hasP($uid, 'system', 'test'));
277
+		$this->assertFalse($configCache->hasP($uid, 'system'));
230 278
 
231 279
 		$configCache->setP($uid, 'system', 'test', 'it');
232 280
 		$this->assertTrue($configCache->hasP($uid, 'system', 'test'));
233
-
234
-		$this->assertFalse($configCache->hasP($uid, 'system', null));
235
-		$configCache->setP($uid, 'system', null, 'it');
236
-		$this->assertTrue($configCache->hasP($uid, 'system', null));
281
+		$this->assertTrue($configCache->hasP($uid, 'system'));
237 282
 	}
238 283
 }

Loading…
Cancel
Save