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