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