]> git.mxchange.org Git - friendica.git/blob - src/Factory/LoggerFactory.php
updated the credits
[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                 $debugging = $config->get('system', 'debugging');
39                 $stream    = $config->get('system', 'logfile');
40                 $level     = $config->get('system', 'loglevel');
41
42                 if ($debugging) {
43                         $loglevel = self::mapLegacyConfigDebugLevel((string)$level);
44                         static::addStreamHandler($logger, $stream, $loglevel);
45                 } else {
46                         static::addVoidHandler($logger);
47                 }
48
49                 Logger::init($logger);
50
51                 return $logger;
52         }
53
54         /**
55          * Creates a new PSR-3 compliant develop logger
56          *
57          * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
58          * you'll use this logger instance for the duration of your work.
59          *
60          * It should never get filled during normal usage of Friendica
61          *
62          * @param string        $channel The channel of the logger instance
63          * @param Configuration $config  The config
64          *
65          * @return LoggerInterface The PSR-3 compliant logger instance
66          */
67         public static function createDev($channel, Configuration $config)
68         {
69                 $debugging   = $config->get('system', 'debugging');
70                 $stream      = $config->get('system', 'dlogfile');
71                 $developerIp = $config->get('system', 'dlogip');
72
73                 if (!isset($developerIp) || !$debugging) {
74                         return null;
75                 }
76
77                 $logger = new Monolog\Logger($channel);
78                 $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
79                 $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
80                 $logger->pushProcessor(new Monolog\Processor\UidProcessor());
81                 $logger->pushProcessor(new FriendicaIntrospectionProcessor(LogLevel::DEBUG, ['Friendica\\Core\\Logger']));
82
83                 $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
84
85                 static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
86
87                 Logger::setDevLogger($logger);
88
89                 return $logger;
90         }
91
92         /**
93          * Mapping a legacy level to the PSR-3 compliant levels
94          * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
95          *
96          * @param string $level the level to be mapped
97          *
98          * @return string the PSR-3 compliant level
99          */
100         private static function mapLegacyConfigDebugLevel($level)
101         {
102                 switch ($level) {
103                         // legacy WARNING
104                         case "0":
105                                 return LogLevel::ERROR;
106                         // legacy INFO
107                         case "1":
108                                 return LogLevel::WARNING;
109                         // legacy TRACE
110                         case "2":
111                                 return LogLevel::NOTICE;
112                         // legacy DEBUG
113                         case "3":
114                                 return LogLevel::INFO;
115                         // legacy DATA
116                         case "4":
117                                 return LogLevel::DEBUG;
118                         // legacy ALL
119                         case "5":
120                                 return LogLevel::DEBUG;
121                         // default if nothing set
122                         default:
123                                 return $level;
124                 }
125         }
126
127         /**
128          * Adding a handler to a given logger instance
129          *
130          * @param LoggerInterface $logger  The logger instance
131          * @param mixed           $stream  The stream which handles the logger output
132          * @param string          $level   The level, for which this handler at least should handle logging
133          *
134          * @return void
135          *
136          * @throws InternalServerErrorException if the logger is incompatible to the logger factory
137          * @throws \Exception in case of general failures
138          */
139         public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
140         {
141                 if ($logger instanceof Monolog\Logger) {
142                         $loglevel = Monolog\Logger::toMonologLevel($level);
143
144                         // fallback to notice if an invalid loglevel is set
145                         if (!is_int($loglevel)) {
146                                 $loglevel = LogLevel::NOTICE;
147                         }
148                         $fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
149
150                         $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
151                         $fileHandler->setFormatter($formatter);
152
153                         $logger->pushHandler($fileHandler);
154                 } else {
155                         throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
156                 }
157         }
158
159         public static function addVoidHandler($logger)
160         {
161                 if ($logger instanceof Monolog\Logger) {
162                         $logger->pushHandler(new Monolog\Handler\NullHandler());
163                 }
164         }
165 }