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 $loglevel = self::mapLegacyConfigDebugLevel((string)$loglevel);
88 LoggerFactory::addStreamHandler($logger, $logfile, $loglevel);
90 self::$logger = $logger;
92 $logfile = Config::get('system', 'dlogfile');
98 $developIp = Config::get('system', 'dlogip');
100 self::$devLogger = LoggerFactory::createDev('develop', $developIp);
101 LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
105 * Mapping a legacy level to the PSR-3 compliant levels
106 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
108 * @param string $level the level to be mapped
110 * @return string the PSR-3 compliant level
112 private static function mapLegacyConfigDebugLevel($level)
117 return LogLevel::ERROR;
120 return LogLevel::WARNING;
123 return LogLevel::NOTICE;
126 return LogLevel::INFO;
129 return LogLevel::DEBUG;
132 return LogLevel::DEBUG;
133 // default if nothing set
140 * System is unusable.
142 * @see LoggerInterface::emergency()
144 * @param string $message
145 * @param array $context
150 public static function emergency($message, $context = [])
152 if (!isset(self::$logger)) {
156 $stamp1 = microtime(true);
157 self::$logger->emergency($message, $context);
158 self::getApp()->saveTimestamp($stamp1, 'file');
162 * Action must be taken immediately.
163 * @see LoggerInterface::alert()
165 * Example: Entire website down, database unavailable, etc. This should
166 * trigger the SMS alerts and wake you up.
168 * @param string $message
169 * @param array $context
174 public static function alert($message, $context = [])
176 if (!isset(self::$logger)) {
180 $stamp1 = microtime(true);
181 self::$logger->alert($message, $context);
182 self::getApp()->saveTimestamp($stamp1, 'file');
186 * Critical conditions.
187 * @see LoggerInterface::critical()
189 * Example: Application component unavailable, unexpected exception.
191 * @param string $message
192 * @param array $context
197 public static function critical($message, $context = [])
199 if (!isset(self::$logger)) {
203 $stamp1 = microtime(true);
204 self::$logger->critical($message, $context);
205 self::getApp()->saveTimestamp($stamp1, 'file');
209 * Runtime errors that do not require immediate action but should typically
210 * be logged and monitored.
211 * @see LoggerInterface::error()
213 * @param string $message
214 * @param array $context
219 public static function error($message, $context = [])
221 if (!isset(self::$logger)) {
226 $stamp1 = microtime(true);
227 self::$logger->error($message, $context);
228 self::getApp()->saveTimestamp($stamp1, 'file');
232 * Exceptional occurrences that are not errors.
233 * @see LoggerInterface::warning()
235 * Example: Use of deprecated APIs, poor use of an API, undesirable things
236 * that are not necessarily wrong.
238 * @param string $message
239 * @param array $context
244 public static function warning($message, $context = [])
246 if (!isset(self::$logger)) {
250 $stamp1 = microtime(true);
251 self::$logger->warning($message, $context);
252 self::getApp()->saveTimestamp($stamp1, 'file');
256 * Normal but significant events.
257 * @see LoggerInterface::notice()
259 * @param string $message
260 * @param array $context
265 public static function notice($message, $context = [])
267 if (!isset(self::$logger)) {
271 $stamp1 = microtime(true);
272 self::$logger->notice($message, $context);
273 self::getApp()->saveTimestamp($stamp1, 'file');
277 * Interesting events.
278 * @see LoggerInterface::info()
280 * Example: User logs in, SQL logs.
282 * @param string $message
283 * @param array $context
288 public static function info($message, $context = [])
290 if (!isset(self::$logger)) {
294 $stamp1 = microtime(true);
295 self::$logger->info($message, $context);
296 self::getApp()->saveTimestamp($stamp1, 'file');
300 * Detailed debug information.
301 * @see LoggerInterface::debug()
303 * @param string $message
304 * @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
324 * @param string $level
327 * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
329 public static function log($msg, $level = LogLevel::INFO)
331 if (!isset(self::$logger)) {
335 $stamp1 = microtime(true);
336 self::$logger->log($level, $msg);
337 self::getApp()->saveTimestamp($stamp1, "file");
341 * @brief An alternative logger for development.
342 * Works largely as log() but allows developers
343 * to isolate particular elements they are targetting
344 * personally without background noise
347 * @param string $level
350 public static function devLog($msg, $level = LogLevel::DEBUG)
352 if (!isset(self::$logger)) {
356 $stamp1 = microtime(true);
357 self::$devLogger->log($level, $msg);
358 self::getApp()->saveTimestamp($stamp1, "file");