3 * @file src/Core/Logger.php
5 namespace Friendica\Core;
7 use Friendica\BaseObject;
8 use Psr\Log\LoggerInterface;
12 * @brief Logger functions
14 class Logger extends BaseObject
17 * @see Logger::error()
19 const WARNING = LogLevel::ERROR;
21 * @see Logger::warning()
23 const INFO = LogLevel::WARNING;
25 * @see Logger::notice()
27 const TRACE = LogLevel::NOTICE;
31 const DEBUG = LogLevel::INFO;
33 * @see Logger::debug()
35 const DATA = LogLevel::DEBUG;
37 * @see Logger::debug()
39 const ALL = LogLevel::DEBUG;
42 * @var array the legacy loglevels
43 * @deprecated 2019.03 use PSR-3 loglevels
44 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
47 public static $levels = [
48 self::WARNING => 'Warning',
50 self::TRACE => 'Trace',
51 self::DEBUG => 'Debug',
57 * @var LoggerInterface A PSR-3 compliant logger instance
59 private static $logger;
62 * @var LoggerInterface A PSR-3 compliant logger instance for developing only
64 private static $devLogger;
67 * Sets the default logging handler for Friendica.
69 * @param LoggerInterface $logger The Logger instance of this Application
71 public static function init($logger)
73 self::$logger = $logger;
77 * Sets the default dev-logging handler for Friendica.
79 * @param LoggerInterface $logger The Logger instance of this Application
81 public static function setDevLogger($logger)
83 self::$devLogger = $logger;
89 * @see LoggerInterface::emergency()
91 * @param string $message
92 * @param array $context
97 public static function emergency($message, $context = [])
99 if (!isset(self::$logger)) {
103 $stamp1 = microtime(true);
104 self::$logger->emergency($message, $context);
105 self::getApp()->GetProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
109 * Action must be taken immediately.
110 * @see LoggerInterface::alert()
112 * Example: Entire website down, database unavailable, etc. This should
113 * trigger the SMS alerts and wake you up.
115 * @param string $message
116 * @param array $context
121 public static function alert($message, $context = [])
123 if (!isset(self::$logger)) {
127 $stamp1 = microtime(true);
128 self::$logger->alert($message, $context);
129 self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
133 * Critical conditions.
134 * @see LoggerInterface::critical()
136 * Example: Application component unavailable, unexpected exception.
138 * @param string $message
139 * @param array $context
144 public static function critical($message, $context = [])
146 if (!isset(self::$logger)) {
150 $stamp1 = microtime(true);
151 self::$logger->critical($message, $context);
152 self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
156 * Runtime errors that do not require immediate action but should typically
157 * be logged and monitored.
158 * @see LoggerInterface::error()
160 * @param string $message
161 * @param array $context
166 public static function error($message, $context = [])
168 if (!isset(self::$logger)) {
173 $stamp1 = microtime(true);
174 self::$logger->error($message, $context);
175 self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
179 * Exceptional occurrences that are not errors.
180 * @see LoggerInterface::warning()
182 * Example: Use of deprecated APIs, poor use of an API, undesirable things
183 * that are not necessarily wrong.
185 * @param string $message
186 * @param array $context
191 public static function warning($message, $context = [])
193 if (!isset(self::$logger)) {
197 $stamp1 = microtime(true);
198 self::$logger->warning($message, $context);
199 self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
203 * Normal but significant events.
204 * @see LoggerInterface::notice()
206 * @param string $message
207 * @param array $context
212 public static function notice($message, $context = [])
214 if (!isset(self::$logger)) {
218 $stamp1 = microtime(true);
219 self::$logger->notice($message, $context);
220 self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
224 * Interesting events.
225 * @see LoggerInterface::info()
227 * Example: User logs in, SQL logs.
229 * @param string $message
230 * @param array $context
235 public static function info($message, $context = [])
237 if (!isset(self::$logger)) {
241 $stamp1 = microtime(true);
242 self::$logger->info($message, $context);
243 self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
247 * Detailed debug information.
248 * @see LoggerInterface::debug()
250 * @param string $message
251 * @param array $context
256 public static function debug($message, $context = [])
258 if (!isset(self::$logger)) {
262 $stamp1 = microtime(true);
263 self::$logger->debug($message, $context);
264 self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
268 * @brief Logs the given message at the given log level
271 * @param string $level
274 * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
276 public static function log($msg, $level = LogLevel::INFO)
278 if (!isset(self::$logger)) {
282 $stamp1 = microtime(true);
283 self::$logger->log($level, $msg);
284 self::getApp()->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
288 * @brief An alternative logger for development.
289 * Works largely as log() but allows developers
290 * to isolate particular elements they are targetting
291 * personally without background noise
294 * @param string $level
297 public static function devLog($msg, $level = LogLevel::DEBUG)
299 if (!isset(self::$logger)) {
303 $stamp1 = microtime(true);
304 self::$devLogger->log($level, $msg);
305 self::getApp()->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());