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 * @see Logger::error()
21 const WARNING = LogLevel::ERROR;
23 * @see Logger::warning()
25 const INFO = LogLevel::WARNING;
27 * @see Logger::notice()
29 const TRACE = LogLevel::NOTICE;
33 const DEBUG = LogLevel::INFO;
35 * @see Logger::debug()
37 const DATA = LogLevel::DEBUG;
39 * @see Logger::debug()
41 const ALL = LogLevel::DEBUG;
44 * @var array the legacy loglevels
45 * @deprecated 2019.03 use PSR-3 loglevels
46 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
49 public static $levels = [
50 self::WARNING => 'Warning',
52 self::TRACE => 'Trace',
53 self::DEBUG => 'Debug',
59 * @var LoggerInterface A PSR-3 compliant logger instance
61 private static $logger;
64 * @var LoggerInterface A PSR-3 compliant logger instance for developing only
66 private static $devLogger;
69 * Sets the default logging handler for Friendica.
70 * @todo Can be combined with other handlers too if necessary, could be configurable.
72 * @param LoggerInterface $logger The Logger instance of this Application
74 * @throws InternalServerErrorException if the logger factory is incompatible to this logger
76 public static function setLogger($logger)
78 $debugging = Config::get('system', 'debugging');
79 $logfile = Config::get('system', 'logfile');
80 $loglevel = Config::get('system', 'loglevel');
82 if (!$debugging || !$logfile) {
86 if (is_int($loglevel)) {
87 $loglevel = self::mapLegacyConfigDebugLevel($loglevel);
90 LoggerFactory::addStreamHandler($logger, $logfile, $loglevel);
92 self::$logger = $logger;
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 * Mapping a legacy level to the PSR-3 compliant levels
108 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
110 * @param int $level the level to be mapped
112 * @return string the PSR-3 compliant level
114 private static function mapLegacyConfigDebugLevel($level)
119 return LogLevel::ERROR;
122 return LogLevel::WARNING;
125 return LogLevel::NOTICE;
128 return LogLevel::INFO;
131 return LogLevel::DEBUG;
134 return LogLevel::DEBUG;
135 // default if nothing set
137 return LogLevel::NOTICE;
142 * System is unusable.
143 * @see LoggerInterface::emergency()
145 * @param string $message
146 * @param array $context
151 public static function emergency($message, $context = [])
153 if (!isset(self::$logger)) {
157 $stamp1 = microtime(true);
158 self::$logger->emergency($message, $context);
159 self::getApp()->saveTimestamp($stamp1, 'file');
163 * Action must be taken immediately.
164 * @see LoggerInterface::alert()
166 * Example: Entire website down, database unavailable, etc. This should
167 * trigger the SMS alerts and wake you up.
169 * @param string $message
170 * @param array $context
175 public static function alert($message, $context = [])
177 if (!isset(self::$logger)) {
181 $stamp1 = microtime(true);
182 self::$logger->alert($message, $context);
183 self::getApp()->saveTimestamp($stamp1, 'file');
187 * Critical conditions.
188 * @see LoggerInterface::critical()
190 * Example: Application component unavailable, unexpected exception.
192 * @param string $message
193 * @param array $context
198 public static function critical($message, $context = [])
200 if (!isset(self::$logger)) {
204 $stamp1 = microtime(true);
205 self::$logger->critical($message, $context);
206 self::getApp()->saveTimestamp($stamp1, 'file');
210 * Runtime errors that do not require immediate action but should typically
211 * be logged and monitored.
212 * @see LoggerInterface::error()
214 * @param string $message
215 * @param array $context
220 public static function error($message, $context = [])
222 if (!isset(self::$logger)) {
227 $stamp1 = microtime(true);
228 self::$logger->error($message, $context);
229 self::getApp()->saveTimestamp($stamp1, 'file');
233 * Exceptional occurrences that are not errors.
234 * @see LoggerInterface::warning()
236 * Example: Use of deprecated APIs, poor use of an API, undesirable things
237 * that are not necessarily wrong.
239 * @param string $message
240 * @param array $context
245 public static function warning($message, $context = [])
247 if (!isset(self::$logger)) {
251 $stamp1 = microtime(true);
252 self::$logger->warning($message, $context);
253 self::getApp()->saveTimestamp($stamp1, 'file');
257 * Normal but significant events.
258 * @see LoggerInterface::notice()
260 * @param string $message
261 * @param array $context
266 public static function notice($message, $context = [])
268 if (!isset(self::$logger)) {
272 $stamp1 = microtime(true);
273 self::$logger->notice($message, $context);
274 self::getApp()->saveTimestamp($stamp1, 'file');
278 * Interesting events.
279 * @see LoggerInterface::info()
281 * Example: User logs in, SQL logs.
283 * @param string $message
284 * @param array $context
289 public static function info($message, $context = [])
291 if (!isset(self::$logger)) {
295 $stamp1 = microtime(true);
296 self::$logger->info($message, $context);
297 self::getApp()->saveTimestamp($stamp1, 'file');
301 * Detailed debug information.
302 * @see LoggerInterface::debug()
304 * @param string $message
305 * @param array $context
309 public static function debug($message, $context = [])
311 if (!isset(self::$logger)) {
315 $stamp1 = microtime(true);
316 self::$logger->debug($message, $context);
317 self::getApp()->saveTimestamp($stamp1, 'file');
321 * @brief Logs the given message at the given log level
326 * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
328 public static function log($msg, $level = LogLevel::NOTICE)
330 if (!isset(self::$logger)) {
334 $stamp1 = microtime(true);
335 self::$logger->log($level, $msg);
336 self::getApp()->saveTimestamp($stamp1, "file");
340 * @brief An alternative logger for development.
341 * Works largely as log() but allows developers
342 * to isolate particular elements they are targetting
343 * personally without background noise
346 * @param string $level
348 public static function devLog($msg, $level = LogLevel::DEBUG)
350 if (!isset(self::$logger)) {
354 $stamp1 = microtime(true);
355 self::$devLogger->log($level, $msg);
356 self::getApp()->saveTimestamp($stamp1, "file");