]> git.mxchange.org Git - friendica.git/blob - src/Factory/LoggerFactory.php
Config FollowUp
[friendica.git] / src / Factory / LoggerFactory.php
1 <?php
2
3 namespace Friendica\Factory;
4
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;
11 use Monolog;
12 use Psr\Log\LoggerInterface;
13 use Psr\Log\LogLevel;
14
15 /**
16  * A logger factory
17  *
18  * Currently only Monolog is supported
19  */
20 class LoggerFactory
21 {
22         /**
23          * Creates a new PSR-3 compliant logger instances
24          *
25          * @param string        $channel The channel of the logger instance
26          * @param Configuration $config  The config
27          *
28          * @return LoggerInterface The PSR-3 compliant logger instance
29          */
30         public static function create($channel, Configuration $config)
31         {
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]));
37
38                 if (isset($config)) {
39                         $debugging = $config->get('system', 'debugging');
40                         $stream = $config->get('system', 'logfile');
41                         $level = $config->get('system', 'loglevel');
42
43                         if ($debugging) {
44                                 static::addStreamHandler($logger, $stream, $level);
45                         }
46                 }
47
48                 return $logger;
49         }
50
51         /**
52          * Creates a new PSR-3 compliant develop logger
53          *
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.
56          *
57          * It should never get filled during normal usage of Friendica
58          *
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
61          *
62          * @return LoggerInterface The PSR-3 compliant logger instance
63          */
64         public static function createDev($channel, $developerIp)
65         {
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']));
71
72
73                 $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
74
75                 return $logger;
76         }
77
78         /**
79          * Adding a handler to a given logger instance
80          *
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
84          *
85          * @return void
86          *
87          * @throws InternalServerErrorException if the logger is incompatible to the logger factory
88          * @throws \Exception in case of general failures
89          */
90         public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
91         {
92                 if ($logger instanceof Monolog\Logger) {
93                         $loglevel = Monolog\Logger::toMonologLevel($level);
94
95                         // fallback to notice if an invalid loglevel is set
96                         if (!is_int($loglevel)) {
97                                 $loglevel = LogLevel::NOTICE;
98                         }
99                         $fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
100
101                         $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
102                         $fileHandler->setFormatter($formatter);
103
104                         $logger->pushHandler($fileHandler);
105                 } else {
106                         throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
107                 }
108         }
109
110         /**
111          * This method enables the test mode of a given logger
112          *
113          * @param LoggerInterface $logger The logger
114          *
115          * @return Monolog\Handler\TestHandler the Handling for tests
116          *
117          * @throws InternalServerErrorException if the logger is incompatible to the logger factory
118          */
119         public static function enableTest($logger)
120         {
121                 if ($logger instanceof Monolog\Logger) {
122                         // disable every handler so far
123                         $logger->pushHandler(new Monolog\Handler\NullHandler());
124
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);
129
130                         $logger->pushHandler($fileHandler);
131
132                         return $fileHandler;
133                 } else {
134                         throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
135                 }
136         }
137 }