]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
Merge pull request #11015 from MrPetovan/task/10979-frio-time-tooltip
[friendica.git] / src / Core / Logger.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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\Core\Logger\Type\WorkerLogger;
26 use Psr\Log\LoggerInterface;
27 use Psr\Log\LogLevel;
28
29 /**
30  * Logger functions
31  */
32 class Logger
33 {
34         /**
35          * @var LoggerInterface The default Logger type
36          */
37         const TYPE_LOGGER = LoggerInterface::class;
38         /**
39          * @var WorkerLogger A specific worker logger type, which can be anabled
40          */
41         const TYPE_WORKER = WorkerLogger::class;
42         /**
43          * @var LoggerInterface The current logger type
44          */
45         private static $type = self::TYPE_LOGGER;
46
47         /**
48          * @return LoggerInterface
49          */
50         private static function getWorker()
51         {
52                 if (self::$type === self::TYPE_LOGGER) {
53                         return DI::logger();
54                 } else {
55                         return DI::workerLogger();
56                 }
57         }
58
59         /**
60          * Enable additional logging for worker usage
61          *
62          * @param string $functionName The worker function, which got called
63          *
64          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
65          */
66         public static function enableWorker(string $functionName)
67         {
68                 self::$type = self::TYPE_WORKER;
69                 self::getWorker()->setFunctionName($functionName);
70         }
71
72         /**
73          * Disable additional logging for worker usage
74          */
75         public static function disableWorker()
76         {
77                 self::$type = self::TYPE_LOGGER;
78         }
79
80         /**
81          * System is unusable.
82          *
83          * @see LoggerInterface::emergency()
84          *
85          * @param string $message
86          * @param array  $context
87          *
88          * @return void
89          * @throws \Exception
90          */
91         public static function emergency($message, $context = [])
92         {
93                 self::getWorker()->emergency($message, $context);
94         }
95
96         /**
97          * Action must be taken immediately.
98          * @see LoggerInterface::alert()
99          *
100          * Example: Entire website down, database unavailable, etc. This should
101          * trigger the SMS alerts and wake you up.
102          *
103          * @param string $message
104          * @param array  $context
105          *
106          * @return void
107          * @throws \Exception
108          */
109         public static function alert($message, $context = [])
110         {
111                 self::getWorker()->alert($message, $context);
112         }
113
114         /**
115          * Critical conditions.
116          * @see LoggerInterface::critical()
117          *
118          * Example: Application component unavailable, unexpected exception.
119          *
120          * @param string $message
121          * @param array  $context
122          *
123          * @return void
124          * @throws \Exception
125          */
126         public static function critical($message, $context = [])
127         {
128                 self::getWorker()->critical($message, $context);
129         }
130
131         /**
132          * Runtime errors that do not require immediate action but should typically
133          * be logged and monitored.
134          * @see LoggerInterface::error()
135          *
136          * @param string $message
137          * @param array  $context
138          *
139          * @return void
140          * @throws \Exception
141          */
142         public static function error($message, $context = [])
143         {
144                 self::getWorker()->error($message, $context);
145         }
146
147         /**
148          * Exceptional occurrences that are not errors.
149          * @see LoggerInterface::warning()
150          *
151          * Example: Use of deprecated APIs, poor use of an API, undesirable things
152          * that are not necessarily wrong.
153          *
154          * @param string $message
155          * @param array  $context
156          *
157          * @return void
158          * @throws \Exception
159          */
160         public static function warning($message, $context = [])
161         {
162                 self::getWorker()->warning($message, $context);
163         }
164
165         /**
166          * Normal but significant events.
167          * @see LoggerInterface::notice()
168          *
169          * @param string $message
170          * @param array  $context
171          *
172          * @return void
173          * @throws \Exception
174          */
175         public static function notice($message, $context = [])
176         {
177                 self::getWorker()->notice($message, $context);
178         }
179
180         /**
181          * Interesting events.
182          * @see LoggerInterface::info()
183          *
184          * Example: User logs in, SQL logs.
185          *
186          * @param string $message
187          * @param array  $context
188          *
189          * @return void
190          * @throws \Exception
191          */
192         public static function info($message, $context = [])
193         {
194                 self::getWorker()->info($message, $context);
195         }
196
197         /**
198          * Detailed debug information.
199          * @see LoggerInterface::debug()
200          *
201          * @param string $message
202          * @param array  $context
203          *
204          * @return void
205          * @throws \Exception
206          */
207         public static function debug($message, $context = [])
208         {
209                 self::getWorker()->debug($message, $context);
210         }
211
212         /**
213          * An alternative logger for development.
214          *
215          * Works largely as log() but allows developers
216          * to isolate particular elements they are targetting
217          * personally without background noise
218          *
219          * @param string $msg
220          * @param string $level
221          * @throws \Exception
222          */
223         public static function devLog($msg, $level = LogLevel::DEBUG)
224         {
225                 DI::devLogger()->log($level, $msg);
226         }
227 }