]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger/Type/SyslogLogger.php
Fix: Pagination in search result works again
[friendica.git] / src / Core / Logger / Type / SyslogLogger.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\Core\Logger\Exception\LogLevelException;
27 use Psr\Log\LogLevel;
28
29 /**
30  * A Logger instance for syslogging (fast, but simple)
31  * @see http://php.net/manual/en/function.syslog.php
32  */
33 class SyslogLogger extends AbstractLogger
34 {
35         const NAME = 'syslog';
36
37         const IDENT = 'Friendica';
38
39         /** @var int The default syslog flags */
40         const DEFAULT_FLAGS = LOG_PID | LOG_ODELAY | LOG_CONS;
41         /** @var int The default syslog facility */
42         const DEFAULT_FACILITY = LOG_USER;
43
44         /**
45          * Translates LogLevel log levels to syslog log priorities.
46          * @var array
47          */
48         public const logLevels = [
49                 LogLevel::DEBUG     => LOG_DEBUG,
50                 LogLevel::INFO      => LOG_INFO,
51                 LogLevel::NOTICE    => LOG_NOTICE,
52                 LogLevel::WARNING   => LOG_WARNING,
53                 LogLevel::ERROR     => LOG_ERR,
54                 LogLevel::CRITICAL  => LOG_CRIT,
55                 LogLevel::ALERT     => LOG_ALERT,
56                 LogLevel::EMERGENCY => LOG_EMERG,
57         ];
58
59         /**
60          * Translates log priorities to string outputs
61          * @var array
62          */
63         protected const logToString = [
64                 LOG_DEBUG   => 'DEBUG',
65                 LOG_INFO    => 'INFO',
66                 LOG_NOTICE  => 'NOTICE',
67                 LOG_WARNING => 'WARNING',
68                 LOG_ERR     => 'ERROR',
69                 LOG_CRIT    => 'CRITICAL',
70                 LOG_ALERT   => 'ALERT',
71                 LOG_EMERG   => 'EMERGENCY'
72         ];
73
74         /**
75          * Indicates what logging options will be used when generating a log message
76          * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
77          *
78          * @var int
79          */
80         private $logOpts;
81
82         /**
83          * Used to specify what type of program is logging the message
84          * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
85          *
86          * @var int
87          */
88         private $logFacility;
89
90         /**
91          * The minimum loglevel at which this logger will be triggered
92          * @var int
93          */
94         private $logLevel;
95
96         /**
97          * A error message of the current operation
98          * @var string
99          */
100         private $errorMessage;
101
102         /**
103          * {@inheritdoc}
104          *
105          * @param string $logLevel    The minimum loglevel at which this logger will be triggered
106          * @param string $logOptions
107          * @param string $logFacility
108          */
109         public function __construct(string $channel, IHaveCallIntrospections $introspection, string $logLevel, string $logOptions, string $logFacility)
110         {
111                 parent::__construct($channel, $introspection);
112
113                 $this->logOpts     = $logOptions;
114                 $this->logFacility = $logFacility;
115                 $this->logLevel    = $logLevel;
116         }
117
118         /**
119          * Adds a new entry to the syslog
120          *
121          * @param mixed  $level
122          * @param string $message
123          * @param array  $context
124          *
125          * @throws LogLevelException in case the level isn't valid
126          * @throws LoggerException In case the syslog cannot be opened for writing
127          */
128         protected function addEntry($level, string $message, array $context = [])
129         {
130                 $logLevel = $this->mapLevelToPriority($level);
131
132                 if ($logLevel > $this->logLevel) {
133                         return;
134                 }
135
136                 $formattedLog = $this->formatLog($logLevel, $message, $context);
137                 $this->write($logLevel, $formattedLog);
138         }
139
140         /**
141          * Maps the LogLevel (@see LogLevel) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters)
142          *
143          * @param string $level A LogLevel
144          *
145          * @return int The SysLog priority
146          *
147          * @throws LogLevelException If the loglevel isn't valid
148          */
149         public function mapLevelToPriority(string $level): int
150         {
151                 if (!array_key_exists($level, static::logLevels)) {
152                         throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
153                 }
154
155                 return static::logLevels[$level];
156         }
157
158         /**
159          * Closes the Syslog
160          */
161         public function close()
162         {
163                 closelog();
164         }
165
166         /**
167          * Writes a message to the syslog
168          *
169          * @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters
170          *
171          * @param int    $priority The Priority
172          * @param string $message  The message of the log
173          *
174          * @throws LoggerException In case the syslog cannot be opened/written
175          */
176         private function write(int $priority, string $message)
177         {
178                 set_error_handler([$this, 'customErrorHandler']);
179                 $opened = openlog(self::IDENT, $this->logOpts, $this->logFacility);
180                 restore_error_handler();
181
182                 if (!$opened) {
183                         throw new LoggerException(sprintf('Can\'t open syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
184                 }
185
186                 $this->syslogWrapper($priority, $message);
187         }
188
189         /**
190          * Formats a log record for the syslog output
191          *
192          * @param int    $level   The loglevel/priority
193          * @param string $message The message
194          * @param array  $context The context of this call
195          *
196          * @return string the formatted syslog output
197          */
198         private function formatLog(int $level, string $message, array $context = []): string
199         {
200                 $record = $this->introspection->getRecord();
201                 $record = array_merge($record, ['uid' => $this->logUid]);
202
203                 $logMessage = $this->channel . ' ';
204                 $logMessage .= '[' . static::logToString[$level] . ']: ';
205                 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
206                 $logMessage .= $this->jsonEncodeArray($context) . ' - ';
207                 $logMessage .= $this->jsonEncodeArray($record);
208
209                 return $logMessage;
210         }
211
212         private function customErrorHandler($code, $msg)
213         {
214                 $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
215         }
216
217         /**
218          * A syslog wrapper to make syslog functionality testable
219          *
220          * @param int    $level The syslog priority
221          * @param string $entry The message to send to the syslog function
222          *
223          * @throws LoggerException
224          */
225         protected function syslogWrapper(int $level, string $entry)
226         {
227                 set_error_handler([$this, 'customErrorHandler']);
228                 $written = syslog($level, $entry);
229                 restore_error_handler();
230
231                 if (!$written) {
232                         throw new LoggerException(sprintf('Can\'t write into syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
233                 }
234         }
235 }