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