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