]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger/Type/AbstractLogger.php
Replace own VoidLogger with PSR-Standard NullLogger()
[friendica.git] / src / Core / Logger / Type / AbstractLogger.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\Introspection;
26 use Friendica\Util\Strings;
27 use Psr\Log\LoggerInterface;
28 use Psr\Log\LogLevel;
29
30 /**
31  * This class contains all necessary dependencies and calls for Friendica
32  * Every new Logger should extend this class and define, how addEntry() works
33  *
34  * Additional information for each Logger, who extends this class:
35  * - Introspection
36  * - UID for each call
37  * - Channel of the current call (i.e. index, worker, daemon, ...)
38  */
39 abstract class AbstractLogger implements LoggerInterface
40 {
41         /**
42          * The output channel of this logger
43          * @var string
44          */
45         protected $channel;
46
47         /**
48          * The Introspection for the current call
49          * @var Introspection
50          */
51         protected $introspection;
52
53         /**
54          * The UID of the current call
55          * @var string
56          */
57         protected $logUid;
58
59         /**
60          * Adds a new entry to the log
61          *
62          * @param mixed  $level
63          * @param string $message
64          * @param array  $context
65          *
66          * @return void
67          */
68         abstract protected function addEntry($level, string $message, array $context = []);
69
70         /**
71          * @param string        $channel       The output channel
72          * @param Introspection $introspection The introspection of the current call
73          *
74          * @throws LoggerException
75          */
76         public function __construct(string $channel, Introspection $introspection)
77         {
78                 $this->channel       = $channel;
79                 $this->introspection = $introspection;
80
81                 try {
82                         $this->logUid = Strings::getRandomHex(6);
83                 } catch (\Exception $exception) {
84                         throw new LoggerException('Cannot generate log Id', $exception);
85                 }
86         }
87
88         /**
89          * Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
90          *
91          * @see https://www.php-fig.org/psr/psr-3/#12-message
92          *
93          * @param string $message
94          * @param array  $context
95          *
96          * @return string the interpolated message
97          */
98         protected function psrInterpolate(string $message, array $context = []): string
99         {
100                 $replace = [];
101                 foreach ($context as $key => $value) {
102                         // check that the value can be casted to string
103                         if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
104                                 $replace['{' . $key . '}'] = $value;
105                         } elseif (is_array($value)) {
106                                 $replace['{' . $key . '}'] = @json_encode($value);
107                         }
108                 }
109
110                 return strtr($message, $replace);
111         }
112
113         /**
114          * JSON Encodes a complete array including objects with "__toString()" methods
115          *
116          * @param array $input an Input Array to encode
117          *
118          * @return false|string The json encoded output of the array
119          */
120         protected function jsonEncodeArray(array $input)
121         {
122                 $output = [];
123
124                 foreach ($input as $key => $value) {
125                         if (is_object($value) && method_exists($value, '__toString')) {
126                                 $output[$key] = $value->__toString();
127                         } else {
128                                 $output[$key] = $value;
129                         }
130                 }
131
132                 return @json_encode($output);
133         }
134
135         /**
136          * {@inheritdoc}
137          */
138         public function emergency($message, array $context = [])
139         {
140                 $this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
141         }
142
143         /**
144          * {@inheritdoc}
145          */
146         public function alert($message, array $context = [])
147         {
148                 $this->addEntry(LogLevel::ALERT, (string) $message, $context);
149         }
150
151         /**
152          * {@inheritdoc}
153          */
154         public function critical($message, array $context = [])
155         {
156                 $this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
157         }
158
159         /**
160          * {@inheritdoc}
161          */
162         public function error($message, array $context = [])
163         {
164                 $this->addEntry(LogLevel::ERROR, (string) $message, $context);
165         }
166
167         /**
168          * {@inheritdoc}
169          */
170         public function warning($message, array $context = [])
171         {
172                 $this->addEntry(LogLevel::WARNING, (string) $message, $context);
173         }
174
175         /**
176          * {@inheritdoc}
177          */
178         public function notice($message, array $context = [])
179         {
180                 $this->addEntry(LogLevel::NOTICE, (string) $message, $context);
181         }
182
183         /**
184          * {@inheritdoc}
185          */
186         public function info($message, array $context = [])
187         {
188                 $this->addEntry(LogLevel::INFO, (string) $message, $context);
189         }
190
191         /**
192          * {@inheritdoc}
193          */
194         public function debug($message, array $context = [])
195         {
196                 $this->addEntry(LogLevel::DEBUG, (string) $message, $context);
197         }
198
199         /**
200          * {@inheritdoc}
201          */
202         public function log($level, $message, array $context = [])
203         {
204                 $this->addEntry($level, (string) $message, $context);
205         }
206 }