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]));
39 $debugging = $config->get('system', 'debugging');
40 $stream = $config->get('system', 'logfile');
41 $level = $config->get('system', 'loglevel');
44 static::addStreamHandler($logger, $stream, $level);
52 * Creates a new PSR-3 compliant develop logger
54 * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
55 * you'll use this logger instance for the duration of your work.
57 * It should never get filled during normal usage of Friendica
59 * @param string $channel The channel of the logger instance
60 * @param string $developerIp The IP of the developer who wants to use the logger
62 * @return LoggerInterface The PSR-3 compliant logger instance
64 public static function createDev($channel, $developerIp)
66 $logger = new Monolog\Logger($channel);
67 $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
68 $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
69 $logger->pushProcessor(new Monolog\Processor\UidProcessor());
70 $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger']));
73 $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
79 * Adding a handler to a given logger instance
81 * @param LoggerInterface $logger The logger instance
82 * @param mixed $stream The stream which handles the logger output
83 * @param string $level The level, for which this handler at least should handle logging
87 * @throws InternalServerErrorException if the logger is incompatible to the logger factory
88 * @throws \Exception in case of general failures
90 public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
92 if ($logger instanceof Monolog\Logger) {
93 $loglevel = Monolog\Logger::toMonologLevel($level);
95 // fallback to notice if an invalid loglevel is set
96 if (!is_int($loglevel)) {
97 $loglevel = LogLevel::NOTICE;
99 $fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
101 $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
102 $fileHandler->setFormatter($formatter);
104 $logger->pushHandler($fileHandler);
106 throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
111 * This method enables the test mode of a given logger
113 * @param LoggerInterface $logger The logger
115 * @return Monolog\Handler\TestHandler the Handling for tests
117 * @throws InternalServerErrorException if the logger is incompatible to the logger factory
119 public static function enableTest($logger)
121 if ($logger instanceof Monolog\Logger) {
122 // disable every handler so far
123 $logger->pushHandler(new Monolog\Handler\NullHandler());
125 // enable the test handler
126 $fileHandler = new Monolog\Handler\TestHandler();
127 $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
128 $fileHandler->setFormatter($formatter);
130 $logger->pushHandler($fileHandler);
134 throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');