]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
Merge pull request #8222 from annando/ap-gnusocial
[friendica.git] / src / Core / Logger.php
1 <?php
2 /**
3  * @file src/Core/Logger.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\DI;
8 use Friendica\Util\Logger\WorkerLogger;
9 use Psr\Log\LoggerInterface;
10 use Psr\Log\LogLevel;
11
12 /**
13  * Logger functions
14  */
15 class Logger
16 {
17         /**
18          * @see Logger::error()
19          * @deprecated since 2019.01
20          */
21         const WARNING = LogLevel::ERROR;
22         /**
23          * @see Logger::warning()
24          * @deprecated since 2019.01
25          */
26         const INFO = LogLevel::WARNING;
27         /**
28          * @see Logger::notice()
29          * @deprecated since 2019.01
30          */
31         const TRACE = LogLevel::NOTICE;
32         /**
33          * @see Logger::info()
34          * @deprecated since 2019.01
35          */
36         const DEBUG = LogLevel::INFO;
37         /**
38          * @see Logger::debug()
39          * @deprecated since 2019.01
40          */
41         const DATA = LogLevel::DEBUG;
42         /**
43          * @see Logger::debug()
44          * @deprecated since 2019.01
45          */
46         const ALL = LogLevel::DEBUG;
47
48         /**
49          * @var LoggerInterface The default Logger type
50          */
51         const TYPE_LOGGER = LoggerInterface::class;
52         /**
53          * @var WorkerLogger A specific worker logger type, which can be anabled
54          */
55         const TYPE_WORKER = WorkerLogger::class;
56         /**
57          * @var LoggerInterface The current logger type
58          */
59         private static $type = self::TYPE_LOGGER;
60
61         /**
62          * @var array the legacy loglevels
63          * @deprecated 2019.03 use PSR-3 loglevels
64          * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
65          *
66          */
67         public static $levels = [
68                 self::WARNING => 'Warning',
69                 self::INFO => 'Info',
70                 self::TRACE => 'Trace',
71                 self::DEBUG => 'Debug',
72                 self::DATA => 'Data',
73         ];
74
75         /**
76          * @return LoggerInterface
77          */
78         private static function getWorker()
79         {
80                 if (self::$type === self::TYPE_LOGGER) {
81                         return DI::logger();
82                 } else {
83                         return DI::workerLogger();
84                 }
85         }
86
87         /**
88          * Enable additional logging for worker usage
89          *
90          * @param string $functionName The worker function, which got called
91          *
92          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
93          */
94         public static function enableWorker(string $functionName)
95         {
96                 self::$type = self::TYPE_WORKER;
97                 self::getWorker()->setFunctionName($functionName);
98         }
99
100         /**
101          * Disable additional logging for worker usage
102          */
103         public static function disableWorker()
104         {
105                 self::$type = self::TYPE_LOGGER;
106         }
107
108         /**
109          * System is unusable.
110          *
111          * @see LoggerInterface::emergency()
112          *
113          * @param string $message
114          * @param array  $context
115          *
116          * @return void
117          * @throws \Exception
118          */
119         public static function emergency($message, $context = [])
120         {
121                 self::getWorker()->emergency($message, $context);
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          * @throws \Exception
136          */
137         public static function alert($message, $context = [])
138         {
139                 self::getWorker()->alert($message, $context);
140         }
141
142         /**
143          * Critical conditions.
144          * @see LoggerInterface::critical()
145          *
146          * Example: Application component unavailable, unexpected exception.
147          *
148          * @param string $message
149          * @param array  $context
150          *
151          * @return void
152          * @throws \Exception
153          */
154         public static function critical($message, $context = [])
155         {
156                 self::getWorker()->critical($message, $context);
157         }
158
159         /**
160          * Runtime errors that do not require immediate action but should typically
161          * be logged and monitored.
162          * @see LoggerInterface::error()
163          *
164          * @param string $message
165          * @param array  $context
166          *
167          * @return void
168          * @throws \Exception
169          */
170         public static function error($message, $context = [])
171         {
172                 self::getWorker()->error($message, $context);
173         }
174
175         /**
176          * Exceptional occurrences that are not errors.
177          * @see LoggerInterface::warning()
178          *
179          * Example: Use of deprecated APIs, poor use of an API, undesirable things
180          * that are not necessarily wrong.
181          *
182          * @param string $message
183          * @param array  $context
184          *
185          * @return void
186          * @throws \Exception
187          */
188         public static function warning($message, $context = [])
189         {
190                 self::getWorker()->warning($message, $context);
191         }
192
193         /**
194          * Normal but significant events.
195          * @see LoggerInterface::notice()
196          *
197          * @param string $message
198          * @param array  $context
199          *
200          * @return void
201          * @throws \Exception
202          */
203         public static function notice($message, $context = [])
204         {
205                 self::getWorker()->notice($message, $context);
206         }
207
208         /**
209          * Interesting events.
210          * @see LoggerInterface::info()
211          *
212          * Example: User logs in, SQL logs.
213          *
214          * @param string $message
215          * @param array  $context
216          *
217          * @return void
218          * @throws \Exception
219          */
220         public static function info($message, $context = [])
221         {
222                 self::getWorker()->info($message, $context);
223         }
224
225         /**
226          * Detailed debug information.
227          * @see LoggerInterface::debug()
228          *
229          * @param string $message
230          * @param array  $context
231          *
232          * @return void
233          * @throws \Exception
234          */
235         public static function debug($message, $context = [])
236         {
237                 self::getWorker()->debug($message, $context);
238         }
239
240         /**
241          * Logs the given message at the given log level
242          *
243          * @param string $msg
244          * @param string $level
245          *
246          * @throws \Exception
247          * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
248          */
249         public static function log($msg, $level = LogLevel::INFO)
250         {
251                 self::getWorker()->log($level, $msg);
252         }
253
254         /**
255          * An alternative logger for development.
256          *
257          * Works largely as log() but allows developers
258          * to isolate particular elements they are targetting
259          * personally without background noise
260          *
261          * @param string $msg
262          * @param string $level
263          * @throws \Exception
264          */
265         public static function devLog($msg, $level = LogLevel::DEBUG)
266         {
267                 DI::devLogger()->log($level, $msg);
268         }
269 }