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