]> git.mxchange.org Git - friendica.git/blob - src/Util/Logger/WorkerLogger.php
Merge pull request #7248 from nupplaphil/bugs/6916-fatal-network
[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          * Adds the worker context for each log entry
43          *
44          * @param array $context
45          */
46         private function addContext(array &$context)
47         {
48                 $context['worker_id'] = $this->workerId;
49                 $context['worker_cmd'] = $this->functionName;
50         }
51
52         /**
53          * Returns the worker ID
54          *
55          * @return string
56          */
57         public function getWorkerId()
58         {
59                 return $this->workerId;
60         }
61
62         /**
63          * System is unusable.
64          *
65          * @param string $message
66          * @param array $context
67          *
68          * @return void
69          */
70         public function emergency($message, array $context = [])
71         {
72                 $this->addContext($context);
73                 $this->logger->emergency($message, $context);
74         }
75
76         /**
77          * Action must be taken immediately.
78          *
79          * Example: Entire website down, database unavailable, etc. This should
80          * trigger the SMS alerts and wake you up.
81          *
82          * @param string $message
83          * @param array $context
84          *
85          * @return void
86          */
87         public function alert($message, array $context = [])
88         {
89                 $this->addContext($context);
90                 $this->logger->alert($message, $context);
91         }
92
93         /**
94          * Critical conditions.
95          *
96          * Example: Application component unavailable, unexpected exception.
97          *
98          * @param string $message
99          * @param array $context
100          *
101          * @return void
102          */
103         public function critical($message, array $context = [])
104         {
105                 $this->addContext($context);
106                 $this->logger->critical($message, $context);
107         }
108
109         /**
110          * Runtime errors that do not require immediate action but should typically
111          * be logged and monitored.
112          *
113          * @param string $message
114          * @param array $context
115          *
116          * @return void
117          */
118         public function error($message, array $context = [])
119         {
120                 $this->addContext($context);
121                 $this->logger->error($message, $context);
122         }
123
124         /**
125          * Exceptional occurrences that are not errors.
126          *
127          * Example: Use of deprecated APIs, poor use of an API, undesirable things
128          * that are not necessarily wrong.
129          *
130          * @param string $message
131          * @param array $context
132          *
133          * @return void
134          */
135         public function warning($message, array $context = [])
136         {
137                 $this->addContext($context);
138                 $this->logger->warning($message, $context);
139         }
140
141         /**
142          * Normal but significant events.
143          *
144          * @param string $message
145          * @param array $context
146          *
147          * @return void
148          */
149         public function notice($message, array $context = [])
150         {
151                 $this->addContext($context);
152                 $this->logger->notice($message, $context);
153         }
154
155         /**
156          * Interesting events.
157          *
158          * Example: User logs in, SQL logs.
159          *
160          * @param string $message
161          * @param array $context
162          *
163          * @return void
164          */
165         public function info($message, array $context = [])
166         {
167                 $this->addContext($context);
168                 $this->logger->info($message, $context);
169         }
170
171         /**
172          * Detailed debug information.
173          *
174          * @param string $message
175          * @param array $context
176          *
177          * @return void
178          */
179         public function debug($message, array $context = [])
180         {
181                 $this->addContext($context);
182                 $this->logger->debug($message, $context);
183         }
184
185         /**
186          * Logs with an arbitrary level.
187          *
188          * @param mixed $level
189          * @param string $message
190          * @param array $context
191          *
192          * @return void
193          */
194         public function log($level, $message, array $context = [])
195         {
196                 $this->addContext($context);
197                 $this->logger->log($level, $message, $context);
198         }
199 }