3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core;
25 use Friendica\Core\Logger\Type\WorkerLogger;
26 use Psr\Log\LoggerInterface;
35 * @var LoggerInterface The default Logger type
37 const TYPE_LOGGER = LoggerInterface::class;
39 * @var WorkerLogger A specific worker logger type, which can be anabled
41 const TYPE_WORKER = WorkerLogger::class;
43 * @var LoggerInterface The current logger type
45 private static $type = self::TYPE_LOGGER;
48 * @return LoggerInterface
50 private static function getInstance()
52 if (self::$type === self::TYPE_LOGGER) {
55 return DI::workerLogger();
60 * Enable additional logging for worker usage
62 * @param string $functionName The worker function, which got called
64 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
66 public static function enableWorker(string $functionName)
68 self::$type = self::TYPE_WORKER;
69 self::getInstance()->setFunctionName($functionName);
73 * Disable additional logging for worker usage
75 public static function disableWorker()
77 self::$type = self::TYPE_LOGGER;
83 * @see LoggerInterface::emergency()
85 * @param string $message Message to log
86 * @param array $context Optional variables
90 public static function emergency(string $message, array $context = [])
92 self::getInstance()->emergency($message, $context);
96 * Action must be taken immediately.
97 * @see LoggerInterface::alert()
99 * Example: Entire website down, database unavailable, etc. This should
100 * trigger the SMS alerts and wake you up.
102 * @param string $message Message to log
103 * @param array $context Optional variables
107 public static function alert(string $message, array $context = [])
109 self::getInstance()->alert($message, $context);
113 * Critical conditions.
114 * @see LoggerInterface::critical()
116 * Example: Application component unavailable, unexpected exception.
118 * @param string $message Message to log
119 * @param array $context Optional variables
123 public static function critical(string $message, array $context = [])
125 self::getInstance()->critical($message, $context);
129 * Runtime errors that do not require immediate action but should typically
130 * be logged and monitored.
131 * @see LoggerInterface::error()
133 * @param string $message Message to log
134 * @param array $context Optional variables
138 public static function error(string $message, array $context = [])
140 self::getInstance()->error($message, $context);
144 * Exceptional occurrences that are not errors.
145 * @see LoggerInterface::warning()
147 * Example: Use of deprecated APIs, poor use of an API, undesirable things
148 * that are not necessarily wrong.
150 * @param string $message Message to log
151 * @param array $context Optional variables
155 public static function warning(string $message, array $context = [])
157 self::getInstance()->warning($message, $context);
161 * Normal but significant events.
162 * @see LoggerInterface::notice()
164 * @param string $message Message to log
165 * @param array $context Optional variables
169 public static function notice(string $message, array $context = [])
171 self::getInstance()->notice($message, $context);
175 * Interesting events.
176 * @see LoggerInterface::info()
178 * Example: User logs in, SQL logs.
180 * @param string $message
181 * @param array $context
186 public static function info(string $message, array $context = [])
188 self::getInstance()->info($message, $context);
192 * Detailed debug information.
193 * @see LoggerInterface::debug()
195 * @param string $message Message to log
196 * @param array $context Optional variables
200 public static function debug(string $message, array $context = [])
202 self::getInstance()->debug($message, $context);
206 * An alternative logger for development.
208 * Works largely as log() but allows developers
209 * to isolate particular elements they are targetting
210 * personally without background noise
212 * @param string $message Message to log
213 * @param string $level Logging level
217 public static function devLog(string $message, string $level = LogLevel::DEBUG)
219 DI::devLogger()->log($level, $message);