]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
Reset map function name
[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          *
144          * @see LoggerInterface::emergency()
145          *
146          * @param string $message
147          * @param array  $context
148          *
149          * @return void
150          * @throws \Exception
151          */
152         public static function emergency($message, $context = [])
153         {
154                 if (!isset(self::$logger)) {
155                         return;
156                 }
157
158                 $stamp1 = microtime(true);
159                 self::$logger->emergency($message, $context);
160                 self::getApp()->saveTimestamp($stamp1, 'file');
161         }
162
163         /**
164          * Action must be taken immediately.
165          * @see LoggerInterface::alert()
166          *
167          * Example: Entire website down, database unavailable, etc. This should
168          * trigger the SMS alerts and wake you up.
169          *
170          * @param string $message
171          * @param array  $context
172          *
173          * @return void
174          * @throws \Exception
175          */
176         public static function alert($message, $context = [])
177         {
178                 if (!isset(self::$logger)) {
179                         return;
180                 }
181
182                 $stamp1 = microtime(true);
183                 self::$logger->alert($message, $context);
184                 self::getApp()->saveTimestamp($stamp1, 'file');
185         }
186
187         /**
188          * Critical conditions.
189          * @see LoggerInterface::critical()
190          *
191          * Example: Application component unavailable, unexpected exception.
192          *
193          * @param string $message
194          * @param array  $context
195          *
196          * @return void
197          * @throws \Exception
198          */
199         public static function critical($message, $context = [])
200         {
201                 if (!isset(self::$logger)) {
202                         return;
203                 }
204
205                 $stamp1 = microtime(true);
206                 self::$logger->critical($message, $context);
207                 self::getApp()->saveTimestamp($stamp1, 'file');
208         }
209
210         /**
211          * Runtime errors that do not require immediate action but should typically
212          * be logged and monitored.
213          * @see LoggerInterface::error()
214          *
215          * @param string $message
216          * @param array  $context
217          *
218          * @return void
219          * @throws \Exception
220          */
221         public static function error($message, $context = [])
222         {
223                 if (!isset(self::$logger)) {
224                         echo "not set!?\n";
225                         return;
226                 }
227
228                 $stamp1 = microtime(true);
229                 self::$logger->error($message, $context);
230                 self::getApp()->saveTimestamp($stamp1, 'file');
231         }
232
233         /**
234          * Exceptional occurrences that are not errors.
235          * @see LoggerInterface::warning()
236          *
237          * Example: Use of deprecated APIs, poor use of an API, undesirable things
238          * that are not necessarily wrong.
239          *
240          * @param string $message
241          * @param array  $context
242          *
243          * @return void
244          * @throws \Exception
245          */
246         public static function warning($message, $context = [])
247         {
248                 if (!isset(self::$logger)) {
249                         return;
250                 }
251
252                 $stamp1 = microtime(true);
253                 self::$logger->warning($message, $context);
254                 self::getApp()->saveTimestamp($stamp1, 'file');
255         }
256
257         /**
258          * Normal but significant events.
259          * @see LoggerInterface::notice()
260          *
261          * @param string $message
262          * @param array  $context
263          *
264          * @return void
265          * @throws \Exception
266          */
267         public static function notice($message, $context = [])
268         {
269                 if (!isset(self::$logger)) {
270                         return;
271                 }
272
273                 $stamp1 = microtime(true);
274                 self::$logger->notice($message, $context);
275                 self::getApp()->saveTimestamp($stamp1, 'file');
276         }
277
278         /**
279          * Interesting events.
280          * @see LoggerInterface::info()
281          *
282          * Example: User logs in, SQL logs.
283          *
284          * @param string $message
285          * @param array  $context
286          *
287          * @return void
288          * @throws \Exception
289          */
290         public static function info($message, $context = [])
291         {
292                 if (!isset(self::$logger)) {
293                         return;
294                 }
295
296                 $stamp1 = microtime(true);
297                 self::$logger->info($message, $context);
298                 self::getApp()->saveTimestamp($stamp1, 'file');
299         }
300
301         /**
302          * Detailed debug information.
303          * @see LoggerInterface::debug()
304          *
305          * @param string $message
306          * @param array  $context
307          *
308          * @return void
309          * @throws \Exception
310          */
311         public static function debug($message, $context = [])
312         {
313                 if (!isset(self::$logger)) {
314                         return;
315                 }
316
317                 $stamp1 = microtime(true);
318                 self::$logger->debug($message, $context);
319                 self::getApp()->saveTimestamp($stamp1, 'file');
320         }
321
322     /**
323      * @brief Logs the given message at the given log level
324      *
325      * @param string $msg
326      * @param string $level
327          *
328          * @throws \Exception
329          * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
330      */
331     public static function log($msg, $level = LogLevel::INFO)
332     {
333                 if (!isset(self::$logger)) {
334                         return;
335                 }
336
337         $stamp1 = microtime(true);
338                 self::$logger->log($level, $msg);
339         self::getApp()->saveTimestamp($stamp1, "file");
340     }
341
342         /**
343          * @brief An alternative logger for development.
344          * Works largely as log() but allows developers
345          * to isolate particular elements they are targetting
346          * personally without background noise
347          *
348          * @param string $msg
349          * @param string $level
350          * @throws \Exception
351          */
352     public static function devLog($msg, $level = LogLevel::DEBUG)
353     {
354                 if (!isset(self::$logger)) {
355                         return;
356                 }
357
358         $stamp1 = microtime(true);
359         self::$devLogger->log($level, $msg);
360         self::getApp()->saveTimestamp($stamp1, "file");
361     }
362 }