3 * @copyright Copyright (C) 2010-2023, 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\Logger\Type;
24 use Friendica\Core\Logger\Exception\LoggerException;
25 use Friendica\Util\Strings;
26 use Psr\Log\LoggerInterface;
29 * A Logger for specific worker tasks, which adds a worker id to it.
30 * Uses the decorator pattern (https://en.wikipedia.org/wiki/Decorator_pattern)
32 class WorkerLogger implements LoggerInterface
34 /** @var int Length of the unique worker id */
35 const WORKER_ID_LENGTH = 7;
38 * @var LoggerInterface The original Logger instance
43 * @var string the current worker ID
48 * @var string The called function name
50 private $functionName;
53 * @param LoggerInterface $logger The logger for worker entries
55 * @throws LoggerException
57 public function __construct(LoggerInterface $logger)
59 $this->logger = $logger;
61 $this->workerId = Strings::getRandomHex(self::WORKER_ID_LENGTH);
62 } catch (\Exception $exception) {
63 throw new LoggerException('Cannot generate random Hex.', $exception);
68 * Sets the function name for additional logging
70 * @param string $functionName
72 * @throws LoggerException
74 public function setFunctionName(string $functionName)
76 $this->functionName = $functionName;
78 $this->workerId = Strings::getRandomHex(self::WORKER_ID_LENGTH);
79 } catch (\Exception $exception) {
80 throw new LoggerException('Cannot generate random Hex.', $exception);
85 * Adds the worker context for each log entry
87 * @param array $context
89 private function addContext(array &$context)
91 $context['worker_id'] = $this->workerId;
92 $context['worker_cmd'] = $this->functionName;
96 * Returns the worker ID
100 public function getWorkerId(): string
102 return $this->workerId;
106 * System is unusable.
108 * @param string $message
109 * @param array $context
113 public function emergency($message, array $context = [])
115 $this->addContext($context);
116 $this->logger->emergency($message, $context);
120 * Action must be taken immediately.
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
130 public function alert($message, array $context = [])
132 $this->addContext($context);
133 $this->logger->alert($message, $context);
137 * Critical conditions.
139 * Example: Application component unavailable, unexpected exception.
141 * @param string $message
142 * @param array $context
146 public function critical($message, array $context = [])
148 $this->addContext($context);
149 $this->logger->critical($message, $context);
153 * Runtime errors that do not require immediate action but should typically
154 * be logged and monitored.
156 * @param string $message
157 * @param array $context
161 public function error($message, array $context = [])
163 $this->addContext($context);
164 $this->logger->error($message, $context);
168 * Exceptional occurrences that are not errors.
170 * Example: Use of deprecated APIs, poor use of an API, undesirable things
171 * that are not necessarily wrong.
173 * @param string $message
174 * @param array $context
178 public function warning($message, array $context = [])
180 $this->addContext($context);
181 $this->logger->warning($message, $context);
185 * Normal but significant events.
187 * @param string $message
188 * @param array $context
192 public function notice($message, array $context = [])
194 $this->addContext($context);
195 $this->logger->notice($message, $context);
199 * Interesting events.
201 * Example: User logs in, SQL logs.
203 * @param string $message
204 * @param array $context
208 public function info($message, array $context = [])
210 $this->addContext($context);
211 $this->logger->info($message, $context);
215 * Detailed debug information.
217 * @param string $message
218 * @param array $context
222 public function debug($message, array $context = [])
224 $this->addContext($context);
225 $this->logger->debug($message, $context);
229 * Logs with an arbitrary level.
231 * @param mixed $level
232 * @param string $message
233 * @param array $context
237 public function log($level, $message, array $context = [])
239 $this->addContext($context);
240 $this->logger->log($level, $message, $context);