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