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