3 namespace Friendica\Util\Logger;
5 use Friendica\Network\HTTPException\InternalServerErrorException;
6 use Friendica\Util\Strings;
7 use Psr\Log\InvalidArgumentException;
8 use Psr\Log\LoggerInterface;
12 * A Logger instance for syslogging (fast, but simple)
13 * @see http://php.net/manual/en/function.syslog.php
15 class SyslogLogger implements LoggerInterface
17 const IDENT = 'Friendica';
20 * Translates LogLevel log levels to syslog log priorities.
23 private $logLevels = [
24 LogLevel::DEBUG => LOG_DEBUG,
25 LogLevel::INFO => LOG_INFO,
26 LogLevel::NOTICE => LOG_NOTICE,
27 LogLevel::WARNING => LOG_WARNING,
28 LogLevel::ERROR => LOG_ERR,
29 LogLevel::CRITICAL => LOG_CRIT,
30 LogLevel::ALERT => LOG_ALERT,
31 LogLevel::EMERGENCY => LOG_EMERG,
35 * Translates log priorities to string outputs
38 private $logToString = [
41 LOG_NOTICE => 'NOTICE',
42 LOG_WARNING => 'WARNING',
44 LOG_CRIT => 'CRITICAL',
46 LOG_EMERG => 'EMERGENCY'
50 * The channel of the current process (added to each message)
56 * Indicates what logging options will be used when generating a log message
57 * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
64 * Used to specify what type of program is logging the message
65 * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
72 * The minimum loglevel at which this logger will be triggered
78 * The Introspector for the current call
81 private $introspection;
84 * The UID of the current call
90 * @param string $channel The output channel
91 * @param string $level The minimum loglevel at which this logger will be triggered
92 * @param int $logOpts Indicates what logging options will be used when generating a log message
93 * @param int $logFacility Used to specify what type of program is logging the message
95 public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
97 $this->logUid = Strings::getRandomHex(6);
98 $this->channel = $channel;
99 $this->logOpts = $logOpts;
100 $this->logFacility = $logFacility;
101 $this->logLevel = $this->mapLevelToPriority($level);
102 $this->introspection = $introspection;
106 * Maps the LogLevel (@see LogLevel ) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters )
108 * @param string $level A LogLevel
110 * @return int The SysLog priority
112 * @throws \Psr\Log\InvalidArgumentException If the loglevel isn't valid
114 public function mapLevelToPriority($level)
116 if (!array_key_exists($level, $this->logLevels)) {
117 throw new InvalidArgumentException('LogLevel \'' . $level . '\' isn\'t valid.');
120 return $this->logLevels[$level];
124 * Writes a message to the syslog
125 * @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters
127 * @param int $priority The Priority
128 * @param string $message The message of the log
130 * @throws InternalServerErrorException if syslog cannot be used
132 private function write($priority, $message)
134 if (!openlog(self::IDENT, $this->logOpts, $this->logFacility)) {
135 throw new InternalServerErrorException('Can\'t open syslog for ident "' . $this->channel . '" and facility "' . $this->logFacility . '""');
138 syslog($priority, $message);
144 public function close()
150 * Formats a log record for the syslog output
152 * @param int $level The loglevel/priority
153 * @param string $message The message
154 * @param array $context The context of this call
156 * @return string the formatted syslog output
158 private function formatLog($level, $message, $context = [])
160 $record = $this->introspection->getRecord();
161 $record = array_merge($record, ['uid' => $this->logUid]);
164 $logMessage .= $this->channel . ' ';
165 $logMessage .= '[' . $this->logToString[$level] . ']: ';
166 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
167 $logMessage .= @json_encode($context) . ' - ';
168 $logMessage .= @json_encode($record);
174 * Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
175 * @see https://www.php-fig.org/psr/psr-3/#12-message
177 * @param string $message
178 * @param array $context
180 * @return string the interpolated message
182 private function psrInterpolate($message, array $context = array())
185 foreach ($context as $key => $value) {
186 // check that the value can be casted to string
187 if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
188 $replace['{' . $key . '}'] = $value;
189 } elseif (is_array($value)) {
190 $replace['{' . $key . '}'] = @json_encode($value);
194 return strtr($message, $replace);
198 * Adds a new entry to the syslog
201 * @param string $message
202 * @param array $context
204 * @throws InternalServerErrorException if the syslog isn't available
206 private function addEntry($level, $message, $context = [])
208 if ($level >= $this->logLevel) {
212 $formattedLog = $this->formatLog($level, $message, $context);
213 $this->write($level, $formattedLog);
218 * @throws InternalServerErrorException if the syslog isn't available
220 public function emergency($message, array $context = array())
222 $this->addEntry(LOG_EMERG, $message, $context);
227 * @throws InternalServerErrorException if the syslog isn't available
229 public function alert($message, array $context = array())
231 $this->addEntry(LOG_ALERT, $message, $context);
236 * @throws InternalServerErrorException if the syslog isn't available
238 public function critical($message, array $context = array())
240 $this->addEntry(LOG_CRIT, $message, $context);
245 * @throws InternalServerErrorException if the syslog isn't available
247 public function error($message, array $context = array())
249 $this->addEntry(LOG_ERR, $message, $context);
254 * @throws InternalServerErrorException if the syslog isn't available
256 public function warning($message, array $context = array())
258 $this->addEntry(LOG_WARNING, $message, $context);
263 * @throws InternalServerErrorException if the syslog isn't available
265 public function notice($message, array $context = array())
267 $this->addEntry(LOG_NOTICE, $message, $context);
272 * @throws InternalServerErrorException if the syslog isn't available
274 public function info($message, array $context = array())
276 $this->addEntry(LOG_INFO, $message, $context);
281 * @throws InternalServerErrorException if the syslog isn't available
283 public function debug($message, array $context = array())
285 $this->addEntry(LOG_DEBUG, $message, $context);
290 * @throws InternalServerErrorException if the syslog isn't available
292 public function log($level, $message, array $context = array())
294 $logLevel = $this->mapLevelToPriority($level);
295 $this->addEntry($logLevel, $message, $context);