]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
Merge pull request #13480 from Raroun/fix_13457
[friendica.git] / src / Core / Logger.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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 enabled
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 getInstance()
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::getInstance()->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 Message to log
86          * @param array  $context Optional variables
87          * @return void
88          * @throws \Exception
89          */
90         public static function emergency(string $message, array $context = [])
91         {
92                 self::getInstance()->emergency($message, $context);
93         }
94
95         /**
96          * Action must be taken immediately.
97          * @see LoggerInterface::alert()
98          *
99          * Example: Entire website down, database unavailable, etc. This should
100          * trigger the SMS alerts and wake you up.
101          *
102          * @param string $message Message to log
103          * @param array  $context Optional variables
104          * @return void
105          * @throws \Exception
106          */
107         public static function alert(string $message, array $context = [])
108         {
109                 self::getInstance()->alert($message, $context);
110         }
111
112         /**
113          * Critical conditions.
114          * @see LoggerInterface::critical()
115          *
116          * Example: Application component unavailable, unexpected exception.
117          *
118          * @param string $message Message to log
119          * @param array  $context Optional variables
120          * @return void
121          * @throws \Exception
122          */
123         public static function critical(string $message, array $context = [])
124         {
125                 self::getInstance()->critical($message, $context);
126         }
127
128         /**
129          * Runtime errors that do not require immediate action but should typically
130          * be logged and monitored.
131          * @see LoggerInterface::error()
132          *
133          * @param string $message Message to log
134          * @param array  $context Optional variables
135          * @return void
136          * @throws \Exception
137          */
138         public static function error(string $message, array $context = [])
139         {
140                 self::getInstance()->error($message, $context);
141         }
142
143         /**
144          * Exceptional occurrences that are not errors.
145          * @see LoggerInterface::warning()
146          *
147          * Example: Use of deprecated APIs, poor use of an API, undesirable things
148          * that are not necessarily wrong.
149          *
150          * @param string $message Message to log
151          * @param array  $context Optional variables
152          * @return void
153          * @throws \Exception
154          */
155         public static function warning(string $message, array $context = [])
156         {
157                 self::getInstance()->warning($message, $context);
158         }
159
160         /**
161          * Normal but significant events.
162          * @see LoggerInterface::notice()
163          *
164          * @param string $message Message to log
165          * @param array  $context Optional variables
166          * @return void
167          * @throws \Exception
168          */
169         public static function notice(string $message, array $context = [])
170         {
171                 self::getInstance()->notice($message, $context);
172         }
173
174         /**
175          * Interesting events.
176          * @see LoggerInterface::info()
177          *
178          * Example: User logs in, SQL logs.
179          *
180          * @param string $message
181          * @param array  $context
182          *
183          * @return void
184          * @throws \Exception
185          */
186         public static function info(string $message, array $context = [])
187         {
188                 self::getInstance()->info($message, $context);
189         }
190
191         /**
192          * Detailed debug information.
193          * @see LoggerInterface::debug()
194          *
195          * @param string $message Message to log
196          * @param array  $context Optional variables
197          * @return void
198          * @throws \Exception
199          */
200         public static function debug(string $message, array $context = [])
201         {
202                 self::getInstance()->debug($message, $context);
203         }
204
205         /**
206          * An alternative logger for development.
207          *
208          * Works largely as log() but allows developers
209          * to isolate particular elements they are targeting
210          * personally without background noise
211          *
212          * @param string $message Message to log
213          * @param string $level Logging level
214          * @return void
215          * @throws \Exception
216          */
217         public static function devLog(string $message, string $level = LogLevel::DEBUG)
218         {
219                 DI::devLogger()->log($level, $message);
220         }
221 }