]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
Add Monolog
[friendica.git] / src / Core / Logger.php
1 <?php
2 /**
3  * @file src/Core/Logger.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\BaseObject;
8 use Friendica\Network\HTTPException\InternalServerErrorException;
9 use Friendica\Util\LoggerFactory;
10 use Psr\Log\LoggerInterface;
11 use Psr\Log\LogLevel;
12
13 /**
14  * @brief Logger functions
15  */
16 class Logger extends BaseObject
17 {
18         /**
19          * @see Logger::error()
20          */
21         const WARNING = LogLevel::ERROR;
22         /**
23          * @see Logger::warning()
24          */
25         const INFO = LogLevel::WARNING;
26         /**
27          * @see Logger::notice()
28          */
29         const TRACE = LogLevel::NOTICE;
30         /**
31          * @see Logger::info()
32          */
33         const DEBUG = LogLevel::INFO;
34         /**
35          * @see Logger::debug()
36          */
37         const DATA = LogLevel::DEBUG;
38         /**
39          * @see Logger::debug()
40          */
41         const ALL = LogLevel::DEBUG;
42
43         /**
44          * @var array the legacy loglevels
45          * @deprecated 2019.03 use PSR-3 loglevels
46          * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
47          *
48          */
49         public static $levels = [
50                 self::WARNING => 'Warning',
51                 self::INFO => 'Info',
52                 self::TRACE => 'Trace',
53                 self::DEBUG => 'Debug',
54                 self::DATA => 'Data',
55                 self::ALL => 'All',
56         ];
57
58         /**
59          * @var LoggerInterface A PSR-3 compliant logger instance
60          */
61         private static $logger;
62
63         /**
64          * @var LoggerInterface A PSR-3 compliant logger instance for developing only
65          */
66         private static $devLogger;
67
68         /**
69          * Sets the default logging handler for Friendica.
70          * @todo Can be combined with other handlers too if necessary, could be configurable.
71          *
72          * @param LoggerInterface $logger The Logger instance of this Application
73          *
74          * @throws InternalServerErrorException if the logger factory is incompatible to this logger
75          */
76         public static function setLogger($logger)
77         {
78                 $debugging = Config::get('system', 'debugging');
79                 $logfile = Config::get('system', 'logfile');
80                 $loglevel = Config::get('system', 'loglevel');
81
82                 if (!$debugging || !$logfile) {
83                         return;
84                 }
85
86                 if (is_int($loglevel)) {
87                         $loglevel = self::mapLegacyConfigDebugLevel($loglevel);
88                 }
89
90                 LoggerFactory::addStreamHandler($logger, $logfile, $loglevel);
91
92                 self::$logger = $logger;
93
94                 $logfile = Config::get('system', 'dlogfile');
95
96                 if (!$logfile) {
97                         return;
98                 }
99
100                 $developIp = Config::get('system', 'dlogip');
101
102                 self::$devLogger = LoggerFactory::createDev('develop', $developIp);
103                 LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
104         }
105
106         /**
107          * Mapping a legacy level to the PSR-3 compliant levels
108          * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
109          *
110          * @param int $level the level to be mapped
111          *
112          * @return string the PSR-3 compliant level
113          */
114         private static function mapLegacyConfigDebugLevel($level)
115         {
116                 switch ($level) {
117                         // legacy WARNING
118                         case 0:
119                                 return LogLevel::ERROR;
120                         // legacy INFO
121                         case 1:
122                                 return LogLevel::WARNING;
123                         // legacy TRACE
124                         case 2:
125                                 return LogLevel::NOTICE;
126                         // legacy DEBUG
127                         case 3:
128                                 return LogLevel::INFO;
129                         // legacy DATA
130                         case 4:
131                                 return LogLevel::DEBUG;
132                         // legacy ALL
133                         case 5:
134                                 return LogLevel::DEBUG;
135                         // default if nothing set
136                         default:
137                                 return LogLevel::NOTICE;
138                 }
139         }
140
141         /**
142          * System is unusable.
143          * @see LoggerInterface::emergency()
144          *
145          * @param string $message
146          * @param array  $context
147          *
148          * @return void
149          *
150          */
151         public static function emergency($message, $context = [])
152         {
153                 if (!isset(self::$logger)) {
154                         return;
155                 }
156
157                 $stamp1 = microtime(true);
158                 self::$logger->emergency($message, $context);
159                 self::getApp()->saveTimestamp($stamp1, 'file');
160         }
161
162         /**
163          * Action must be taken immediately.
164          * @see LoggerInterface::alert()
165          *
166          * Example: Entire website down, database unavailable, etc. This should
167          * trigger the SMS alerts and wake you up.
168          *
169          * @param string $message
170          * @param array  $context
171          *
172          * @return void
173          *
174          */
175         public static function alert($message, $context = [])
176         {
177                 if (!isset(self::$logger)) {
178                         return;
179                 }
180
181                 $stamp1 = microtime(true);
182                 self::$logger->alert($message, $context);
183                 self::getApp()->saveTimestamp($stamp1, 'file');
184         }
185
186         /**
187          * Critical conditions.
188          * @see LoggerInterface::critical()
189          *
190          * Example: Application component unavailable, unexpected exception.
191          *
192          * @param string $message
193          * @param array  $context
194          *
195          * @return void
196          *
197          */
198         public static function critical($message, $context = [])
199         {
200                 if (!isset(self::$logger)) {
201                         return;
202                 }
203
204                 $stamp1 = microtime(true);
205                 self::$logger->critical($message, $context);
206                 self::getApp()->saveTimestamp($stamp1, 'file');
207         }
208
209         /**
210          * Runtime errors that do not require immediate action but should typically
211          * be logged and monitored.
212          * @see LoggerInterface::error()
213          *
214          * @param string $message
215          * @param array  $context
216          *
217          * @return void
218          *
219          */
220         public static function error($message, $context = [])
221         {
222                 if (!isset(self::$logger)) {
223                         echo "not set!?\n";
224                         return;
225                 }
226
227                 $stamp1 = microtime(true);
228                 self::$logger->error($message, $context);
229                 self::getApp()->saveTimestamp($stamp1, 'file');
230         }
231
232         /**
233          * Exceptional occurrences that are not errors.
234          * @see LoggerInterface::warning()
235          *
236          * Example: Use of deprecated APIs, poor use of an API, undesirable things
237          * that are not necessarily wrong.
238          *
239          * @param string $message
240          * @param array  $context
241          *
242          * @return void
243          *
244          */
245         public static function warning($message, $context = [])
246         {
247                 if (!isset(self::$logger)) {
248                         return;
249                 }
250
251                 $stamp1 = microtime(true);
252                 self::$logger->warning($message, $context);
253                 self::getApp()->saveTimestamp($stamp1, 'file');
254         }
255
256         /**
257          * Normal but significant events.
258          * @see LoggerInterface::notice()
259          *
260          * @param string $message
261          * @param array  $context
262          *
263          * @return void
264          *
265          */
266         public static function notice($message, $context = [])
267         {
268                 if (!isset(self::$logger)) {
269                         return;
270                 }
271
272                 $stamp1 = microtime(true);
273                 self::$logger->notice($message, $context);
274                 self::getApp()->saveTimestamp($stamp1, 'file');
275         }
276
277         /**
278          * Interesting events.
279          * @see LoggerInterface::info()
280          *
281          * Example: User logs in, SQL logs.
282          *
283          * @param string $message
284          * @param array  $context
285          *
286          * @return void
287          *
288          */
289         public static function info($message, $context = [])
290         {
291                 if (!isset(self::$logger)) {
292                         return;
293                 }
294
295                 $stamp1 = microtime(true);
296                 self::$logger->info($message, $context);
297                 self::getApp()->saveTimestamp($stamp1, 'file');
298         }
299
300         /**
301          * Detailed debug information.
302          * @see LoggerInterface::debug()
303          *
304          * @param string $message
305          * @param array  $context
306          *
307          * @return void
308          */
309         public static function debug($message, $context = [])
310         {
311                 if (!isset(self::$logger)) {
312                         return;
313                 }
314
315                 $stamp1 = microtime(true);
316                 self::$logger->debug($message, $context);
317                 self::getApp()->saveTimestamp($stamp1, 'file');
318         }
319
320     /**
321      * @brief Logs the given message at the given log level
322      *
323      * @param string $msg
324      * @param int $level
325          *
326          * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
327      */
328     public static function log($msg, $level = LogLevel::NOTICE)
329     {
330                 if (!isset(self::$logger)) {
331                         return;
332                 }
333
334         $stamp1 = microtime(true);
335                 self::$logger->log($level, $msg);
336         self::getApp()->saveTimestamp($stamp1, "file");
337     }
338
339     /**
340      * @brief An alternative logger for development.
341      * Works largely as log() but allows developers
342      * to isolate particular elements they are targetting
343      * personally without background noise
344      *
345      * @param string $msg
346          * @param string $level
347      */
348     public static function devLog($msg, $level = LogLevel::DEBUG)
349     {
350                 if (!isset(self::$logger)) {
351                         return;
352                 }
353
354         $stamp1 = microtime(true);
355         self::$devLogger->log($level, $msg);
356         self::getApp()->saveTimestamp($stamp1, "file");
357     }
358 }