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::mapLegacyDebugLevel($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 mapLegacyDebugLevel($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.
144 * @see LoggerInterface::emergency()
146 * @param string $message
147 * @param array $context
152 public static function emergency($message, $context = [])
154 if (!isset(self::$logger)) {
158 $stamp1 = microtime(true);
159 self::$logger->emergency($message, $context);
160 self::getApp()->saveTimestamp($stamp1, 'file');
164 * Action must be taken immediately.
165 * @see LoggerInterface::alert()
167 * Example: Entire website down, database unavailable, etc. This should
168 * trigger the SMS alerts and wake you up.
170 * @param string $message
171 * @param array $context
176 public static function alert($message, $context = [])
178 if (!isset(self::$logger)) {
182 $stamp1 = microtime(true);
183 self::$logger->alert($message, $context);
184 self::getApp()->saveTimestamp($stamp1, 'file');
188 * Critical conditions.
189 * @see LoggerInterface::critical()
191 * Example: Application component unavailable, unexpected exception.
193 * @param string $message
194 * @param array $context
199 public static function critical($message, $context = [])
201 if (!isset(self::$logger)) {
205 $stamp1 = microtime(true);
206 self::$logger->critical($message, $context);
207 self::getApp()->saveTimestamp($stamp1, 'file');
211 * Runtime errors that do not require immediate action but should typically
212 * be logged and monitored.
213 * @see LoggerInterface::error()
215 * @param string $message
216 * @param array $context
221 public static function error($message, $context = [])
223 if (!isset(self::$logger)) {
228 $stamp1 = microtime(true);
229 self::$logger->error($message, $context);
230 self::getApp()->saveTimestamp($stamp1, 'file');
234 * Exceptional occurrences that are not errors.
235 * @see LoggerInterface::warning()
237 * Example: Use of deprecated APIs, poor use of an API, undesirable things
238 * that are not necessarily wrong.
240 * @param string $message
241 * @param array $context
246 public static function warning($message, $context = [])
248 if (!isset(self::$logger)) {
252 $stamp1 = microtime(true);
253 self::$logger->warning($message, $context);
254 self::getApp()->saveTimestamp($stamp1, 'file');
258 * Normal but significant events.
259 * @see LoggerInterface::notice()
261 * @param string $message
262 * @param array $context
267 public static function notice($message, $context = [])
269 if (!isset(self::$logger)) {
273 $stamp1 = microtime(true);
274 self::$logger->notice($message, $context);
275 self::getApp()->saveTimestamp($stamp1, 'file');
279 * Interesting events.
280 * @see LoggerInterface::info()
282 * Example: User logs in, SQL logs.
284 * @param string $message
285 * @param array $context
290 public static function info($message, $context = [])
292 if (!isset(self::$logger)) {
296 $stamp1 = microtime(true);
297 self::$logger->info($message, $context);
298 self::getApp()->saveTimestamp($stamp1, 'file');
302 * Detailed debug information.
303 * @see LoggerInterface::debug()
305 * @param string $message
306 * @param array $context
311 public static function debug($message, $context = [])
313 if (!isset(self::$logger)) {
317 $stamp1 = microtime(true);
318 self::$logger->debug($message, $context);
319 self::getApp()->saveTimestamp($stamp1, 'file');
323 * @brief Logs the given message at the given log level
329 * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
331 public static function log($msg, $level = 3)
333 if (!isset(self::$logger)) {
337 $loglevel = self::mapLegacyDebugLevel($level);
339 $stamp1 = microtime(true);
340 self::$logger->log($loglevel, $msg);
341 self::getApp()->saveTimestamp($stamp1, "file");
345 * @brief An alternative logger for development.
346 * Works largely as log() but allows developers
347 * to isolate particular elements they are targetting
348 * personally without background noise
351 * @param string $level
354 public static function devLog($msg, $level = LogLevel::DEBUG)
356 if (!isset(self::$logger)) {
360 $stamp1 = microtime(true);
361 self::$devLogger->log($level, $msg);
362 self::getApp()->saveTimestamp($stamp1, "file");