3 * @copyright Copyright (C) 2010-2021, 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
35 * @var LoggerInterface The original Logger instance
40 * @var string the current worker ID
45 * @var string The called function name
47 private $functionName;
50 * @param LoggerInterface $logger The logger for worker entries
51 * @param string $functionName The current function name of the worker
52 * @param int $idLength The length of the generated worker ID
54 * @throws LoggerException
56 public function __construct(LoggerInterface $logger, string $functionName = '', int $idLength = 7)
58 $this->logger = $logger;
59 $this->functionName = $functionName;
61 $this->workerId = Strings::getRandomHex($idLength);
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 public function setFunctionName(string $functionName)
74 $this->functionName = $functionName;
78 * Adds the worker context for each log entry
80 * @param array $context
82 private function addContext(array &$context)
84 $context['worker_id'] = $this->workerId;
85 $context['worker_cmd'] = $this->functionName;
89 * Returns the worker ID
93 public function getWorkerId(): string
95 return $this->workerId;
101 * @param string $message
102 * @param array $context
106 public function emergency($message, array $context = [])
108 $this->addContext($context);
109 $this->logger->emergency($message, $context);
113 * Action must be taken immediately.
115 * Example: Entire website down, database unavailable, etc. This should
116 * trigger the SMS alerts and wake you up.
118 * @param string $message
119 * @param array $context
123 public function alert($message, array $context = [])
125 $this->addContext($context);
126 $this->logger->alert($message, $context);
130 * Critical conditions.
132 * Example: Application component unavailable, unexpected exception.
134 * @param string $message
135 * @param array $context
139 public function critical($message, array $context = [])
141 $this->addContext($context);
142 $this->logger->critical($message, $context);
146 * Runtime errors that do not require immediate action but should typically
147 * be logged and monitored.
149 * @param string $message
150 * @param array $context
154 public function error($message, array $context = [])
156 $this->addContext($context);
157 $this->logger->error($message, $context);
161 * Exceptional occurrences that are not errors.
163 * Example: Use of deprecated APIs, poor use of an API, undesirable things
164 * that are not necessarily wrong.
166 * @param string $message
167 * @param array $context
171 public function warning($message, array $context = [])
173 $this->addContext($context);
174 $this->logger->warning($message, $context);
178 * Normal but significant events.
180 * @param string $message
181 * @param array $context
185 public function notice($message, array $context = [])
187 $this->addContext($context);
188 $this->logger->notice($message, $context);
192 * Interesting events.
194 * Example: User logs in, SQL logs.
196 * @param string $message
197 * @param array $context
201 public function info($message, array $context = [])
203 $this->addContext($context);
204 $this->logger->info($message, $context);
208 * Detailed debug information.
210 * @param string $message
211 * @param array $context
215 public function debug($message, array $context = [])
217 $this->addContext($context);
218 $this->logger->debug($message, $context);
222 * Logs with an arbitrary level.
224 * @param mixed $level
225 * @param string $message
226 * @param array $context
230 public function log($level, $message, array $context = [])
232 $this->addContext($context);
233 $this->logger->log($level, $message, $context);