3 namespace Friendica\Util\Logger;
5 use Friendica\Network\HTTPException\InternalServerErrorException;
6 use Friendica\Util\Introspection;
10 * A Logger instance for syslogging (fast, but simple)
11 * @see http://php.net/manual/en/function.syslog.php
13 class SyslogLogger extends AbstractLogger
15 const IDENT = 'Friendica';
18 * Translates LogLevel log levels to syslog log priorities.
21 private $logLevels = [
22 LogLevel::DEBUG => LOG_DEBUG,
23 LogLevel::INFO => LOG_INFO,
24 LogLevel::NOTICE => LOG_NOTICE,
25 LogLevel::WARNING => LOG_WARNING,
26 LogLevel::ERROR => LOG_ERR,
27 LogLevel::CRITICAL => LOG_CRIT,
28 LogLevel::ALERT => LOG_ALERT,
29 LogLevel::EMERGENCY => LOG_EMERG,
33 * Translates log priorities to string outputs
36 private $logToString = [
39 LOG_NOTICE => 'NOTICE',
40 LOG_WARNING => 'WARNING',
42 LOG_CRIT => 'CRITICAL',
44 LOG_EMERG => 'EMERGENCY'
48 * Indicates what logging options will be used when generating a log message
49 * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
56 * Used to specify what type of program is logging the message
57 * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
64 * The minimum loglevel at which this logger will be triggered
70 * A error message of the current operation
73 private $errorMessage;
77 * @param string $level The minimum loglevel at which this logger will be triggered
78 * @param int $logOpts Indicates what logging options will be used when generating a log message
79 * @param int $logFacility Used to specify what type of program is logging the message
83 public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
85 parent::__construct($channel, $introspection);
86 $this->logOpts = $logOpts;
87 $this->logFacility = $logFacility;
88 $this->logLevel = $this->mapLevelToPriority($level);
89 $this->introspection->addClasses(array(self::class));
93 * Adds a new entry to the syslog
96 * @param string $message
97 * @param array $context
99 * @throws InternalServerErrorException if the syslog isn't available
101 protected function addEntry($level, $message, $context = [])
103 $logLevel = $this->mapLevelToPriority($level);
105 if ($logLevel > $this->logLevel) {
109 $formattedLog = $this->formatLog($logLevel, $message, $context);
110 $this->write($logLevel, $formattedLog);
114 * Maps the LogLevel (@see LogLevel) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters)
116 * @param string $level A LogLevel
118 * @return int The SysLog priority
120 * @throws \Psr\Log\InvalidArgumentException If the loglevel isn't valid
122 public function mapLevelToPriority($level)
124 if (!array_key_exists($level, $this->logLevels)) {
125 throw new \InvalidArgumentException(sprintf('The level "%s" is not valid.', $level));
128 return $this->logLevels[$level];
134 public function close()
140 * Writes a message to the syslog
141 * @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters
143 * @param int $priority The Priority
144 * @param string $message The message of the log
146 * @throws InternalServerErrorException if syslog cannot be used
148 private function write($priority, $message)
150 set_error_handler([$this, 'customErrorHandler']);
151 $opened = openlog(self::IDENT, $this->logOpts, $this->logFacility);
152 restore_error_handler();
155 throw new \UnexpectedValueException(sprintf('Can\'t open syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
158 $this->syslogWrapper($priority, $message);
162 * Formats a log record for the syslog output
164 * @param int $level The loglevel/priority
165 * @param string $message The message
166 * @param array $context The context of this call
168 * @return string the formatted syslog output
170 private function formatLog($level, $message, $context = [])
172 $record = $this->introspection->getRecord();
173 $record = array_merge($record, ['uid' => $this->logUid]);
176 $logMessage .= $this->channel . ' ';
177 $logMessage .= '[' . $this->logToString[$level] . ']: ';
178 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
179 $logMessage .= @json_encode($context) . ' - ';
180 $logMessage .= @json_encode($record);
185 private function customErrorHandler($code, $msg)
187 $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
191 * A syslog wrapper to make syslog functionality testable
193 * @param int $level The syslog priority
194 * @param string $entry The message to send to the syslog function
196 protected function syslogWrapper($level, $entry)
198 set_error_handler([$this, 'customErrorHandler']);
199 $written = syslog($level, $entry);
200 restore_error_handler();
203 throw new \UnexpectedValueException(sprintf('Can\'t write into syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));