3 * @file src/Core/Logger.php
5 namespace Friendica\Core;
7 use Friendica\BaseObject;
8 use Friendica\Network\HTTPException\InternalServerErrorException;
9 use Friendica\Util\LoggerFactory;
10 use Psr\Log\LoggerInterface;
14 * @brief Logger functions
16 class Logger extends BaseObject
19 * @deprecated 2019.03 use Logger::error() instead
20 * @see Logger::error()
24 * @deprecated 2019.03 use Logger::warning() instead
25 * @see Logger::warning()
29 * @deprecated 2019.03 use Logger::notice() instead
30 * @see Logger::notice()
34 * @deprecated 2019.03 use Logger::info() instead
39 * @deprecated 2019.03 use Logger::debug() instead
40 * @see Logger::debug()
44 * @deprecated 2019.03 use Logger::debug() instead
45 * @see Logger::debug()
50 * @var array the legacy loglevels
51 * @deprecated 2019.03 use PSR-3 loglevels
52 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
55 public static $levels = [
56 self::WARNING => 'Warning',
58 self::TRACE => 'Trace',
59 self::DEBUG => 'Debug',
65 * @var LoggerInterface A PSR-3 compliant logger instance
67 private static $logger;
70 * @var LoggerInterface A PSR-3 compliant logger instance for developing only
72 private static $devLogger;
75 * Sets the default logging handler for Friendica.
76 * @todo Can be combined with other handlers too if necessary, could be configurable.
78 * @param LoggerInterface $logger The Logger instance of this Application
80 * @throws InternalServerErrorException if the logger factory is incompatible to this logger
82 public static function setLogger($logger)
84 $debugging = Config::get('system', 'debugging');
85 $logfile = Config::get('system', 'logfile');
86 $loglevel = intval(Config::get('system', 'loglevel'));
88 if (!$debugging || !$logfile) {
92 $level = self::mapPSR3Level($loglevel);
93 LoggerFactory::addStreamHandler($logger, $logfile, $level);
95 self::$logger = $logger;
97 $logfile = Config::get('system', 'dlogfile');
103 $developIp = Config::get('system', 'dlogip');
105 self::$devLogger = LoggerFactory::createDev('develop', $developIp);
106 LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
110 * System is unusable.
111 * @see LoggerInterface::emergency()
113 * @param string $message
114 * @param array $context
119 public static function emergency($message, $context = [])
121 if (!isset(self::$logger)) {
125 $stamp1 = microtime(true);
126 self::$logger->emergency($message, $context);
127 self::getApp()->saveTimestamp($stamp1, 'file');
131 * Action must be taken immediately.
132 * @see LoggerInterface::alert()
134 * Example: Entire website down, database unavailable, etc. This should
135 * trigger the SMS alerts and wake you up.
137 * @param string $message
138 * @param array $context
143 public static function alert($message, $context = [])
145 if (!isset(self::$logger)) {
149 $stamp1 = microtime(true);
150 self::$logger->alert($message, $context);
151 self::getApp()->saveTimestamp($stamp1, 'file');
155 * Critical conditions.
156 * @see LoggerInterface::critical()
158 * Example: Application component unavailable, unexpected exception.
160 * @param string $message
161 * @param array $context
166 public static function critical($message, $context = [])
168 if (!isset(self::$logger)) {
172 $stamp1 = microtime(true);
173 self::$logger->critical($message, $context);
174 self::getApp()->saveTimestamp($stamp1, 'file');
178 * Runtime errors that do not require immediate action but should typically
179 * be logged and monitored.
180 * @see LoggerInterface::error()
182 * @param string $message
183 * @param array $context
188 public static function error($message, $context = [])
190 if (!isset(self::$logger)) {
195 $stamp1 = microtime(true);
196 self::$logger->error($message, $context);
197 self::getApp()->saveTimestamp($stamp1, 'file');
201 * Exceptional occurrences that are not errors.
202 * @see LoggerInterface::warning()
204 * Example: Use of deprecated APIs, poor use of an API, undesirable things
205 * that are not necessarily wrong.
207 * @param string $message
208 * @param array $context
213 public static function warning($message, $context = [])
215 if (!isset(self::$logger)) {
219 $stamp1 = microtime(true);
220 self::$logger->warning($message, $context);
221 self::getApp()->saveTimestamp($stamp1, 'file');
225 * Normal but significant events.
226 * @see LoggerInterface::notice()
228 * @param string $message
229 * @param array $context
234 public static function notice($message, $context = [])
236 if (!isset(self::$logger)) {
240 $stamp1 = microtime(true);
241 self::$logger->notice($message, $context);
242 self::getApp()->saveTimestamp($stamp1, 'file');
246 * Interesting events.
247 * @see LoggerInterface::info()
249 * Example: User logs in, SQL logs.
251 * @param string $message
252 * @param array $context
257 public static function info($message, $context = [])
259 if (!isset(self::$logger)) {
263 $stamp1 = microtime(true);
264 self::$logger->info($message, $context);
265 self::getApp()->saveTimestamp($stamp1, 'file');
269 * Detailed debug information.
270 * @see LoggerInterface::debug()
272 * @param string $message
273 * @param array $context
277 public static function debug($message, $context = [])
279 if (!isset(self::$logger)) {
283 $stamp1 = microtime(true);
284 self::$logger->debug($message, $context);
285 self::getApp()->saveTimestamp($stamp1, 'file');
289 * Mapping a legacy level to the PSR-3 compliant levels
290 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
292 * @param int $level the level to be mapped
294 * @return string the PSR-3 compliant level
296 private static function mapPSR3Level($level)
300 return LogLevel::ERROR;
302 return LogLevel::WARNING;
304 return LogLevel::NOTICE;
306 return LogLevel::INFO;
308 return LogLevel::DEBUG;
310 return LogLevel::DEBUG;
312 return LogLevel::CRITICAL;
317 * @brief Logs the given message at the given log level
322 * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
324 public static function log($msg, $level = self::INFO)
326 if (!isset(self::$logger)) {
330 $loglevel = self::mapPSR3Level($level);
332 $stamp1 = microtime(true);
333 self::$logger->log($loglevel, $msg);
334 self::getApp()->saveTimestamp($stamp1, "file");
338 * @brief An alternative logger for development.
339 * Works largely as log() but allows developers
340 * to isolate particular elements they are targetting
341 * personally without background noise
344 * @param string $level
346 public static function devLog($msg, $level = LogLevel::DEBUG)
348 if (!isset(self::$logger)) {
352 $stamp1 = microtime(true);
353 self::$devLogger->log($level, $msg);
354 self::getApp()->saveTimestamp($stamp1, "file");