3 * @file src/Core/Logger.php
5 namespace Friendica\Core;
8 use Friendica\Util\Logger\WorkerLogger;
9 use Psr\Log\LoggerInterface;
13 * @brief Logger functions
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',
70 * @return LoggerInterface
72 private static function getWorker()
74 if (self::$type === self::TYPE_LOGGER) {
77 return DI::workerLogger();
82 * Enable additional logging for worker usage
84 * @param string $functionName The worker function, which got called
86 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
88 public static function enableWorker(string $functionName)
90 self::$type = self::TYPE_WORKER;
91 self::getWorker()->setFunctionName($functionName);
95 * Disable additional logging for worker usage
97 public static function disableWorker()
99 self::$type = self::TYPE_LOGGER;
103 * System is unusable.
105 * @see LoggerInterface::emergency()
107 * @param string $message
108 * @param array $context
113 public static function emergency($message, $context = [])
115 self::getWorker()->emergency($message, $context);
119 * Action must be taken immediately.
120 * @see LoggerInterface::alert()
122 * Example: Entire website down, database unavailable, etc. This should
123 * trigger the SMS alerts and wake you up.
125 * @param string $message
126 * @param array $context
131 public static function alert($message, $context = [])
133 self::getWorker()->alert($message, $context);
137 * Critical conditions.
138 * @see LoggerInterface::critical()
140 * Example: Application component unavailable, unexpected exception.
142 * @param string $message
143 * @param array $context
148 public static function critical($message, $context = [])
150 self::getWorker()->critical($message, $context);
154 * Runtime errors that do not require immediate action but should typically
155 * be logged and monitored.
156 * @see LoggerInterface::error()
158 * @param string $message
159 * @param array $context
164 public static function error($message, $context = [])
166 self::getWorker()->error($message, $context);
170 * Exceptional occurrences that are not errors.
171 * @see LoggerInterface::warning()
173 * Example: Use of deprecated APIs, poor use of an API, undesirable things
174 * that are not necessarily wrong.
176 * @param string $message
177 * @param array $context
182 public static function warning($message, $context = [])
184 self::getWorker()->warning($message, $context);
188 * Normal but significant events.
189 * @see LoggerInterface::notice()
191 * @param string $message
192 * @param array $context
197 public static function notice($message, $context = [])
199 self::getWorker()->notice($message, $context);
203 * Interesting events.
204 * @see LoggerInterface::info()
206 * Example: User logs in, SQL logs.
208 * @param string $message
209 * @param array $context
214 public static function info($message, $context = [])
216 self::getWorker()->info($message, $context);
220 * Detailed debug information.
221 * @see LoggerInterface::debug()
223 * @param string $message
224 * @param array $context
229 public static function debug($message, $context = [])
231 self::getWorker()->debug($message, $context);
235 * @brief Logs the given message at the given log level
238 * @param string $level
241 * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
243 public static function log($msg, $level = LogLevel::INFO)
245 self::getWorker()->log($level, $msg);
249 * @brief An alternative logger for development.
250 * Works largely as log() but allows developers
251 * to isolate particular elements they are targetting
252 * personally without background noise
255 * @param string $level
258 public static function devLog($msg, $level = LogLevel::DEBUG)
260 DI::devLogger()->log($level, $msg);