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 LoggerFactory::addStreamHandler($logger, $logfile, $loglevel);
94 $logfile = Config::get('system', 'dlogfile');
100 $developIp = Config::get('system', 'dlogip');
102 self::$devLogger = LoggerFactory::createDev('develop', $developIp);
103 LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
107 * System is unusable.
108 * @see LoggerInterface::emergency()
110 * @param string $message
111 * @param array $context
116 public static function emergency($message, $context = [])
118 if (!isset(self::$logger)) {
122 $stamp1 = microtime(true);
123 self::$logger->emergency($message, $context);
124 self::getApp()->saveTimestamp($stamp1, 'file');
128 * Action must be taken immediately.
129 * @see LoggerInterface::alert()
131 * Example: Entire website down, database unavailable, etc. This should
132 * trigger the SMS alerts and wake you up.
134 * @param string $message
135 * @param array $context
140 public static function alert($message, $context = [])
142 if (!isset(self::$logger)) {
146 $stamp1 = microtime(true);
147 self::$logger->alert($message, $context);
148 self::getApp()->saveTimestamp($stamp1, 'file');
152 * Critical conditions.
153 * @see LoggerInterface::critical()
155 * Example: Application component unavailable, unexpected exception.
157 * @param string $message
158 * @param array $context
163 public static function critical($message, $context = [])
165 if (!isset(self::$logger)) {
169 $stamp1 = microtime(true);
170 self::$logger->critical($message, $context);
171 self::getApp()->saveTimestamp($stamp1, 'file');
175 * Runtime errors that do not require immediate action but should typically
176 * be logged and monitored.
177 * @see LoggerInterface::error()
179 * @param string $message
180 * @param array $context
185 public static function error($message, $context = [])
187 if (!isset(self::$logger)) {
191 $stamp1 = microtime(true);
192 self::$logger->error($message, $context);
193 self::getApp()->saveTimestamp($stamp1, 'file');
197 * Exceptional occurrences that are not errors.
198 * @see LoggerInterface::warning()
200 * Example: Use of deprecated APIs, poor use of an API, undesirable things
201 * that are not necessarily wrong.
203 * @param string $message
204 * @param array $context
209 public static function warning($message, $context = [])
211 if (!isset(self::$logger)) {
215 $stamp1 = microtime(true);
216 self::$logger->warning($message, $context);
217 self::getApp()->saveTimestamp($stamp1, 'file');
221 * Normal but significant events.
222 * @see LoggerInterface::notice()
224 * @param string $message
225 * @param array $context
230 public static function notice($message, $context = [])
232 if (!isset(self::$logger)) {
236 $stamp1 = microtime(true);
237 self::$logger->notice($message, $context);
238 self::getApp()->saveTimestamp($stamp1, 'file');
242 * Interesting events.
243 * @see LoggerInterface::info()
245 * Example: User logs in, SQL logs.
247 * @param string $message
248 * @param array $context
253 public static function info($message, $context = [])
255 if (!isset(self::$logger)) {
259 $stamp1 = microtime(true);
260 self::$logger->info($message, $context);
261 self::getApp()->saveTimestamp($stamp1, 'file');
265 * Detailed debug information.
266 * @see LoggerInterface::debug()
268 * @param string $message
269 * @param array $context
273 public static function debug($message, $context = [])
275 if (!isset(self::$logger)) {
279 $stamp1 = microtime(true);
280 self::$logger->debug($message, $context);
281 self::getApp()->saveTimestamp($stamp1, 'file');
285 * Mapping a legacy level to the PSR-3 compliant levels
286 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
288 * @param int $level the level to be mapped
290 * @return string the PSR-3 compliant level
292 private static function mapPSR3Level($level)
296 return LogLevel::ERROR;
298 return LogLevel::WARNING;
300 return LogLevel::NOTICE;
302 return LogLevel::INFO;
304 return LogLevel::DEBUG;
306 return LogLevel::DEBUG;
308 return LogLevel::CRITICAL;
313 * @brief Logs the given message at the given log level
318 * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
320 public static function log($msg, $level = self::INFO)
322 if (!isset(self::$logger)) {
326 $loglevel = self::mapPSR3Level($level);
328 $stamp1 = microtime(true);
329 self::$logger->log($loglevel, $msg);
330 self::getApp()->saveTimestamp($stamp1, "file");
334 * @brief An alternative logger for development.
335 * Works largely as log() but allows developers
336 * to isolate particular elements they are targetting
337 * personally without background noise
340 * @param string $level
342 public static function devLog($msg, $level = LogLevel::DEBUG)
344 if (!isset(self::$logger)) {
348 $stamp1 = microtime(true);
349 self::$devLogger->log($level, $msg);
350 self::getApp()->saveTimestamp($stamp1, "file");