]> git.mxchange.org Git - friendica.git/blob - src/Util/Logger/WorkerLogger.php
wrapping up 2019.12
[friendica.git] / src / Util / Logger / WorkerLogger.php
1 <?php
2
3 namespace Friendica\Util\Logger;
4
5 use Friendica\Util\Strings;
6 use Psr\Log\LoggerInterface;
7
8 /**
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)
11  */
12 class WorkerLogger implements LoggerInterface
13 {
14         /**
15          * @var LoggerInterface The original Logger instance
16          */
17         private $logger;
18
19         /**
20          * @var string the current worker ID
21          */
22         private $workerId;
23
24         /**
25          * @var string The called function name
26          */
27         private $functionName;
28
29         /**
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
33          */
34         public function __construct(LoggerInterface $logger, $functionName = '', $idLength = 7)
35         {
36                 $this->logger = $logger;
37                 $this->functionName = $functionName;
38                 $this->workerId = Strings::getRandomHex($idLength);
39         }
40
41         /**
42          * Sets the function name for additional logging
43          *
44          * @param string $functionName
45          */
46         public function setFunctionName(string $functionName)
47         {
48                 $this->functionName = $functionName;
49         }
50
51         /**
52          * Adds the worker context for each log entry
53          *
54          * @param array $context
55          */
56         private function addContext(array &$context)
57         {
58                 $context['worker_id'] = $this->workerId;
59                 $context['worker_cmd'] = $this->functionName;
60         }
61
62         /**
63          * Returns the worker ID
64          *
65          * @return string
66          */
67         public function getWorkerId()
68         {
69                 return $this->workerId;
70         }
71
72         /**
73          * System is unusable.
74          *
75          * @param string $message
76          * @param array $context
77          *
78          * @return void
79          */
80         public function emergency($message, array $context = [])
81         {
82                 $this->addContext($context);
83                 $this->logger->emergency($message, $context);
84         }
85
86         /**
87          * Action must be taken immediately.
88          *
89          * Example: Entire website down, database unavailable, etc. This should
90          * trigger the SMS alerts and wake you up.
91          *
92          * @param string $message
93          * @param array $context
94          *
95          * @return void
96          */
97         public function alert($message, array $context = [])
98         {
99                 $this->addContext($context);
100                 $this->logger->alert($message, $context);
101         }
102
103         /**
104          * Critical conditions.
105          *
106          * Example: Application component unavailable, unexpected exception.
107          *
108          * @param string $message
109          * @param array $context
110          *
111          * @return void
112          */
113         public function critical($message, array $context = [])
114         {
115                 $this->addContext($context);
116                 $this->logger->critical($message, $context);
117         }
118
119         /**
120          * Runtime errors that do not require immediate action but should typically
121          * be logged and monitored.
122          *
123          * @param string $message
124          * @param array $context
125          *
126          * @return void
127          */
128         public function error($message, array $context = [])
129         {
130                 $this->addContext($context);
131                 $this->logger->error($message, $context);
132         }
133
134         /**
135          * Exceptional occurrences that are not errors.
136          *
137          * Example: Use of deprecated APIs, poor use of an API, undesirable things
138          * that are not necessarily wrong.
139          *
140          * @param string $message
141          * @param array $context
142          *
143          * @return void
144          */
145         public function warning($message, array $context = [])
146         {
147                 $this->addContext($context);
148                 $this->logger->warning($message, $context);
149         }
150
151         /**
152          * Normal but significant events.
153          *
154          * @param string $message
155          * @param array $context
156          *
157          * @return void
158          */
159         public function notice($message, array $context = [])
160         {
161                 $this->addContext($context);
162                 $this->logger->notice($message, $context);
163         }
164
165         /**
166          * Interesting events.
167          *
168          * Example: User logs in, SQL logs.
169          *
170          * @param string $message
171          * @param array $context
172          *
173          * @return void
174          */
175         public function info($message, array $context = [])
176         {
177                 $this->addContext($context);
178                 $this->logger->info($message, $context);
179         }
180
181         /**
182          * Detailed debug information.
183          *
184          * @param string $message
185          * @param array $context
186          *
187          * @return void
188          */
189         public function debug($message, array $context = [])
190         {
191                 $this->addContext($context);
192                 $this->logger->debug($message, $context);
193         }
194
195         /**
196          * Logs with an arbitrary level.
197          *
198          * @param mixed $level
199          * @param string $message
200          * @param array $context
201          *
202          * @return void
203          */
204         public function log($level, $message, array $context = [])
205         {
206                 $this->addContext($context);
207                 $this->logger->log($level, $message, $context);
208         }
209 }