Moving Profiling to class
[friendica.git/.git] / src / Core / Logger.php
1 <?php
2 /**
3  * @file src/Core/Logger.php
4  */
5 namespace Friendica\Core;
6
7 use Psr\Log\LoggerInterface;
8 use Psr\Log\LogLevel;
9
10 /**
11  * @brief Logger functions
12  */
13 class Logger
14 {
15         /**
16          * @see Logger::error()
17          */
18         const WARNING = LogLevel::ERROR;
19         /**
20          * @see Logger::warning()
21          */
22         const INFO = LogLevel::WARNING;
23         /**
24          * @see Logger::notice()
25          */
26         const TRACE = LogLevel::NOTICE;
27         /**
28          * @see Logger::info()
29          */
30         const DEBUG = LogLevel::INFO;
31         /**
32          * @see Logger::debug()
33          */
34         const DATA = LogLevel::DEBUG;
35         /**
36          * @see Logger::debug()
37          */
38         const ALL = LogLevel::DEBUG;
39
40         /**
41          * @var array the legacy loglevels
42          * @deprecated 2019.03 use PSR-3 loglevels
43          * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
44          *
45          */
46         public static $levels = [
47                 self::WARNING => 'Warning',
48                 self::INFO => 'Info',
49                 self::TRACE => 'Trace',
50                 self::DEBUG => 'Debug',
51                 self::DATA => 'Data',
52                 self::ALL => 'All',
53         ];
54
55         /**
56          * @var LoggerInterface A PSR-3 compliant logger instance
57          */
58         private static $logger;
59
60         /**
61          * @var LoggerInterface A PSR-3 compliant logger instance for developing only
62          */
63         private static $devLogger;
64
65         /**
66          * Sets the default logging handler for Friendica.
67          *
68          * @param LoggerInterface $logger The Logger instance of this Application
69          */
70         public static function init(LoggerInterface $logger)
71         {
72                 self::$logger = $logger;
73         }
74
75         /**
76          * Sets the default dev-logging handler for Friendica.
77          *
78          * @param LoggerInterface $logger The Logger instance of this Application
79          */
80         public static function setDevLogger(LoggerInterface $logger)
81         {
82                 self::$devLogger = $logger;
83         }
84
85         /**
86          * System is unusable.
87          *
88          * @see LoggerInterface::emergency()
89          *
90          * @param string $message
91          * @param array  $context
92          *
93          * @return void
94          * @throws \Exception
95          */
96         public static function emergency($message, $context = [])
97         {
98                 self::$logger->emergency($message, $context);
99         }
100
101         /**
102          * Action must be taken immediately.
103          * @see LoggerInterface::alert()
104          *
105          * Example: Entire website down, database unavailable, etc. This should
106          * trigger the SMS alerts and wake you up.
107          *
108          * @param string $message
109          * @param array  $context
110          *
111          * @return void
112          * @throws \Exception
113          */
114         public static function alert($message, $context = [])
115         {
116                 self::$logger->alert($message, $context);
117         }
118
119         /**
120          * Critical conditions.
121          * @see LoggerInterface::critical()
122          *
123          * Example: Application component unavailable, unexpected exception.
124          *
125          * @param string $message
126          * @param array  $context
127          *
128          * @return void
129          * @throws \Exception
130          */
131         public static function critical($message, $context = [])
132         {
133                 self::$logger->critical($message, $context);
134         }
135
136         /**
137          * Runtime errors that do not require immediate action but should typically
138          * be logged and monitored.
139          * @see LoggerInterface::error()
140          *
141          * @param string $message
142          * @param array  $context
143          *
144          * @return void
145          * @throws \Exception
146          */
147         public static function error($message, $context = [])
148         {
149                 self::$logger->error($message, $context);
150         }
151
152         /**
153          * Exceptional occurrences that are not errors.
154          * @see LoggerInterface::warning()
155          *
156          * Example: Use of deprecated APIs, poor use of an API, undesirable things
157          * that are not necessarily wrong.
158          *
159          * @param string $message
160          * @param array  $context
161          *
162          * @return void
163          * @throws \Exception
164          */
165         public static function warning($message, $context = [])
166         {
167                 self::$logger->warning($message, $context);
168         }
169
170         /**
171          * Normal but significant events.
172          * @see LoggerInterface::notice()
173          *
174          * @param string $message
175          * @param array  $context
176          *
177          * @return void
178          * @throws \Exception
179          */
180         public static function notice($message, $context = [])
181         {
182                 self::$logger->notice($message, $context);
183         }
184
185         /**
186          * Interesting events.
187          * @see LoggerInterface::info()
188          *
189          * Example: User logs in, SQL logs.
190          *
191          * @param string $message
192          * @param array  $context
193          *
194          * @return void
195          * @throws \Exception
196          */
197         public static function info($message, $context = [])
198         {
199                 self::$logger->info($message, $context);
200         }
201
202         /**
203          * Detailed debug information.
204          * @see LoggerInterface::debug()
205          *
206          * @param string $message
207          * @param array  $context
208          *
209          * @return void
210          * @throws \Exception
211          */
212         public static function debug($message, $context = [])
213         {
214                 self::$logger->debug($message, $context);
215         }
216
217             /**
218          * @brief Logs the given message at the given log level
219          *
220          * @param string $msg
221          * @param string $level
222          *
223          * @throws \Exception
224          * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
225          */
226         public static function log($msg, $level = LogLevel::INFO)
227         {
228                 self::$logger->log($level, $msg);
229         }
230
231         /**
232          * @brief An alternative logger for development.
233          * Works largely as log() but allows developers
234          * to isolate particular elements they are targetting
235          * personally without background noise
236          *
237          * @param string $msg
238          * @param string $level
239          * @throws \Exception
240          */
241         public static function devLog($msg, $level = LogLevel::DEBUG)
242         {
243                 if (!isset(self::$devLogger)) {
244                         return;
245                 }
246
247                 self::$devLogger->log($level, $msg);
248         }
249 }