]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
Merge pull request #6577 from rabuzarus/20190129_-_jot_atachment_preview
[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\Factory\LoggerFactory;
9 use Friendica\Network\HTTPException\InternalServerErrorException;
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                 $loglevel = self::mapLegacyConfigDebugLevel((string)$loglevel);
87
88                 LoggerFactory::addStreamHandler($logger, $logfile, $loglevel);
89
90                 self::$logger = $logger;
91
92                 $logfile = Config::get('system', 'dlogfile');
93
94                 if (!$logfile) {
95                         return;
96                 }
97
98                 $developIp = Config::get('system', 'dlogip');
99
100                 self::$devLogger = LoggerFactory::createDev('develop', $developIp);
101                 LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
102         }
103
104         /**
105          * Mapping a legacy level to the PSR-3 compliant levels
106          * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
107          *
108          * @param string $level the level to be mapped
109          *
110          * @return string the PSR-3 compliant level
111          */
112         private static function mapLegacyConfigDebugLevel($level)
113         {
114                 switch ($level) {
115                         // legacy WARNING
116                         case "0":
117                                 return LogLevel::ERROR;
118                         // legacy INFO
119                         case "1":
120                                 return LogLevel::WARNING;
121                         // legacy TRACE
122                         case "2":
123                                 return LogLevel::NOTICE;
124                         // legacy DEBUG
125                         case "3":
126                                 return LogLevel::INFO;
127                         // legacy DATA
128                         case "4":
129                                 return LogLevel::DEBUG;
130                         // legacy ALL
131                         case "5":
132                                 return LogLevel::DEBUG;
133                         // default if nothing set
134                         default:
135                                 return $level;
136                 }
137         }
138
139         /**
140          * System is unusable.
141          *
142          * @see LoggerInterface::emergency()
143          *
144          * @param string $message
145          * @param array  $context
146          *
147          * @return void
148          * @throws \Exception
149          */
150         public static function emergency($message, $context = [])
151         {
152                 if (!isset(self::$logger)) {
153                         return;
154                 }
155
156                 $stamp1 = microtime(true);
157                 self::$logger->emergency($message, $context);
158                 self::getApp()->saveTimestamp($stamp1, 'file');
159         }
160
161         /**
162          * Action must be taken immediately.
163          * @see LoggerInterface::alert()
164          *
165          * Example: Entire website down, database unavailable, etc. This should
166          * trigger the SMS alerts and wake you up.
167          *
168          * @param string $message
169          * @param array  $context
170          *
171          * @return void
172          * @throws \Exception
173          */
174         public static function alert($message, $context = [])
175         {
176                 if (!isset(self::$logger)) {
177                         return;
178                 }
179
180                 $stamp1 = microtime(true);
181                 self::$logger->alert($message, $context);
182                 self::getApp()->saveTimestamp($stamp1, 'file');
183         }
184
185         /**
186          * Critical conditions.
187          * @see LoggerInterface::critical()
188          *
189          * Example: Application component unavailable, unexpected exception.
190          *
191          * @param string $message
192          * @param array  $context
193          *
194          * @return void
195          * @throws \Exception
196          */
197         public static function critical($message, $context = [])
198         {
199                 if (!isset(self::$logger)) {
200                         return;
201                 }
202
203                 $stamp1 = microtime(true);
204                 self::$logger->critical($message, $context);
205                 self::getApp()->saveTimestamp($stamp1, 'file');
206         }
207
208         /**
209          * Runtime errors that do not require immediate action but should typically
210          * be logged and monitored.
211          * @see LoggerInterface::error()
212          *
213          * @param string $message
214          * @param array  $context
215          *
216          * @return void
217          * @throws \Exception
218          */
219         public static function error($message, $context = [])
220         {
221                 if (!isset(self::$logger)) {
222                         echo "not set!?\n";
223                         return;
224                 }
225
226                 $stamp1 = microtime(true);
227                 self::$logger->error($message, $context);
228                 self::getApp()->saveTimestamp($stamp1, 'file');
229         }
230
231         /**
232          * Exceptional occurrences that are not errors.
233          * @see LoggerInterface::warning()
234          *
235          * Example: Use of deprecated APIs, poor use of an API, undesirable things
236          * that are not necessarily wrong.
237          *
238          * @param string $message
239          * @param array  $context
240          *
241          * @return void
242          * @throws \Exception
243          */
244         public static function warning($message, $context = [])
245         {
246                 if (!isset(self::$logger)) {
247                         return;
248                 }
249
250                 $stamp1 = microtime(true);
251                 self::$logger->warning($message, $context);
252                 self::getApp()->saveTimestamp($stamp1, 'file');
253         }
254
255         /**
256          * Normal but significant events.
257          * @see LoggerInterface::notice()
258          *
259          * @param string $message
260          * @param array  $context
261          *
262          * @return void
263          * @throws \Exception
264          */
265         public static function notice($message, $context = [])
266         {
267                 if (!isset(self::$logger)) {
268                         return;
269                 }
270
271                 $stamp1 = microtime(true);
272                 self::$logger->notice($message, $context);
273                 self::getApp()->saveTimestamp($stamp1, 'file');
274         }
275
276         /**
277          * Interesting events.
278          * @see LoggerInterface::info()
279          *
280          * Example: User logs in, SQL logs.
281          *
282          * @param string $message
283          * @param array  $context
284          *
285          * @return void
286          * @throws \Exception
287          */
288         public static function info($message, $context = [])
289         {
290                 if (!isset(self::$logger)) {
291                         return;
292                 }
293
294                 $stamp1 = microtime(true);
295                 self::$logger->info($message, $context);
296                 self::getApp()->saveTimestamp($stamp1, 'file');
297         }
298
299         /**
300          * Detailed debug information.
301          * @see LoggerInterface::debug()
302          *
303          * @param string $message
304          * @param array  $context
305          *
306          * @return void
307          * @throws \Exception
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 string $level
325          *
326          * @throws \Exception
327          * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
328      */
329     public static function log($msg, $level = LogLevel::INFO)
330     {
331                 if (!isset(self::$logger)) {
332                         return;
333                 }
334
335         $stamp1 = microtime(true);
336                 self::$logger->log($level, $msg);
337         self::getApp()->saveTimestamp($stamp1, "file");
338     }
339
340         /**
341          * @brief An alternative logger for development.
342          * Works largely as log() but allows developers
343          * to isolate particular elements they are targetting
344          * personally without background noise
345          *
346          * @param string $msg
347          * @param string $level
348          * @throws \Exception
349          */
350     public static function devLog($msg, $level = LogLevel::DEBUG)
351     {
352                 if (!isset(self::$logger)) {
353                         return;
354                 }
355
356         $stamp1 = microtime(true);
357         self::$devLogger->log($level, $msg);
358         self::getApp()->saveTimestamp($stamp1, "file");
359     }
360 }