3 namespace Friendica\Factory;
5 use Friendica\Core\Config\Configuration;
6 use Friendica\Core\Logger;
7 use Friendica\Network\HTTPException\InternalServerErrorException;
8 use Friendica\Util\Logger\FriendicaDevelopHandler;
9 use Friendica\Util\Logger\FriendicaIntrospectionProcessor;
10 use Friendica\Util\Profiler;
12 use Psr\Log\LoggerInterface;
18 * Currently only Monolog is supported
23 * Creates a new PSR-3 compliant logger instances
25 * @param string $channel The channel of the logger instance
26 * @param Configuration $config The config
28 * @return LoggerInterface The PSR-3 compliant logger instance
30 public static function create($channel, Configuration $config)
32 $logger = new Monolog\Logger($channel);
33 $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
34 $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
35 $logger->pushProcessor(new Monolog\Processor\UidProcessor());
36 $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, [Logger::class, Profiler::class]));
38 $debugging = $config->get('system', 'debugging');
39 $stream = $config->get('system', 'logfile');
40 $level = $config->get('system', 'loglevel');
43 $loglevel = self::mapLegacyConfigDebugLevel((string)$level);
44 static::addStreamHandler($logger, $stream, $loglevel);
46 static::addVoidHandler($logger);
49 Logger::init($logger);
55 * Creates a new PSR-3 compliant develop logger
57 * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
58 * you'll use this logger instance for the duration of your work.
60 * It should never get filled during normal usage of Friendica
62 * @param string $channel The channel of the logger instance
63 * @param Configuration $config The config
65 * @return LoggerInterface The PSR-3 compliant logger instance
67 public static function createDev($channel, Configuration $config)
69 $debugging = $config->get('system', 'debugging');
70 $stream = $config->get('system', 'dlogfile');
71 $developerIp = $config->get('system', 'dlogip');
73 if (!isset($developerIp) || !$debugging) {
77 $logger = new Monolog\Logger($channel);
78 $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
79 $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
80 $logger->pushProcessor(new Monolog\Processor\UidProcessor());
81 $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger']));
83 $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
85 static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
87 Logger::setDevLogger($logger);
93 * Mapping a legacy level to the PSR-3 compliant levels
94 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
96 * @param string $level the level to be mapped
98 * @return string the PSR-3 compliant level
100 private static function mapLegacyConfigDebugLevel($level)
105 return LogLevel::ERROR;
108 return LogLevel::WARNING;
111 return LogLevel::NOTICE;
114 return LogLevel::INFO;
117 return LogLevel::DEBUG;
120 return LogLevel::DEBUG;
121 // default if nothing set
128 * Adding a handler to a given logger instance
130 * @param LoggerInterface $logger The logger instance
131 * @param mixed $stream The stream which handles the logger output
132 * @param string $level The level, for which this handler at least should handle logging
136 * @throws InternalServerErrorException if the logger is incompatible to the logger factory
137 * @throws \Exception in case of general failures
139 public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
141 if ($logger instanceof Monolog\Logger) {
142 $loglevel = Monolog\Logger::toMonologLevel($level);
144 // fallback to notice if an invalid loglevel is set
145 if (!is_int($loglevel)) {
146 $loglevel = LogLevel::NOTICE;
148 $fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
150 $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
151 $fileHandler->setFormatter($formatter);
153 $logger->pushHandler($fileHandler);
155 throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
159 public static function addVoidHandler($logger)
161 if ($logger instanceof Monolog\Logger) {
162 $logger->pushHandler(new Monolog\Handler\NullHandler());