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 * Adds the worker context for each log entry
44 * @param array $context
46 private function addContext(array &$context)
48 $context['worker_id'] = $this->workerId;
49 $context['worker_cmd'] = $this->functionName;
53 * Returns the worker ID
57 public function getWorkerId()
59 return $this->workerId;
65 * @param string $message
66 * @param array $context
70 public function emergency($message, array $context = [])
72 $this->addContext($context);
73 $this->logger->emergency($message, $context);
77 * Action must be taken immediately.
79 * Example: Entire website down, database unavailable, etc. This should
80 * trigger the SMS alerts and wake you up.
82 * @param string $message
83 * @param array $context
87 public function alert($message, array $context = [])
89 $this->addContext($context);
90 $this->logger->alert($message, $context);
94 * Critical conditions.
96 * Example: Application component unavailable, unexpected exception.
98 * @param string $message
99 * @param array $context
103 public function critical($message, array $context = [])
105 $this->addContext($context);
106 $this->logger->critical($message, $context);
110 * Runtime errors that do not require immediate action but should typically
111 * be logged and monitored.
113 * @param string $message
114 * @param array $context
118 public function error($message, array $context = [])
120 $this->addContext($context);
121 $this->logger->error($message, $context);
125 * Exceptional occurrences that are not errors.
127 * Example: Use of deprecated APIs, poor use of an API, undesirable things
128 * that are not necessarily wrong.
130 * @param string $message
131 * @param array $context
135 public function warning($message, array $context = [])
137 $this->addContext($context);
138 $this->logger->warning($message, $context);
142 * Normal but significant events.
144 * @param string $message
145 * @param array $context
149 public function notice($message, array $context = [])
151 $this->addContext($context);
152 $this->logger->notice($message, $context);
156 * Interesting events.
158 * Example: User logs in, SQL logs.
160 * @param string $message
161 * @param array $context
165 public function info($message, array $context = [])
167 $this->addContext($context);
168 $this->logger->info($message, $context);
172 * Detailed debug information.
174 * @param string $message
175 * @param array $context
179 public function debug($message, array $context = [])
181 $this->addContext($context);
182 $this->logger->debug($message, $context);
186 * Logs with an arbitrary level.
188 * @param mixed $level
189 * @param string $message
190 * @param array $context
194 public function log($level, $message, array $context = [])
196 $this->addContext($context);
197 $this->logger->log($level, $message, $context);