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