]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
Refactor Loglevels
[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 = intval(Config::get('system', 'loglevel'));
81
82                 if (!$debugging || !$logfile) {
83                         return;
84                 }
85
86                 $level = self::mapPSR3Level($loglevel);
87                 LoggerFactory::addStreamHandler($logger, $logfile, $level);
88
89                 self::$logger = $logger;
90
91                 $logfile = Config::get('system', 'dlogfile');
92
93                 if (!$logfile) {
94                         return;
95                 }
96
97                 $developIp = Config::get('system', 'dlogip');
98
99                 self::$devLogger = LoggerFactory::createDev('develop', $developIp);
100                 LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
101         }
102
103         /**
104          * System is unusable.
105          * @see LoggerInterface::emergency()
106          *
107          * @param string $message
108          * @param array  $context
109          *
110          * @return void
111          *
112          */
113         public static function emergency($message, $context = [])
114         {
115                 if (!isset(self::$logger)) {
116                         return;
117                 }
118
119                 $stamp1 = microtime(true);
120                 self::$logger->emergency($message, $context);
121                 self::getApp()->saveTimestamp($stamp1, 'file');
122         }
123
124         /**
125          * Action must be taken immediately.
126          * @see LoggerInterface::alert()
127          *
128          * Example: Entire website down, database unavailable, etc. This should
129          * trigger the SMS alerts and wake you up.
130          *
131          * @param string $message
132          * @param array  $context
133          *
134          * @return void
135          *
136          */
137         public static function alert($message, $context = [])
138         {
139                 if (!isset(self::$logger)) {
140                         return;
141                 }
142
143                 $stamp1 = microtime(true);
144                 self::$logger->alert($message, $context);
145                 self::getApp()->saveTimestamp($stamp1, 'file');
146         }
147
148         /**
149          * Critical conditions.
150          * @see LoggerInterface::critical()
151          *
152          * Example: Application component unavailable, unexpected exception.
153          *
154          * @param string $message
155          * @param array  $context
156          *
157          * @return void
158          *
159          */
160         public static function critical($message, $context = [])
161         {
162                 if (!isset(self::$logger)) {
163                         return;
164                 }
165
166                 $stamp1 = microtime(true);
167                 self::$logger->critical($message, $context);
168                 self::getApp()->saveTimestamp($stamp1, 'file');
169         }
170
171         /**
172          * Runtime errors that do not require immediate action but should typically
173          * be logged and monitored.
174          * @see LoggerInterface::error()
175          *
176          * @param string $message
177          * @param array  $context
178          *
179          * @return void
180          *
181          */
182         public static function error($message, $context = [])
183         {
184                 if (!isset(self::$logger)) {
185                         echo "not set!?\n";
186                         return;
187                 }
188
189                 $stamp1 = microtime(true);
190                 self::$logger->error($message, $context);
191                 self::getApp()->saveTimestamp($stamp1, 'file');
192         }
193
194         /**
195          * Exceptional occurrences that are not errors.
196          * @see LoggerInterface::warning()
197          *
198          * Example: Use of deprecated APIs, poor use of an API, undesirable things
199          * that are not necessarily wrong.
200          *
201          * @param string $message
202          * @param array  $context
203          *
204          * @return void
205          *
206          */
207         public static function warning($message, $context = [])
208         {
209                 if (!isset(self::$logger)) {
210                         return;
211                 }
212
213                 $stamp1 = microtime(true);
214                 self::$logger->warning($message, $context);
215                 self::getApp()->saveTimestamp($stamp1, 'file');
216         }
217
218         /**
219          * Normal but significant events.
220          * @see LoggerInterface::notice()
221          *
222          * @param string $message
223          * @param array  $context
224          *
225          * @return void
226          *
227          */
228         public static function notice($message, $context = [])
229         {
230                 if (!isset(self::$logger)) {
231                         return;
232                 }
233
234                 $stamp1 = microtime(true);
235                 self::$logger->notice($message, $context);
236                 self::getApp()->saveTimestamp($stamp1, 'file');
237         }
238
239         /**
240          * Interesting events.
241          * @see LoggerInterface::info()
242          *
243          * Example: User logs in, SQL logs.
244          *
245          * @param string $message
246          * @param array  $context
247          *
248          * @return void
249          *
250          */
251         public static function info($message, $context = [])
252         {
253                 if (!isset(self::$logger)) {
254                         return;
255                 }
256
257                 $stamp1 = microtime(true);
258                 self::$logger->info($message, $context);
259                 self::getApp()->saveTimestamp($stamp1, 'file');
260         }
261
262         /**
263          * Detailed debug information.
264          * @see LoggerInterface::debug()
265          *
266          * @param string $message
267          * @param array  $context
268          *
269          * @return void
270          */
271         public static function debug($message, $context = [])
272         {
273                 if (!isset(self::$logger)) {
274                         return;
275                 }
276
277                 $stamp1 = microtime(true);
278                 self::$logger->debug($message, $context);
279                 self::getApp()->saveTimestamp($stamp1, 'file');
280         }
281
282     /**
283      * @brief Logs the given message at the given log level
284      *
285      * @param string $msg
286      * @param int $level
287          *
288          * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
289      */
290     public static function log($msg, $level = LogLevel::NOTICE)
291     {
292                 if (!isset(self::$logger)) {
293                         return;
294                 }
295
296         $stamp1 = microtime(true);
297                 self::$logger->log($level, $msg);
298         self::getApp()->saveTimestamp($stamp1, "file");
299     }
300
301     /**
302      * @brief An alternative logger for development.
303      * Works largely as log() but allows developers
304      * to isolate particular elements they are targetting
305      * personally without background noise
306      *
307      * @param string $msg
308          * @param string $level
309      */
310     public static function devLog($msg, $level = LogLevel::DEBUG)
311     {
312                 if (!isset(self::$logger)) {
313                         return;
314                 }
315
316         $stamp1 = microtime(true);
317         self::$devLogger->log($level, $msg);
318         self::getApp()->saveTimestamp($stamp1, "file");
319     }
320 }