2018-12-30 15:42:56 -05:00
|
|
|
<?php
|
|
|
|
|
2019-02-03 16:22:04 -05:00
|
|
|
namespace Friendica\Factory;
|
2018-12-30 15:42:56 -05:00
|
|
|
|
2019-02-10 13:52:21 -05:00
|
|
|
use Friendica\Core\Config\Configuration;
|
2019-02-16 19:56:46 -05:00
|
|
|
use Friendica\Core\Logger;
|
2019-06-06 18:10:45 -04:00
|
|
|
use Friendica\Database\Database;
|
2018-12-30 15:42:56 -05:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
2019-10-22 16:47:37 -04:00
|
|
|
use Friendica\Util\FileSystem;
|
2019-02-28 02:56:28 -05:00
|
|
|
use Friendica\Util\Introspection;
|
2019-03-04 02:42:08 -05:00
|
|
|
use Friendica\Util\Logger\Monolog\DevelopHandler;
|
|
|
|
use Friendica\Util\Logger\Monolog\IntrospectionProcessor;
|
2019-03-03 14:32:27 -05:00
|
|
|
use Friendica\Util\Logger\ProfilerLogger;
|
2019-02-28 03:48:55 -05:00
|
|
|
use Friendica\Util\Logger\StreamLogger;
|
2019-02-27 10:40:35 -05:00
|
|
|
use Friendica\Util\Logger\SyslogLogger;
|
2019-02-27 12:55:26 -05:00
|
|
|
use Friendica\Util\Logger\VoidLogger;
|
2019-02-16 19:56:46 -05:00
|
|
|
use Friendica\Util\Profiler;
|
2018-12-30 15:42:56 -05:00
|
|
|
use Monolog;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\LogLevel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A logger factory
|
|
|
|
*
|
|
|
|
* Currently only Monolog is supported
|
|
|
|
*/
|
|
|
|
class LoggerFactory
|
|
|
|
{
|
2019-07-21 14:24:16 -04:00
|
|
|
const DEV_CHANNEL = 'dev';
|
|
|
|
|
2019-02-24 07:40:54 -05:00
|
|
|
/**
|
|
|
|
* A list of classes, which shouldn't get logged
|
2019-05-26 08:33:09 -04:00
|
|
|
*
|
2019-02-24 07:40:54 -05:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $ignoreClassList = [
|
|
|
|
Logger::class,
|
|
|
|
Profiler::class,
|
2019-03-23 13:31:22 -04:00
|
|
|
'Friendica\\Util\\Logger',
|
2019-02-24 07:40:54 -05:00
|
|
|
];
|
|
|
|
|
2019-09-17 10:47:00 -04:00
|
|
|
private $channel;
|
|
|
|
|
|
|
|
public function __construct(string $channel)
|
2019-07-21 14:24:16 -04:00
|
|
|
{
|
2019-09-17 10:47:00 -04:00
|
|
|
$this->channel = $channel;
|
2019-07-21 14:24:16 -04:00
|
|
|
}
|
|
|
|
|
2018-12-30 15:42:56 -05:00
|
|
|
/**
|
|
|
|
* Creates a new PSR-3 compliant logger instances
|
|
|
|
*
|
2019-09-17 10:47:00 -04:00
|
|
|
* @param Database $database The Friendica Database instance
|
2019-05-26 08:33:09 -04:00
|
|
|
* @param Configuration $config The config
|
2019-02-28 03:41:31 -05:00
|
|
|
* @param Profiler $profiler The profiler of the app
|
2019-10-22 16:47:37 -04:00
|
|
|
* @param FileSystem $fileSystem FileSystem utils
|
2018-12-30 15:42:56 -05:00
|
|
|
*
|
|
|
|
* @return LoggerInterface The PSR-3 compliant logger instance
|
|
|
|
*/
|
2019-10-22 16:47:37 -04:00
|
|
|
public function create(Database $database, Configuration $config, Profiler $profiler, FileSystem $fileSystem)
|
2018-12-30 15:42:56 -05:00
|
|
|
{
|
2019-02-27 12:55:26 -05:00
|
|
|
if (empty($config->get('system', 'debugging', false))) {
|
2019-03-02 08:40:59 -05:00
|
|
|
$logger = new VoidLogger();
|
2019-06-06 18:10:45 -04:00
|
|
|
$database->setLogger($logger);
|
2019-03-02 08:40:59 -05:00
|
|
|
return $logger;
|
2019-02-27 12:55:26 -05:00
|
|
|
}
|
|
|
|
|
2019-02-28 02:56:28 -05:00
|
|
|
$introspection = new Introspection(self::$ignoreClassList);
|
2019-05-26 08:33:09 -04:00
|
|
|
$level = $config->get('system', 'loglevel');
|
|
|
|
$loglevel = self::mapLegacyConfigDebugLevel((string)$level);
|
2019-02-28 02:56:28 -05:00
|
|
|
|
2019-03-03 14:32:27 -05:00
|
|
|
switch ($config->get('system', 'logger_config', 'stream')) {
|
2019-02-27 10:40:35 -05:00
|
|
|
case 'monolog':
|
|
|
|
$loggerTimeZone = new \DateTimeZone('UTC');
|
|
|
|
Monolog\Logger::setTimezone($loggerTimeZone);
|
|
|
|
|
2019-09-17 10:47:00 -04:00
|
|
|
$logger = new Monolog\Logger($this->channel);
|
2019-02-27 10:40:35 -05:00
|
|
|
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
|
|
|
|
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
|
|
|
|
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
|
2019-03-04 02:42:08 -05:00
|
|
|
$logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG));
|
2019-02-27 10:40:35 -05:00
|
|
|
|
2019-02-27 12:55:26 -05:00
|
|
|
$stream = $config->get('system', 'logfile');
|
2019-02-27 10:40:35 -05:00
|
|
|
|
2019-05-26 08:33:09 -04:00
|
|
|
// just add a stream in case it's either writable or not file
|
|
|
|
if (!is_file($stream) || is_writable($stream)) {
|
2019-10-21 15:53:55 -04:00
|
|
|
try {
|
|
|
|
static::addStreamHandler($logger, $stream, $loglevel);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// No Logger ..
|
|
|
|
$logger = new VoidLogger();
|
|
|
|
}
|
2019-05-26 08:33:09 -04:00
|
|
|
}
|
2019-02-27 10:40:35 -05:00
|
|
|
break;
|
2019-03-03 14:32:27 -05:00
|
|
|
|
|
|
|
case 'syslog':
|
2019-10-21 15:53:55 -04:00
|
|
|
try {
|
|
|
|
$logger = new SyslogLogger($this->channel, $introspection, $loglevel);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// No logger ...
|
|
|
|
$logger = new VoidLogger();
|
|
|
|
}
|
2019-03-03 14:32:27 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'stream':
|
|
|
|
default:
|
|
|
|
$stream = $config->get('system', 'logfile');
|
2019-05-26 08:33:09 -04:00
|
|
|
// just add a stream in case it's either writable or not file
|
|
|
|
if (!is_file($stream) || is_writable($stream)) {
|
2019-10-21 15:53:55 -04:00
|
|
|
try {
|
2019-10-22 16:47:37 -04:00
|
|
|
$logger = new StreamLogger($this->channel, $stream, $introspection, $fileSystem, $loglevel);
|
2019-10-21 15:53:55 -04:00
|
|
|
} catch (\Throwable $t) {
|
|
|
|
// No logger ...
|
|
|
|
$logger = new VoidLogger();
|
|
|
|
}
|
2019-05-26 08:33:09 -04:00
|
|
|
} else {
|
|
|
|
$logger = new VoidLogger();
|
|
|
|
}
|
2019-03-03 14:32:27 -05: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 16:22:04 -05:00
|
|
|
}
|
|
|
|
|
2019-06-06 18:10:45 -04:00
|
|
|
$database->setLogger($logger);
|
2018-12-30 15:42:56 -05: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
|
|
|
|
*
|
2019-05-26 08:33:09 -04:00
|
|
|
* @param Configuration $config The config
|
2019-03-03 14:32:27 -05:00
|
|
|
* @param Profiler $profiler The profiler of the app
|
2019-10-22 16:47:37 -04:00
|
|
|
* @param FileSystem $fileSystem FileSystem utils
|
2018-12-30 15:42:56 -05:00
|
|
|
*
|
|
|
|
* @return LoggerInterface The PSR-3 compliant logger instance
|
2019-03-03 14:32:27 -05:00
|
|
|
*
|
2019-02-27 10:40:35 -05:00
|
|
|
* @throws InternalServerErrorException
|
2019-03-03 14:32:27 -05:00
|
|
|
* @throws \Exception
|
2018-12-30 15:42:56 -05:00
|
|
|
*/
|
2019-10-22 16:47:37 -04:00
|
|
|
public static function createDev(Configuration $config, Profiler $profiler, FileSystem $fileSystem)
|
2018-12-30 15:42:56 -05:00
|
|
|
{
|
2019-02-11 15:13:53 -05:00
|
|
|
$debugging = $config->get('system', 'debugging');
|
|
|
|
$stream = $config->get('system', 'dlogfile');
|
|
|
|
$developerIp = $config->get('system', 'dlogip');
|
|
|
|
|
2019-05-26 08:33:09 -04:00
|
|
|
if ((!isset($developerIp) || !$debugging) &&
|
|
|
|
(!is_file($stream) || is_writable($stream))) {
|
2019-03-03 14:32:27 -05:00
|
|
|
$logger = new VoidLogger();
|
|
|
|
return $logger;
|
2019-02-11 15:13:53 -05:00
|
|
|
}
|
|
|
|
|
2019-03-09 08:28:31 -05:00
|
|
|
$loggerTimeZone = new \DateTimeZone('UTC');
|
|
|
|
Monolog\Logger::setTimezone($loggerTimeZone);
|
|
|
|
|
2019-02-28 02:56:28 -05:00
|
|
|
$introspection = new Introspection(self::$ignoreClassList);
|
|
|
|
|
2019-03-03 14:32:27 -05:00
|
|
|
switch ($config->get('system', 'logger_config', 'stream')) {
|
2018-12-30 15:42:56 -05:00
|
|
|
|
2019-03-03 14:32:27 -05:00
|
|
|
case 'monolog':
|
2019-03-09 08:36:32 -05:00
|
|
|
$loggerTimeZone = new \DateTimeZone('UTC');
|
|
|
|
Monolog\Logger::setTimezone($loggerTimeZone);
|
|
|
|
|
2019-07-21 14:24:16 -04:00
|
|
|
$logger = new Monolog\Logger(self::DEV_CHANNEL);
|
2019-03-03 14:32:27 -05:00
|
|
|
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
|
|
|
|
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
|
|
|
|
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
|
2019-03-04 02:42:08 -05:00
|
|
|
$logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG));
|
2019-03-03 14:32:27 -05:00
|
|
|
|
2019-03-04 02:42:08 -05:00
|
|
|
$logger->pushHandler(new DevelopHandler($developerIp));
|
2019-03-03 14:32:27 -05:00
|
|
|
|
|
|
|
static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
|
|
|
|
break;
|
2018-12-30 15:42:56 -05:00
|
|
|
|
2019-03-03 14:32:27 -05:00
|
|
|
case 'syslog':
|
2019-07-21 14:24:16 -04:00
|
|
|
$logger = new SyslogLogger(self::DEV_CHANNEL, $introspection, LogLevel::DEBUG);
|
2019-03-03 14:32:27 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'stream':
|
|
|
|
default:
|
2019-10-22 16:47:37 -04:00
|
|
|
$logger = new StreamLogger(self::DEV_CHANNEL, $stream, $introspection, $fileSystem, LogLevel::DEBUG);
|
2019-03-03 14:32:27 -05: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-11 15:13:53 -05:00
|
|
|
|
2018-12-30 15:42:56 -05:00
|
|
|
return $logger;
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:13:53 -05:00
|
|
|
/**
|
|
|
|
* Mapping a legacy level to the PSR-3 compliant levels
|
2019-05-26 08:33:09 -04:00
|
|
|
*
|
2019-02-11 15:13:53 -05:00
|
|
|
* @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 "5":
|
2019-10-21 15:53:55 -04:00
|
|
|
// legacy ALL
|
|
|
|
case "4":
|
2019-10-22 16:47:37 -04:00
|
|
|
return LogLevel::DEBUG;
|
2019-02-11 15:13:53 -05:00
|
|
|
// default if nothing set
|
|
|
|
default:
|
|
|
|
return $level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 15:42:56 -05:00
|
|
|
/**
|
|
|
|
* Adding a handler to a given logger instance
|
|
|
|
*
|
2019-05-26 08:33:09 -04:00
|
|
|
* @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 15:42:56 -05:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @throws \Exception in case of general failures
|
|
|
|
*/
|
|
|
|
public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
|
|
|
|
{
|
|
|
|
if ($logger instanceof Monolog\Logger) {
|
2019-01-07 15:54:40 -05: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 07:40:54 -05:00
|
|
|
|
2019-01-07 15:54:40 -05:00
|
|
|
$fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
|
2018-12-30 15:42:56 -05:00
|
|
|
|
|
|
|
$formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
|
|
|
|
$fileHandler->setFormatter($formatter);
|
|
|
|
|
|
|
|
$logger->pushHandler($fileHandler);
|
|
|
|
}
|
|
|
|
}
|
2019-02-17 15:12:12 -05:00
|
|
|
|
|
|
|
public static function addVoidHandler($logger)
|
|
|
|
{
|
|
|
|
if ($logger instanceof Monolog\Logger) {
|
|
|
|
$logger->pushHandler(new Monolog\Handler\NullHandler());
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 15:42:56 -05:00
|
|
|
}
|