X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FFactory%2FLoggerFactory.php;h=67829546e91f1a9a1f5b4bf86f99c9ad602a203f;hb=486f1393429afc17ba57f31690fa2388f8925f82;hp=751a6357da84d4d3c00358196846979e949bfafb;hpb=741701d26f3f31613d76600764ce7de7a4057d46;p=friendica.git diff --git a/src/Factory/LoggerFactory.php b/src/Factory/LoggerFactory.php index 751a6357da..67829546e9 100644 --- a/src/Factory/LoggerFactory.php +++ b/src/Factory/LoggerFactory.php @@ -2,10 +2,18 @@ namespace Friendica\Factory; -use Friendica\Core\Config\ConfigCache; +use Friendica\Core\Config\Configuration; +use Friendica\Core\Logger; +use Friendica\Database\Database; use Friendica\Network\HTTPException\InternalServerErrorException; -use Friendica\Util\Logger\FriendicaDevelopHandler; -use Friendica\Util\Logger\FriendicaIntrospectionProcessor; +use Friendica\Util\Introspection; +use Friendica\Util\Logger\Monolog\DevelopHandler; +use Friendica\Util\Logger\Monolog\IntrospectionProcessor; +use Friendica\Util\Logger\ProfilerLogger; +use Friendica\Util\Logger\StreamLogger; +use Friendica\Util\Logger\SyslogLogger; +use Friendica\Util\Logger\VoidLogger; +use Friendica\Util\Profiler; use Monolog; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; @@ -17,32 +25,87 @@ use Psr\Log\LogLevel; */ class LoggerFactory { + /** + * A list of classes, which shouldn't get logged + * + * @var array + */ + private static $ignoreClassList = [ + Logger::class, + Profiler::class, + 'Friendica\\Util\\Logger', + ]; + /** * Creates a new PSR-3 compliant logger instances * - * @param string $channel The channel of the logger instance - * @param ConfigCache $config The config + * @param string $channel The channel of the logger instance + * @param Configuration $config The config + * @param Profiler $profiler The profiler of the app * * @return LoggerInterface The PSR-3 compliant logger instance + * + * @throws \Exception + * @throws InternalServerErrorException */ - public static function create($channel, ConfigCache $config = null) + public static function create($channel, Database $database, Configuration $config, Profiler $profiler) { - $logger = new Monolog\Logger($channel); - $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor()); - $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor()); - $logger->pushProcessor(new Monolog\Processor\UidProcessor()); - $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger'])); - - 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); - } + if (empty($config->get('system', 'debugging', false))) { + $logger = new VoidLogger(); + $database->setLogger($logger); + Logger::init($logger); + return $logger; } + $introspection = new Introspection(self::$ignoreClassList); + $level = $config->get('system', 'loglevel'); + $loglevel = self::mapLegacyConfigDebugLevel((string)$level); + + switch ($config->get('system', 'logger_config', 'stream')) { + 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()); + $logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG)); + + $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)) { + static::addStreamHandler($logger, $stream, $loglevel); + } + break; + + case 'syslog': + $logger = new SyslogLogger($channel, $introspection, $loglevel); + 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(); + } + 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); + } + + $database->setLogger($logger); + Logger::init($logger); + return $logger; } @@ -54,31 +117,114 @@ class LoggerFactory * * 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 + * @param string $channel The channel of the logger instance + * @param Configuration $config The config + * @param Profiler $profiler The profiler of the app * * @return LoggerInterface The PSR-3 compliant logger instance + * + * @throws InternalServerErrorException + * @throws \Exception */ - public static function createDev($channel, $developerIp) + public static function createDev($channel, Configuration $config, Profiler $profiler) { - $logger = new Monolog\Logger($channel); - $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor()); - $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor()); - $logger->pushProcessor(new Monolog\Processor\UidProcessor()); - $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger'])); + $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))) { + $logger = new VoidLogger(); + Logger::setDevLogger($logger); + return $logger; + } + + $loggerTimeZone = new \DateTimeZone('UTC'); + Monolog\Logger::setTimezone($loggerTimeZone); + $introspection = new Introspection(self::$ignoreClassList); - $logger->pushHandler(new FriendicaDevelopHandler($developerIp)); + switch ($config->get('system', 'logger_config', 'stream')) { + + 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()); + $logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG)); + + $logger->pushHandler(new DevelopHandler($developerIp)); + + static::addStreamHandler($logger, $stream, LogLevel::DEBUG); + break; + + case 'syslog': + $logger = new SyslogLogger($channel, $introspection, LogLevel::DEBUG); + 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); 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 * - * @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 + * @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 * @@ -94,6 +240,7 @@ class LoggerFactory if (!is_int($loglevel)) { $loglevel = LogLevel::NOTICE; } + $fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel); $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n"); @@ -105,31 +252,10 @@ class LoggerFactory } } - /** - * 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) + public static function addVoidHandler($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'); } } }