3 * @copyright Copyright (C) 2020, Friendica
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\Util\Logger;
24 use Friendica\Util\Strings;
25 use Psr\Log\LoggerInterface;
28 * A Logger for specific worker tasks, which adds an additional woker-id to it.
29 * Uses the decorator pattern (https://en.wikipedia.org/wiki/Decorator_pattern)
31 class WorkerLogger implements LoggerInterface
34 * @var LoggerInterface The original Logger instance
39 * @var string the current worker ID
44 * @var string The called function name
46 private $functionName;
49 * @param LoggerInterface $logger The logger for worker entries
50 * @param string $functionName The current function name of the worker
51 * @param int $idLength The length of the generated worker ID
53 public function __construct(LoggerInterface $logger, $functionName = '', $idLength = 7)
55 $this->logger = $logger;
56 $this->functionName = $functionName;
57 $this->workerId = Strings::getRandomHex($idLength);
61 * Sets the function name for additional logging
63 * @param string $functionName
65 public function setFunctionName(string $functionName)
67 $this->functionName = $functionName;
71 * Adds the worker context for each log entry
73 * @param array $context
75 private function addContext(array &$context)
77 $context['worker_id'] = $this->workerId;
78 $context['worker_cmd'] = $this->functionName;
82 * Returns the worker ID
86 public function getWorkerId()
88 return $this->workerId;
94 * @param string $message
95 * @param array $context
99 public function emergency($message, array $context = [])
101 $this->addContext($context);
102 $this->logger->emergency($message, $context);
106 * Action must be taken immediately.
108 * Example: Entire website down, database unavailable, etc. This should
109 * trigger the SMS alerts and wake you up.
111 * @param string $message
112 * @param array $context
116 public function alert($message, array $context = [])
118 $this->addContext($context);
119 $this->logger->alert($message, $context);
123 * Critical conditions.
125 * Example: Application component unavailable, unexpected exception.
127 * @param string $message
128 * @param array $context
132 public function critical($message, array $context = [])
134 $this->addContext($context);
135 $this->logger->critical($message, $context);
139 * Runtime errors that do not require immediate action but should typically
140 * be logged and monitored.
142 * @param string $message
143 * @param array $context
147 public function error($message, array $context = [])
149 $this->addContext($context);
150 $this->logger->error($message, $context);
154 * Exceptional occurrences that are not errors.
156 * Example: Use of deprecated APIs, poor use of an API, undesirable things
157 * that are not necessarily wrong.
159 * @param string $message
160 * @param array $context
164 public function warning($message, array $context = [])
166 $this->addContext($context);
167 $this->logger->warning($message, $context);
171 * Normal but significant events.
173 * @param string $message
174 * @param array $context
178 public function notice($message, array $context = [])
180 $this->addContext($context);
181 $this->logger->notice($message, $context);
185 * Interesting events.
187 * Example: User logs in, SQL logs.
189 * @param string $message
190 * @param array $context
194 public function info($message, array $context = [])
196 $this->addContext($context);
197 $this->logger->info($message, $context);
201 * Detailed debug information.
203 * @param string $message
204 * @param array $context
208 public function debug($message, array $context = [])
210 $this->addContext($context);
211 $this->logger->debug($message, $context);
215 * Logs with an arbitrary level.
217 * @param mixed $level
218 * @param string $message
219 * @param array $context
223 public function log($level, $message, array $context = [])
225 $this->addContext($context);
226 $this->logger->log($level, $message, $context);