3 * @file src/Core/Logger.php
5 namespace Friendica\Core;
7 use Friendica\BaseObject;
8 use Friendica\Util\Logger\WorkerLogger;
9 use Psr\Log\LoggerInterface;
13 * @brief Logger functions
15 class Logger extends BaseObject
18 * @see Logger::error()
20 const WARNING = LogLevel::ERROR;
22 * @see Logger::warning()
24 const INFO = LogLevel::WARNING;
26 * @see Logger::notice()
28 const TRACE = LogLevel::NOTICE;
32 const DEBUG = LogLevel::INFO;
34 * @see Logger::debug()
36 const DATA = LogLevel::DEBUG;
38 * @see Logger::debug()
40 const ALL = LogLevel::DEBUG;
43 * @var LoggerInterface The default Logger type
45 const TYPE_LOGGER = LoggerInterface::class;
47 * @var WorkerLogger A specific worker logger type, which can be anabled
49 const TYPE_WORKER = WorkerLogger::class;
51 * @var LoggerInterface The current logger type
53 private static $type = self::TYPE_LOGGER;
56 * @var array the legacy loglevels
57 * @deprecated 2019.03 use PSR-3 loglevels
58 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
61 public static $levels = [
62 self::WARNING => 'Warning',
64 self::TRACE => 'Trace',
65 self::DEBUG => 'Debug',
71 * Enable additional logging for worker usage
73 * @param string $functionName The worker function, which got called
75 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
77 public static function enableWorker(string $functionName)
79 self::$type = self::TYPE_WORKER;
80 self::getClass(self::$type)->setFunctionName($functionName);
84 * Disable additional logging for worker usage
86 public static function disableWorker()
88 self::$type = self::TYPE_LOGGER;
94 * @see LoggerInterface::emergency()
96 * @param string $message
97 * @param array $context
102 public static function emergency($message, $context = [])
104 self::getClass(self::$type)->emergency($message, $context);
108 * Action must be taken immediately.
109 * @see LoggerInterface::alert()
111 * Example: Entire website down, database unavailable, etc. This should
112 * trigger the SMS alerts and wake you up.
114 * @param string $message
115 * @param array $context
120 public static function alert($message, $context = [])
122 self::getClass(self::$type)->alert($message, $context);
126 * Critical conditions.
127 * @see LoggerInterface::critical()
129 * Example: Application component unavailable, unexpected exception.
131 * @param string $message
132 * @param array $context
137 public static function critical($message, $context = [])
139 self::getClass(self::$type)->critical($message, $context);
143 * Runtime errors that do not require immediate action but should typically
144 * be logged and monitored.
145 * @see LoggerInterface::error()
147 * @param string $message
148 * @param array $context
153 public static function error($message, $context = [])
155 self::getClass(self::$type)->error($message, $context);
159 * Exceptional occurrences that are not errors.
160 * @see LoggerInterface::warning()
162 * Example: Use of deprecated APIs, poor use of an API, undesirable things
163 * that are not necessarily wrong.
165 * @param string $message
166 * @param array $context
171 public static function warning($message, $context = [])
173 self::getClass(self::$type)->warning($message, $context);
177 * Normal but significant events.
178 * @see LoggerInterface::notice()
180 * @param string $message
181 * @param array $context
186 public static function notice($message, $context = [])
188 self::getClass(self::$type)->notice($message, $context);
192 * Interesting events.
193 * @see LoggerInterface::info()
195 * Example: User logs in, SQL logs.
197 * @param string $message
198 * @param array $context
203 public static function info($message, $context = [])
205 self::getClass(self::$type)->info($message, $context);
209 * Detailed debug information.
210 * @see LoggerInterface::debug()
212 * @param string $message
213 * @param array $context
218 public static function debug($message, $context = [])
220 self::getClass(self::$type)->debug($message, $context);
224 * @brief Logs the given message at the given log level
227 * @param string $level
230 * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
232 public static function log($msg, $level = LogLevel::INFO)
234 self::getClass(self::$type)->log($level, $msg);
238 * @brief An alternative logger for development.
239 * Works largely as log() but allows developers
240 * to isolate particular elements they are targetting
241 * personally without background noise
244 * @param string $level
247 public static function devLog($msg, $level = LogLevel::DEBUG)
249 self::getClass('$devLogger')->log($level, $msg);