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