3 namespace Friendica\Util\Logger;
5 use Friendica\Util\Strings;
6 use Psr\Log\LoggerInterface;
9 * A Logger for specific worker tasks, which adds an additional woker-id to it.
10 * Uses the decorator pattern (https://en.wikipedia.org/wiki/Decorator_pattern)
12 class WorkerLogger implements LoggerInterface
15 * @var LoggerInterface The original Logger instance
20 * @var string the current worker ID
25 * @var string The called function name
27 private $functionName;
30 * @param LoggerInterface $logger The logger for worker entries
31 * @param string $functionName The current function name of the worker
32 * @param int $idLength The length of the generated worker ID
34 public function __construct(LoggerInterface $logger, $functionName = '', $idLength = 7)
36 $this->logger = $logger;
37 $this->functionName = $functionName;
38 $this->workerId = Strings::getRandomHex($idLength);
42 * Sets the function name for additional logging
44 * @param string $functionName
46 public function setFunctionName(string $functionName)
48 $this->functionName = $functionName;
52 * Adds the worker context for each log entry
54 * @param array $context
56 private function addContext(array &$context)
58 $context['worker_id'] = $this->workerId;
59 $context['worker_cmd'] = $this->functionName;
63 * Returns the worker ID
67 public function getWorkerId()
69 return $this->workerId;
75 * @param string $message
76 * @param array $context
80 public function emergency($message, array $context = [])
82 $this->addContext($context);
83 $this->logger->emergency($message, $context);
87 * Action must be taken immediately.
89 * Example: Entire website down, database unavailable, etc. This should
90 * trigger the SMS alerts and wake you up.
92 * @param string $message
93 * @param array $context
97 public function alert($message, array $context = [])
99 $this->addContext($context);
100 $this->logger->alert($message, $context);
104 * Critical conditions.
106 * Example: Application component unavailable, unexpected exception.
108 * @param string $message
109 * @param array $context
113 public function critical($message, array $context = [])
115 $this->addContext($context);
116 $this->logger->critical($message, $context);
120 * Runtime errors that do not require immediate action but should typically
121 * be logged and monitored.
123 * @param string $message
124 * @param array $context
128 public function error($message, array $context = [])
130 $this->addContext($context);
131 $this->logger->error($message, $context);
135 * Exceptional occurrences that are not errors.
137 * Example: Use of deprecated APIs, poor use of an API, undesirable things
138 * that are not necessarily wrong.
140 * @param string $message
141 * @param array $context
145 public function warning($message, array $context = [])
147 $this->addContext($context);
148 $this->logger->warning($message, $context);
152 * Normal but significant events.
154 * @param string $message
155 * @param array $context
159 public function notice($message, array $context = [])
161 $this->addContext($context);
162 $this->logger->notice($message, $context);
166 * Interesting events.
168 * Example: User logs in, SQL logs.
170 * @param string $message
171 * @param array $context
175 public function info($message, array $context = [])
177 $this->addContext($context);
178 $this->logger->info($message, $context);
182 * Detailed debug information.
184 * @param string $message
185 * @param array $context
189 public function debug($message, array $context = [])
191 $this->addContext($context);
192 $this->logger->debug($message, $context);
196 * Logs with an arbitrary level.
198 * @param mixed $level
199 * @param string $message
200 * @param array $context
204 public function log($level, $message, array $context = [])
206 $this->addContext($context);
207 $this->logger->log($level, $message, $context);