]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
e376485e564adae830ee0e1103d71d1a868fe281
[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\Util\Logger\WorkerLogger;
9 use Psr\Log\LoggerInterface;
10 use Psr\Log\LogLevel;
11
12 /**
13  * @brief Logger functions
14  */
15 class Logger extends BaseObject
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                 self::ALL => 'All',
68         ];
69
70         /**
71          * Enable additional logging for worker usage
72          *
73          * @param string $functionName The worker function, which got called
74          *
75          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
76          */
77         public static function enableWorker(string $functionName)
78         {
79                 self::$type = self::TYPE_WORKER;
80                 self::getClass(self::$type)->setFunctionName($functionName);
81         }
82
83         /**
84          * Disable additional logging for worker usage
85          */
86         public static function disableWorker()
87         {
88                 self::$type = self::TYPE_LOGGER;
89         }
90
91         /**
92          * System is unusable.
93          *
94          * @see LoggerInterface::emergency()
95          *
96          * @param string $message
97          * @param array  $context
98          *
99          * @return void
100          * @throws \Exception
101          */
102         public static function emergency($message, $context = [])
103         {
104                 self::getClass(self::$type)->emergency($message, $context);
105         }
106
107         /**
108          * Action must be taken immediately.
109          * @see LoggerInterface::alert()
110          *
111          * Example: Entire website down, database unavailable, etc. This should
112          * trigger the SMS alerts and wake you up.
113          *
114          * @param string $message
115          * @param array  $context
116          *
117          * @return void
118          * @throws \Exception
119          */
120         public static function alert($message, $context = [])
121         {
122                 self::getClass(self::$type)->alert($message, $context);
123         }
124
125         /**
126          * Critical conditions.
127          * @see LoggerInterface::critical()
128          *
129          * Example: Application component unavailable, unexpected exception.
130          *
131          * @param string $message
132          * @param array  $context
133          *
134          * @return void
135          * @throws \Exception
136          */
137         public static function critical($message, $context = [])
138         {
139                 self::getClass(self::$type)->critical($message, $context);
140         }
141
142         /**
143          * Runtime errors that do not require immediate action but should typically
144          * be logged and monitored.
145          * @see LoggerInterface::error()
146          *
147          * @param string $message
148          * @param array  $context
149          *
150          * @return void
151          * @throws \Exception
152          */
153         public static function error($message, $context = [])
154         {
155                 self::getClass(self::$type)->error($message, $context);
156         }
157
158         /**
159          * Exceptional occurrences that are not errors.
160          * @see LoggerInterface::warning()
161          *
162          * Example: Use of deprecated APIs, poor use of an API, undesirable things
163          * that are not necessarily wrong.
164          *
165          * @param string $message
166          * @param array  $context
167          *
168          * @return void
169          * @throws \Exception
170          */
171         public static function warning($message, $context = [])
172         {
173                 self::getClass(self::$type)->warning($message, $context);
174         }
175
176         /**
177          * Normal but significant events.
178          * @see LoggerInterface::notice()
179          *
180          * @param string $message
181          * @param array  $context
182          *
183          * @return void
184          * @throws \Exception
185          */
186         public static function notice($message, $context = [])
187         {
188                 self::getClass(self::$type)->notice($message, $context);
189         }
190
191         /**
192          * Interesting events.
193          * @see LoggerInterface::info()
194          *
195          * Example: User logs in, SQL logs.
196          *
197          * @param string $message
198          * @param array  $context
199          *
200          * @return void
201          * @throws \Exception
202          */
203         public static function info($message, $context = [])
204         {
205                 self::getClass(self::$type)->info($message, $context);
206         }
207
208         /**
209          * Detailed debug information.
210          * @see LoggerInterface::debug()
211          *
212          * @param string $message
213          * @param array  $context
214          *
215          * @return void
216          * @throws \Exception
217          */
218         public static function debug($message, $context = [])
219         {
220                 self::getClass(self::$type)->debug($message, $context);
221         }
222
223             /**
224          * @brief Logs the given message at the given log level
225          *
226          * @param string $msg
227          * @param string $level
228          *
229          * @throws \Exception
230          * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
231          */
232         public static function log($msg, $level = LogLevel::INFO)
233         {
234                 self::getClass(self::$type)->log($level, $msg);
235         }
236
237         /**
238          * @brief An alternative logger for development.
239          * Works largely as log() but allows developers
240          * to isolate particular elements they are targetting
241          * personally without background noise
242          *
243          * @param string $msg
244          * @param string $level
245          * @throws \Exception
246          */
247         public static function devLog($msg, $level = LogLevel::DEBUG)
248         {
249                 self::getClass('$devLogger')->log($level, $msg);
250         }
251 }