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);
47 Logger::init($logger);
53 * Creates a new PSR-3 compliant develop logger
55 * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
56 * you'll use this logger instance for the duration of your work.
58 * It should never get filled during normal usage of Friendica
60 * @param string $channel The channel of the logger instance
61 * @param Configuration $config The config
63 * @return LoggerInterface The PSR-3 compliant logger instance
65 public static function createDev($channel, Configuration $config)
67 $debugging = $config->get('system', 'debugging');
68 $stream = $config->get('system', 'dlogfile');
69 $developerIp = $config->get('system', 'dlogip');
71 if (!isset($developerIp) || !$debugging) {
75 $logger = new Monolog\Logger($channel);
76 $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
77 $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
78 $logger->pushProcessor(new Monolog\Processor\UidProcessor());
79 $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger']));
81 $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
83 static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
85 Logger::setDevLogger($logger);
91 * Mapping a legacy level to the PSR-3 compliant levels
92 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
94 * @param string $level the level to be mapped
96 * @return string the PSR-3 compliant level
98 private static function mapLegacyConfigDebugLevel($level)
103 return LogLevel::ERROR;
106 return LogLevel::WARNING;
109 return LogLevel::NOTICE;
112 return LogLevel::INFO;
115 return LogLevel::DEBUG;
118 return LogLevel::DEBUG;
119 // default if nothing set
126 * Adding a handler to a given logger instance
128 * @param LoggerInterface $logger The logger instance
129 * @param mixed $stream The stream which handles the logger output
130 * @param string $level The level, for which this handler at least should handle logging
134 * @throws InternalServerErrorException if the logger is incompatible to the logger factory
135 * @throws \Exception in case of general failures
137 public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
139 if ($logger instanceof Monolog\Logger) {
140 $loglevel = Monolog\Logger::toMonologLevel($level);
142 // fallback to notice if an invalid loglevel is set
143 if (!is_int($loglevel)) {
144 $loglevel = LogLevel::NOTICE;
146 $fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
148 $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
149 $fileHandler->setFormatter($formatter);
151 $logger->pushHandler($fileHandler);
153 throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');