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\Introspection;
9 use Friendica\Util\Logger\Monolog\FriendicaDevelopHandler;
10 use Friendica\Util\Logger\Monolog\FriendicaIntrospectionProcessor;
11 use Friendica\Util\Logger\SyslogLogger;
12 use Friendica\Util\Logger\VoidLogger;
13 use Friendica\Util\Profiler;
15 use Psr\Log\LoggerInterface;
21 * Currently only Monolog is supported
26 * Creates a new PSR-3 compliant logger instances
28 * @param string $channel The channel of the logger instance
29 * @param Configuration $config The config
30 * @param Profiler $profiler The profiler of the app
32 * @return LoggerInterface The PSR-3 compliant logger instance
36 public static function create($channel, Configuration $config, Profiler $profiler)
38 if(empty($config->get('system', 'debugging', false))) {
39 $logger = new VoidLogger();
40 Logger::init($logger);
44 $introspection = new Introspection([Logger::class, Profiler::class]);
46 switch ($config->get('system', 'logger_adapter', 'monolog')) {
48 $level = $config->get('system', 'loglevel');
50 $logger = new SyslogLogger($channel, $introspection, $profiler, $level);
54 $logger = new Monolog\Logger($channel);
55 $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
56 $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
57 $logger->pushProcessor(new Monolog\Processor\UidProcessor());
58 $logger->pushProcessor(new FriendicaIntrospectionProcessor($introspection, LogLevel::DEBUG));
60 $stream = $config->get('system', 'logfile');
61 $level = $config->get('system', 'loglevel');
63 $loglevel = self::mapLegacyConfigDebugLevel((string)$level);
64 static::addStreamHandler($logger, $stream, $loglevel);
68 Logger::init($logger);
74 * Creates a new PSR-3 compliant develop logger
76 * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
77 * you'll use this logger instance for the duration of your work.
79 * It should never get filled during normal usage of Friendica
81 * @param string $channel The channel of the logger instance
82 * @param Configuration $config The config
84 * @return LoggerInterface The PSR-3 compliant logger instance
86 public static function createDev($channel, Configuration $config)
88 $debugging = $config->get('system', 'debugging');
89 $stream = $config->get('system', 'dlogfile');
90 $developerIp = $config->get('system', 'dlogip');
92 if (!isset($developerIp) || !$debugging) {
96 $introspection = new Introspection([Logger::class, SyslogLogger::class, Profiler::class]);
98 $logger = new Monolog\Logger($channel);
99 $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
100 $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
101 $logger->pushProcessor(new Monolog\Processor\UidProcessor());
102 $logger->pushProcessor(new FriendicaIntrospectionProcessor($introspection, LogLevel::DEBUG));
104 $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
106 static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
108 Logger::setDevLogger($logger);
114 * Mapping a legacy level to the PSR-3 compliant levels
115 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
117 * @param string $level the level to be mapped
119 * @return string the PSR-3 compliant level
121 private static function mapLegacyConfigDebugLevel($level)
126 return LogLevel::ERROR;
129 return LogLevel::WARNING;
132 return LogLevel::NOTICE;
135 return LogLevel::INFO;
138 return LogLevel::DEBUG;
141 return LogLevel::DEBUG;
142 // default if nothing set
149 * Adding a handler to a given logger instance
151 * @param LoggerInterface $logger The logger instance
152 * @param mixed $stream The stream which handles the logger output
153 * @param string $level The level, for which this handler at least should handle logging
157 * @throws InternalServerErrorException if the logger is incompatible to the logger factory
158 * @throws \Exception in case of general failures
160 public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
162 if ($logger instanceof Monolog\Logger) {
163 $loglevel = Monolog\Logger::toMonologLevel($level);
165 // fallback to notice if an invalid loglevel is set
166 if (!is_int($loglevel)) {
167 $loglevel = LogLevel::NOTICE;
169 $fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
171 $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
172 $fileHandler->setFormatter($formatter);
174 $logger->pushHandler($fileHandler);
176 throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
180 public static function addVoidHandler($logger)
182 if ($logger instanceof Monolog\Logger) {
183 $logger->pushHandler(new Monolog\Handler\NullHandler());