]> git.mxchange.org Git - friendica.git/blob - src/Util/LoggerFactory.php
Merge pull request #6531 from Ixiter/develop-theme-frio
[friendica.git] / src / Util / LoggerFactory.php
1 <?php
2
3 namespace Friendica\Util;
4
5 use Friendica\Network\HTTPException\InternalServerErrorException;
6 use Friendica\Util\Logger\FriendicaDevelopHandler;
7 use Friendica\Util\Logger\FriendicaProcessor;
8 use Monolog;
9 use Psr\Log\LoggerInterface;
10 use Psr\Log\LogLevel;
11
12 /**
13  * A logger factory
14  *
15  * Currently only Monolog is supported
16  */
17 class LoggerFactory
18 {
19         /**
20          * Creates a new PSR-3 compliant logger instances
21          *
22          * @param string $channel The channel of the logger instance
23          *
24          * @return LoggerInterface The PSR-3 compliant logger instance
25          */
26         public static function create($channel)
27         {
28                 $logger = new Monolog\Logger($channel);
29                 $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
30                 $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
31                 $logger->pushProcessor(new FriendicaProcessor(LogLevel::DEBUG, 1));
32
33                 return $logger;
34         }
35
36         /**
37          * Creates a new PSR-3 compliant develop logger
38          *
39          * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
40          * you'll use this logger instance for the duration of your work.
41          *
42          * It should never get filled during normal usage of Friendica
43          *
44          * @param string $channel      The channel of the logger instance
45          * @param string $developerIp  The IP of the developer who wants to use the logger
46          *
47          * @return LoggerInterface The PSR-3 compliant logger instance
48          */
49         public static function createDev($channel, $developerIp)
50         {
51                 $logger = new Monolog\Logger($channel);
52                 $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
53                 $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
54                 $logger->pushProcessor(new FriendicaProcessor(LogLevel::DEBUG, 1));
55
56
57                 $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
58
59                 return $logger;
60         }
61
62         /**
63          * Adding a handler to a given logger instance
64          *
65          * @param LoggerInterface $logger  The logger instance
66          * @param mixed           $stream  The stream which handles the logger output
67          * @param string          $level   The level, for which this handler at least should handle logging
68          *
69          * @return void
70          *
71          * @throws InternalServerErrorException if the logger is incompatible to the logger factory
72          * @throws \Exception in case of general failures
73          */
74         public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
75         {
76                 if ($logger instanceof Monolog\Logger) {
77                         $loglevel = Monolog\Logger::toMonologLevel($level);
78
79                         // fallback to notice if an invalid loglevel is set
80                         if (!is_int($loglevel)) {
81                                 $loglevel = LogLevel::NOTICE;
82                         }
83                         $fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
84
85                         $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
86                         $fileHandler->setFormatter($formatter);
87
88                         $logger->pushHandler($fileHandler);
89                 } else {
90                         throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
91                 }
92         }
93
94         /**
95          * This method enables the test mode of a given logger
96          *
97          * @param LoggerInterface $logger The logger
98          *
99          * @return Monolog\Handler\TestHandler the Handling for tests
100          *
101          * @throws InternalServerErrorException if the logger is incompatible to the logger factory
102          */
103         public static function enableTest($logger)
104         {
105                 if ($logger instanceof Monolog\Logger) {
106                         // disable every handler so far
107                         $logger->pushHandler(new Monolog\Handler\NullHandler());
108
109                         // enable the test handler
110                         $fileHandler = new Monolog\Handler\TestHandler();
111                         $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
112                         $fileHandler->setFormatter($formatter);
113
114                         $logger->pushHandler($fileHandler);
115
116                         return $fileHandler;
117                 } else {
118                         throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
119                 }
120         }
121 }