]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
Remove confirm template obsolete uses (except for contacts)
[friendica.git] / src / Core / Logger.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Friendica\DI;
25 use Friendica\Util\Logger\WorkerLogger;
26 use Psr\Log\LoggerInterface;
27 use Psr\Log\LogLevel;
28
29 /**
30  * Logger functions
31  */
32 class Logger
33 {
34         /**
35          * @see Logger::error()
36          * @deprecated since 2019.01
37          */
38         const WARNING = LogLevel::ERROR;
39         /**
40          * @see Logger::warning()
41          * @deprecated since 2019.01
42          */
43         const INFO = LogLevel::WARNING;
44         /**
45          * @see Logger::notice()
46          * @deprecated since 2019.01
47          */
48         const TRACE = LogLevel::NOTICE;
49         /**
50          * @see Logger::info()
51          * @deprecated since 2019.01
52          */
53         const DEBUG = LogLevel::INFO;
54         /**
55          * @see Logger::debug()
56          * @deprecated since 2019.01
57          */
58         const DATA = LogLevel::DEBUG;
59         /**
60          * @see Logger::debug()
61          * @deprecated since 2019.01
62          */
63         const ALL = LogLevel::DEBUG;
64
65         /**
66          * @var LoggerInterface The default Logger type
67          */
68         const TYPE_LOGGER = LoggerInterface::class;
69         /**
70          * @var WorkerLogger A specific worker logger type, which can be anabled
71          */
72         const TYPE_WORKER = WorkerLogger::class;
73         /**
74          * @var LoggerInterface The current logger type
75          */
76         private static $type = self::TYPE_LOGGER;
77
78         /**
79          * @var array the legacy loglevels
80          * @deprecated 2019.03 use PSR-3 loglevels
81          * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
82          *
83          */
84         public static $levels = [
85                 self::WARNING => 'Warning',
86                 self::INFO => 'Info',
87                 self::TRACE => 'Trace',
88                 self::DEBUG => 'Debug',
89                 self::DATA => 'Data',
90         ];
91
92         /**
93          * @return LoggerInterface
94          */
95         private static function getWorker()
96         {
97                 if (self::$type === self::TYPE_LOGGER) {
98                         return DI::logger();
99                 } else {
100                         return DI::workerLogger();
101                 }
102         }
103
104         /**
105          * Enable additional logging for worker usage
106          *
107          * @param string $functionName The worker function, which got called
108          *
109          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
110          */
111         public static function enableWorker(string $functionName)
112         {
113                 self::$type = self::TYPE_WORKER;
114                 self::getWorker()->setFunctionName($functionName);
115         }
116
117         /**
118          * Disable additional logging for worker usage
119          */
120         public static function disableWorker()
121         {
122                 self::$type = self::TYPE_LOGGER;
123         }
124
125         /**
126          * System is unusable.
127          *
128          * @see LoggerInterface::emergency()
129          *
130          * @param string $message
131          * @param array  $context
132          *
133          * @return void
134          * @throws \Exception
135          */
136         public static function emergency($message, $context = [])
137         {
138                 self::getWorker()->emergency($message, $context);
139         }
140
141         /**
142          * Action must be taken immediately.
143          * @see LoggerInterface::alert()
144          *
145          * Example: Entire website down, database unavailable, etc. This should
146          * trigger the SMS alerts and wake you up.
147          *
148          * @param string $message
149          * @param array  $context
150          *
151          * @return void
152          * @throws \Exception
153          */
154         public static function alert($message, $context = [])
155         {
156                 self::getWorker()->alert($message, $context);
157         }
158
159         /**
160          * Critical conditions.
161          * @see LoggerInterface::critical()
162          *
163          * Example: Application component unavailable, unexpected exception.
164          *
165          * @param string $message
166          * @param array  $context
167          *
168          * @return void
169          * @throws \Exception
170          */
171         public static function critical($message, $context = [])
172         {
173                 self::getWorker()->critical($message, $context);
174         }
175
176         /**
177          * Runtime errors that do not require immediate action but should typically
178          * be logged and monitored.
179          * @see LoggerInterface::error()
180          *
181          * @param string $message
182          * @param array  $context
183          *
184          * @return void
185          * @throws \Exception
186          */
187         public static function error($message, $context = [])
188         {
189                 self::getWorker()->error($message, $context);
190         }
191
192         /**
193          * Exceptional occurrences that are not errors.
194          * @see LoggerInterface::warning()
195          *
196          * Example: Use of deprecated APIs, poor use of an API, undesirable things
197          * that are not necessarily wrong.
198          *
199          * @param string $message
200          * @param array  $context
201          *
202          * @return void
203          * @throws \Exception
204          */
205         public static function warning($message, $context = [])
206         {
207                 self::getWorker()->warning($message, $context);
208         }
209
210         /**
211          * Normal but significant events.
212          * @see LoggerInterface::notice()
213          *
214          * @param string $message
215          * @param array  $context
216          *
217          * @return void
218          * @throws \Exception
219          */
220         public static function notice($message, $context = [])
221         {
222                 self::getWorker()->notice($message, $context);
223         }
224
225         /**
226          * Interesting events.
227          * @see LoggerInterface::info()
228          *
229          * Example: User logs in, SQL logs.
230          *
231          * @param string $message
232          * @param array  $context
233          *
234          * @return void
235          * @throws \Exception
236          */
237         public static function info($message, $context = [])
238         {
239                 self::getWorker()->info($message, $context);
240         }
241
242         /**
243          * Detailed debug information.
244          * @see LoggerInterface::debug()
245          *
246          * @param string $message
247          * @param array  $context
248          *
249          * @return void
250          * @throws \Exception
251          */
252         public static function debug($message, $context = [])
253         {
254                 self::getWorker()->debug($message, $context);
255         }
256
257         /**
258          * Logs the given message at the given log level
259          *
260          * @param string $msg
261          * @param string $level
262          *
263          * @throws \Exception
264          * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
265          */
266         public static function log($msg, $level = LogLevel::INFO)
267         {
268                 self::getWorker()->log($level, $msg);
269         }
270
271         /**
272          * An alternative logger for development.
273          *
274          * Works largely as log() but allows developers
275          * to isolate particular elements they are targetting
276          * personally without background noise
277          *
278          * @param string $msg
279          * @param string $level
280          * @throws \Exception
281          */
282         public static function devLog($msg, $level = LogLevel::DEBUG)
283         {
284                 DI::devLogger()->log($level, $msg);
285         }
286 }