friendica_2021-01/src/Factory/LoggerFactory.php

259 lines
7.5 KiB
PHP
Raw Normal View History

2018-12-30 21:42:56 +01:00
<?php
2019-02-03 22:22:04 +01:00
namespace Friendica\Factory;
2018-12-30 21:42:56 +01:00
use Friendica\Core\Config\Configuration;
use Friendica\Core\Logger;
2018-12-30 21:42:56 +01:00
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\Introspection;
2019-03-04 08:42:08 +01:00
use Friendica\Util\Logger\Monolog\DevelopHandler;
use Friendica\Util\Logger\Monolog\IntrospectionProcessor;
2019-03-03 20:32:27 +01:00
use Friendica\Util\Logger\ProfilerLogger;
2019-02-28 09:48:55 +01:00
use Friendica\Util\Logger\StreamLogger;
2019-02-27 16:40:35 +01:00
use Friendica\Util\Logger\SyslogLogger;
2019-02-27 18:55:26 +01:00
use Friendica\Util\Logger\VoidLogger;
use Friendica\Util\Profiler;
2018-12-30 21:42:56 +01:00
use Monolog;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* A logger factory
*
* Currently only Monolog is supported
*/
class LoggerFactory
{
2019-02-24 13:40:54 +01:00
/**
* A list of classes, which shouldn't get logged
*
2019-02-24 13:40:54 +01:00
* @var array
*/
private static $ignoreClassList = [
Logger::class,
Profiler::class,
2019-03-23 18:31:22 +01:00
'Friendica\\Util\\Logger',
2019-02-24 13:40:54 +01:00
];
2018-12-30 21:42:56 +01:00
/**
* Creates a new PSR-3 compliant logger instances
*
* @param string $channel The channel of the logger instance
* @param Configuration $config The config
2019-02-28 09:41:31 +01:00
* @param Profiler $profiler The profiler of the app
2018-12-30 21:42:56 +01:00
*
* @return LoggerInterface The PSR-3 compliant logger instance
*
* @throws \Exception
2019-02-27 16:40:35 +01:00
* @throws InternalServerErrorException
2018-12-30 21:42:56 +01:00
*/
2019-02-28 09:41:31 +01:00
public static function create($channel, Configuration $config, Profiler $profiler)
2018-12-30 21:42:56 +01:00
{
2019-02-27 18:55:26 +01:00
if (empty($config->get('system', 'debugging', false))) {
$logger = new VoidLogger();
Logger::init($logger);
return $logger;
2019-02-27 18:55:26 +01:00
}
$introspection = new Introspection(self::$ignoreClassList);
$level = $config->get('system', 'loglevel');
$loglevel = self::mapLegacyConfigDebugLevel((string)$level);
2019-03-03 20:32:27 +01:00
switch ($config->get('system', 'logger_config', 'stream')) {
2019-02-27 16:40:35 +01:00
case 'monolog':
$loggerTimeZone = new \DateTimeZone('UTC');
Monolog\Logger::setTimezone($loggerTimeZone);
$logger = new Monolog\Logger($channel);
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
2019-03-04 08:42:08 +01:00
$logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG));
2019-02-27 16:40:35 +01:00
2019-02-27 18:55:26 +01:00
$stream = $config->get('system', 'logfile');
2019-02-27 16:40:35 +01:00
// just add a stream in case it's either writable or not file
if (!is_file($stream) || is_writable($stream)) {
static::addStreamHandler($logger, $stream, $loglevel);
}
2019-02-27 16:40:35 +01:00
break;
2019-03-03 20:32:27 +01:00
case 'syslog':
2019-03-17 10:04:16 +01:00
$logger = new SyslogLogger($channel, $introspection, $loglevel);
2019-03-03 20:32:27 +01:00
break;
case 'stream':
default:
$stream = $config->get('system', 'logfile');
// just add a stream in case it's either writable or not file
if (!is_file($stream) || is_writable($stream)) {
$logger = new StreamLogger($channel, $stream, $introspection, $loglevel);
} else {
$logger = new VoidLogger();
}
2019-03-03 20:32:27 +01:00
break;
}
$profiling = $config->get('system', 'profiling', false);
// In case profiling is enabled, wrap the ProfilerLogger around the current logger
if (isset($profiling) && $profiling !== false) {
$logger = new ProfilerLogger($logger, $profiler);
2019-02-03 22:22:04 +01:00
}
Logger::init($logger);
2018-12-30 21:42:56 +01:00
return $logger;
}
/**
* Creates a new PSR-3 compliant develop logger
*
* If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
* you'll use this logger instance for the duration of your work.
*
* It should never get filled during normal usage of Friendica
*
* @param string $channel The channel of the logger instance
* @param Configuration $config The config
2019-03-03 20:32:27 +01:00
* @param Profiler $profiler The profiler of the app
2018-12-30 21:42:56 +01:00
*
* @return LoggerInterface The PSR-3 compliant logger instance
2019-03-03 20:32:27 +01:00
*
2019-02-27 16:40:35 +01:00
* @throws InternalServerErrorException
2019-03-03 20:32:27 +01:00
* @throws \Exception
2018-12-30 21:42:56 +01:00
*/
2019-03-03 20:32:27 +01:00
public static function createDev($channel, Configuration $config, Profiler $profiler)
2018-12-30 21:42:56 +01:00
{
$debugging = $config->get('system', 'debugging');
$stream = $config->get('system', 'dlogfile');
$developerIp = $config->get('system', 'dlogip');
if ((!isset($developerIp) || !$debugging) &&
(!is_file($stream) || is_writable($stream))) {
2019-03-03 20:32:27 +01:00
$logger = new VoidLogger();
Logger::setDevLogger($logger);
return $logger;
}
2019-03-09 14:28:31 +01:00
$loggerTimeZone = new \DateTimeZone('UTC');
Monolog\Logger::setTimezone($loggerTimeZone);
$introspection = new Introspection(self::$ignoreClassList);
2019-03-03 20:32:27 +01:00
switch ($config->get('system', 'logger_config', 'stream')) {
2018-12-30 21:42:56 +01:00
2019-03-03 20:32:27 +01:00
case 'monolog':
$loggerTimeZone = new \DateTimeZone('UTC');
Monolog\Logger::setTimezone($loggerTimeZone);
2019-03-03 20:32:27 +01:00
$logger = new Monolog\Logger($channel);
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
2019-03-04 08:42:08 +01:00
$logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG));
2019-03-03 20:32:27 +01:00
2019-03-04 08:42:08 +01:00
$logger->pushHandler(new DevelopHandler($developerIp));
2019-03-03 20:32:27 +01:00
static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
break;
2018-12-30 21:42:56 +01:00
2019-03-03 20:32:27 +01:00
case 'syslog':
$logger = new SyslogLogger($channel, $introspection, LogLevel::DEBUG);
2019-03-03 20:32:27 +01:00
break;
case 'stream':
default:
$logger = new StreamLogger($channel, $stream, $introspection, LogLevel::DEBUG);
break;
}
$profiling = $config->get('system', 'profiling', false);
// In case profiling is enabled, wrap the ProfilerLogger around the current logger
if (isset($profiling) && $profiling !== false) {
$logger = new ProfilerLogger($logger, $profiler);
}
Logger::setDevLogger($logger);
2018-12-30 21:42:56 +01:00
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;
}
}
2018-12-30 21:42:56 +01:00
/**
* Adding a handler to a given logger instance
*
* @param LoggerInterface $logger The logger instance
* @param mixed $stream The stream which handles the logger output
* @param string $level The level, for which this handler at least should handle logging
2018-12-30 21:42:56 +01:00
*
* @return void
*
* @throws InternalServerErrorException if the logger is incompatible to the logger factory
* @throws \Exception in case of general failures
*/
public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
{
if ($logger instanceof Monolog\Logger) {
2019-01-07 21:54:40 +01:00
$loglevel = Monolog\Logger::toMonologLevel($level);
// fallback to notice if an invalid loglevel is set
if (!is_int($loglevel)) {
$loglevel = LogLevel::NOTICE;
}
2019-02-24 13:40:54 +01:00
2019-01-07 21:54:40 +01:00
$fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
2018-12-30 21:42:56 +01:00
$formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
$fileHandler->setFormatter($formatter);
$logger->pushHandler($fileHandler);
} else {
throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
}
}
public static function addVoidHandler($logger)
{
if ($logger instanceof Monolog\Logger) {
$logger->pushHandler(new Monolog\Handler\NullHandler());
}
}
2018-12-30 21:42:56 +01:00
}