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 = intval(Config::get('system', 'loglevel'));
82 if (!$debugging || !$logfile) {
86 $level = self::mapPSR3Level($loglevel);
87 LoggerFactory::addStreamHandler($logger, $logfile, $level);
89 self::$logger = $logger;
91 $logfile = Config::get('system', 'dlogfile');
97 $developIp = Config::get('system', 'dlogip');
99 self::$devLogger = LoggerFactory::createDev('develop', $developIp);
100 LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
104 * System is unusable.
105 * @see LoggerInterface::emergency()
107 * @param string $message
108 * @param array $context
113 public static function emergency($message, $context = [])
115 if (!isset(self::$logger)) {
119 $stamp1 = microtime(true);
120 self::$logger->emergency($message, $context);
121 self::getApp()->saveTimestamp($stamp1, 'file');
125 * Action must be taken immediately.
126 * @see LoggerInterface::alert()
128 * Example: Entire website down, database unavailable, etc. This should
129 * trigger the SMS alerts and wake you up.
131 * @param string $message
132 * @param array $context
137 public static function alert($message, $context = [])
139 if (!isset(self::$logger)) {
143 $stamp1 = microtime(true);
144 self::$logger->alert($message, $context);
145 self::getApp()->saveTimestamp($stamp1, 'file');
149 * Critical conditions.
150 * @see LoggerInterface::critical()
152 * Example: Application component unavailable, unexpected exception.
154 * @param string $message
155 * @param array $context
160 public static function critical($message, $context = [])
162 if (!isset(self::$logger)) {
166 $stamp1 = microtime(true);
167 self::$logger->critical($message, $context);
168 self::getApp()->saveTimestamp($stamp1, 'file');
172 * Runtime errors that do not require immediate action but should typically
173 * be logged and monitored.
174 * @see LoggerInterface::error()
176 * @param string $message
177 * @param array $context
182 public static function error($message, $context = [])
184 if (!isset(self::$logger)) {
189 $stamp1 = microtime(true);
190 self::$logger->error($message, $context);
191 self::getApp()->saveTimestamp($stamp1, 'file');
195 * Exceptional occurrences that are not errors.
196 * @see LoggerInterface::warning()
198 * Example: Use of deprecated APIs, poor use of an API, undesirable things
199 * that are not necessarily wrong.
201 * @param string $message
202 * @param array $context
207 public static function warning($message, $context = [])
209 if (!isset(self::$logger)) {
213 $stamp1 = microtime(true);
214 self::$logger->warning($message, $context);
215 self::getApp()->saveTimestamp($stamp1, 'file');
219 * Normal but significant events.
220 * @see LoggerInterface::notice()
222 * @param string $message
223 * @param array $context
228 public static function notice($message, $context = [])
230 if (!isset(self::$logger)) {
234 $stamp1 = microtime(true);
235 self::$logger->notice($message, $context);
236 self::getApp()->saveTimestamp($stamp1, 'file');
240 * Interesting events.
241 * @see LoggerInterface::info()
243 * Example: User logs in, SQL logs.
245 * @param string $message
246 * @param array $context
251 public static function info($message, $context = [])
253 if (!isset(self::$logger)) {
257 $stamp1 = microtime(true);
258 self::$logger->info($message, $context);
259 self::getApp()->saveTimestamp($stamp1, 'file');
263 * Detailed debug information.
264 * @see LoggerInterface::debug()
266 * @param string $message
267 * @param array $context
271 public static function debug($message, $context = [])
273 if (!isset(self::$logger)) {
277 $stamp1 = microtime(true);
278 self::$logger->debug($message, $context);
279 self::getApp()->saveTimestamp($stamp1, 'file');
283 * @brief Logs the given message at the given log level
288 * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
290 public static function log($msg, $level = LogLevel::NOTICE)
292 if (!isset(self::$logger)) {
296 $stamp1 = microtime(true);
297 self::$logger->log($level, $msg);
298 self::getApp()->saveTimestamp($stamp1, "file");
302 * @brief An alternative logger for development.
303 * Works largely as log() but allows developers
304 * to isolate particular elements they are targetting
305 * personally without background noise
308 * @param string $level
310 public static function devLog($msg, $level = LogLevel::DEBUG)
312 if (!isset(self::$logger)) {
316 $stamp1 = microtime(true);
317 self::$devLogger->log($level, $msg);
318 self::getApp()->saveTimestamp($stamp1, "file");