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 getWorker()
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::getWorker()->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
86 * @param array $context
91 public static function emergency($message, $context = [])
93 self::getWorker()->emergency($message, $context);
97 * Action must be taken immediately.
98 * @see LoggerInterface::alert()
100 * Example: Entire website down, database unavailable, etc. This should
101 * trigger the SMS alerts and wake you up.
103 * @param string $message
104 * @param array $context
109 public static function alert($message, $context = [])
111 self::getWorker()->alert($message, $context);
115 * Critical conditions.
116 * @see LoggerInterface::critical()
118 * Example: Application component unavailable, unexpected exception.
120 * @param string $message
121 * @param array $context
126 public static function critical($message, $context = [])
128 self::getWorker()->critical($message, $context);
132 * Runtime errors that do not require immediate action but should typically
133 * be logged and monitored.
134 * @see LoggerInterface::error()
136 * @param string $message
137 * @param array $context
142 public static function error($message, $context = [])
144 self::getWorker()->error($message, $context);
148 * Exceptional occurrences that are not errors.
149 * @see LoggerInterface::warning()
151 * Example: Use of deprecated APIs, poor use of an API, undesirable things
152 * that are not necessarily wrong.
154 * @param string $message
155 * @param array $context
160 public static function warning($message, $context = [])
162 self::getWorker()->warning($message, $context);
166 * Normal but significant events.
167 * @see LoggerInterface::notice()
169 * @param string $message
170 * @param array $context
175 public static function notice($message, $context = [])
177 self::getWorker()->notice($message, $context);
181 * Interesting events.
182 * @see LoggerInterface::info()
184 * Example: User logs in, SQL logs.
186 * @param string $message
187 * @param array $context
192 public static function info($message, $context = [])
194 self::getWorker()->info($message, $context);
198 * Detailed debug information.
199 * @see LoggerInterface::debug()
201 * @param string $message
202 * @param array $context
207 public static function debug($message, $context = [])
209 self::getWorker()->debug($message, $context);
213 * An alternative logger for development.
215 * Works largely as log() but allows developers
216 * to isolate particular elements they are targetting
217 * personally without background noise
220 * @param string $level
223 public static function devLog($msg, $level = LogLevel::DEBUG)
225 DI::devLogger()->log($level, $msg);