3 namespace Friendica\Util\Logger;
5 use Friendica\Network\HTTPException\InternalServerErrorException;
6 use Friendica\Util\Introspection;
7 use Psr\Log\InvalidArgumentException;
11 * A Logger instance for syslogging (fast, but simple)
12 * @see http://php.net/manual/en/function.syslog.php
14 class SyslogLogger extends AbstractFriendicaLogger
16 const IDENT = 'Friendica';
19 * Translates LogLevel log levels to syslog log priorities.
22 private $logLevels = [
23 LogLevel::DEBUG => LOG_DEBUG,
24 LogLevel::INFO => LOG_INFO,
25 LogLevel::NOTICE => LOG_NOTICE,
26 LogLevel::WARNING => LOG_WARNING,
27 LogLevel::ERROR => LOG_ERR,
28 LogLevel::CRITICAL => LOG_CRIT,
29 LogLevel::ALERT => LOG_ALERT,
30 LogLevel::EMERGENCY => LOG_EMERG,
34 * Translates log priorities to string outputs
37 private $logToString = [
40 LOG_NOTICE => 'NOTICE',
41 LOG_WARNING => 'WARNING',
43 LOG_CRIT => 'CRITICAL',
45 LOG_EMERG => 'EMERGENCY'
49 * Indicates what logging options will be used when generating a log message
50 * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
57 * Used to specify what type of program is logging the message
58 * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
65 * The minimum loglevel at which this logger will be triggered
72 * @param string $level The minimum loglevel at which this logger will be triggered
73 * @param int $logOpts Indicates what logging options will be used when generating a log message
74 * @param int $logFacility Used to specify what type of program is logging the message
78 public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
80 parent::__construct($channel, $introspection);
81 $this->logOpts = $logOpts;
82 $this->logFacility = $logFacility;
83 $this->logLevel = $this->mapLevelToPriority($level);
84 $this->introspection->addClasses(array(self::class));
88 * Adds a new entry to the syslog
91 * @param string $message
92 * @param array $context
94 * @throws InternalServerErrorException if the syslog isn't available
96 protected function addEntry($level, $message, $context = [])
98 $logLevel = $this->mapLevelToPriority($level);
100 if ($logLevel > $this->logLevel) {
104 $formattedLog = $this->formatLog($logLevel, $message, $context);
105 $this->write($logLevel, $formattedLog);
109 * Maps the LogLevel (@see LogLevel ) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters )
111 * @param string $level A LogLevel
113 * @return int The SysLog priority
115 * @throws \Psr\Log\InvalidArgumentException If the loglevel isn't valid
117 public function mapLevelToPriority($level)
119 if (!array_key_exists($level, $this->logLevels)) {
120 throw new InvalidArgumentException('LogLevel \'' . $level . '\' isn\'t valid.');
123 return $this->logLevels[$level];
129 public function close()
135 * Writes a message to the syslog
136 * @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters
138 * @param int $priority The Priority
139 * @param string $message The message of the log
141 * @throws InternalServerErrorException if syslog cannot be used
143 private function write($priority, $message)
145 if (!openlog(self::IDENT, $this->logOpts, $this->logFacility)) {
146 throw new InternalServerErrorException('Can\'t open syslog for ident "' . $this->channel . '" and facility "' . $this->logFacility . '""');
149 syslog($priority, $message);
153 * Formats a log record for the syslog output
155 * @param int $level The loglevel/priority
156 * @param string $message The message
157 * @param array $context The context of this call
159 * @return string the formatted syslog output
161 private function formatLog($level, $message, $context = [])
163 $record = $this->introspection->getRecord();
164 $record = array_merge($record, ['uid' => $this->logUid]);
167 $logMessage .= $this->channel . ' ';
168 $logMessage .= '[' . $this->logToString[$level] . ']: ';
169 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
170 $logMessage .= @json_encode($context) . ' - ';
171 $logMessage .= @json_encode($record);