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-03 16:22:04 -05:00
|
|
|
use Friendica\Core\Config\ConfigCache;
|
2019-02-16 19:56:46 -05:00
|
|
|
use Friendica\Core\Logger;
|
2018-12-30 15:42:56 -05:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
|
|
|
use Friendica\Util\Logger\FriendicaDevelopHandler;
|
2019-01-28 12:41:29 -05:00
|
|
|
use Friendica\Util\Logger\FriendicaIntrospectionProcessor;
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Creates a new PSR-3 compliant logger instances
|
|
|
|
*
|
2019-02-03 16:22:04 -05:00
|
|
|
* @param string $channel The channel of the logger instance
|
|
|
|
* @param ConfigCache $config The config
|
2018-12-30 15:42:56 -05:00
|
|
|
*
|
|
|
|
* @return LoggerInterface The PSR-3 compliant logger instance
|
|
|
|
*/
|
2019-02-04 03:35:01 -05:00
|
|
|
public static function create($channel, ConfigCache $config = null)
|
2018-12-30 15:42:56 -05:00
|
|
|
{
|
|
|
|
$logger = new Monolog\Logger($channel);
|
|
|
|
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
|
|
|
|
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
|
2019-01-28 05:21:48 -05:00
|
|
|
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
|
2019-02-16 19:56:46 -05:00
|
|
|
$logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, [Logger::class, Profiler::class]));
|
2018-12-30 15:42:56 -05:00
|
|
|
|
2019-02-03 16:22:04 -05:00
|
|
|
if (isset($config)) {
|
|
|
|
$debugging = $config->get('system', 'debugging');
|
|
|
|
$stream = $config->get('system', 'logfile');
|
|
|
|
$level = $config->get('system', 'loglevel');
|
|
|
|
|
|
|
|
if ($debugging) {
|
|
|
|
static::addStreamHandler($logger, $stream, $level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param string $channel The channel of the logger instance
|
|
|
|
* @param string $developerIp The IP of the developer who wants to use the logger
|
|
|
|
*
|
|
|
|
* @return LoggerInterface The PSR-3 compliant logger instance
|
|
|
|
*/
|
|
|
|
public static function createDev($channel, $developerIp)
|
|
|
|
{
|
|
|
|
$logger = new Monolog\Logger($channel);
|
|
|
|
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
|
|
|
|
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
|
2019-01-28 05:21:48 -05:00
|
|
|
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
|
2019-01-28 12:41:29 -05:00
|
|
|
$logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger']));
|
2018-12-30 15:42:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
$logger->pushHandler(new FriendicaDevelopHandler($developerIp));
|
|
|
|
|
|
|
|
return $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @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 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;
|
|
|
|
}
|
|
|
|
$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);
|
|
|
|
} else {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|