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',
70 * Enable additional logging for worker usage
72 * @param string $functionName The worker function, which got called
74 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
76 public static function enableWorker(string $functionName)
78 self::$type = self::TYPE_WORKER;
79 self::getClass(self::$type)->setFunctionName($functionName);
83 * Disable additional logging for worker usage
85 public static function disableWorker()
87 self::$type = self::TYPE_LOGGER;
93 * @see LoggerInterface::emergency()
95 * @param string $message
96 * @param array $context
101 public static function emergency($message, $context = [])
103 self::getClass(self::$type)->emergency($message, $context);
107 * Action must be taken immediately.
108 * @see LoggerInterface::alert()
110 * Example: Entire website down, database unavailable, etc. This should
111 * trigger the SMS alerts and wake you up.
113 * @param string $message
114 * @param array $context
119 public static function alert($message, $context = [])
121 self::getClass(self::$type)->alert($message, $context);
125 * Critical conditions.
126 * @see LoggerInterface::critical()
128 * Example: Application component unavailable, unexpected exception.
130 * @param string $message
131 * @param array $context
136 public static function critical($message, $context = [])
138 self::getClass(self::$type)->critical($message, $context);
142 * Runtime errors that do not require immediate action but should typically
143 * be logged and monitored.
144 * @see LoggerInterface::error()
146 * @param string $message
147 * @param array $context
152 public static function error($message, $context = [])
154 self::getClass(self::$type)->error($message, $context);
158 * Exceptional occurrences that are not errors.
159 * @see LoggerInterface::warning()
161 * Example: Use of deprecated APIs, poor use of an API, undesirable things
162 * that are not necessarily wrong.
164 * @param string $message
165 * @param array $context
170 public static function warning($message, $context = [])
172 self::getClass(self::$type)->warning($message, $context);
176 * Normal but significant events.
177 * @see LoggerInterface::notice()
179 * @param string $message
180 * @param array $context
185 public static function notice($message, $context = [])
187 self::getClass(self::$type)->notice($message, $context);
191 * Interesting events.
192 * @see LoggerInterface::info()
194 * Example: User logs in, SQL logs.
196 * @param string $message
197 * @param array $context
202 public static function info($message, $context = [])
204 self::getClass(self::$type)->info($message, $context);
208 * Detailed debug information.
209 * @see LoggerInterface::debug()
211 * @param string $message
212 * @param array $context
217 public static function debug($message, $context = [])
219 self::getClass(self::$type)->debug($message, $context);
223 * @brief Logs the given message at the given log level
226 * @param string $level
229 * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
231 public static function log($msg, $level = LogLevel::INFO)
233 self::getClass(self::$type)->log($level, $msg);
237 * @brief An alternative logger for development.
238 * Works largely as log() but allows developers
239 * to isolate particular elements they are targetting
240 * personally without background noise
243 * @param string $level
246 public static function devLog($msg, $level = LogLevel::DEBUG)
248 self::getClass('$devLogger')->log($level, $msg);