3 namespace Friendica\Util\Logger;
5 use Friendica\Network\HTTPException\InternalServerErrorException;
6 use Friendica\Util\Introspection;
7 use Friendica\Util\Profiler;
8 use Psr\Log\InvalidArgumentException;
12 * A Logger instance for syslogging (fast, but simple)
13 * @see http://php.net/manual/en/function.syslog.php
15 class SyslogLogger extends AbstractFriendicaLogger
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 * Indicates what logging options will be used when generating a log message
51 * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
58 * Used to specify what type of program is logging the message
59 * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
66 * The minimum loglevel at which this logger will be triggered
73 * @param string $level The minimum loglevel at which this logger will be triggered
74 * @param int $logOpts Indicates what logging options will be used when generating a log message
75 * @param int $logFacility Used to specify what type of program is logging the message
79 public function __construct($channel, Introspection $introspection, Profiler $profiler, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
81 parent::__construct($channel, $introspection, $profiler);
82 $this->logOpts = $logOpts;
83 $this->logFacility = $logFacility;
84 $this->logLevel = $this->mapLevelToPriority($level);
85 $this->introspection->addClasses(array(self::class));
89 * Maps the LogLevel (@see LogLevel ) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters )
91 * @param string $level A LogLevel
93 * @return int The SysLog priority
95 * @throws \Psr\Log\InvalidArgumentException If the loglevel isn't valid
97 public function mapLevelToPriority($level)
99 if (!array_key_exists($level, $this->logLevels)) {
100 throw new InvalidArgumentException('LogLevel \'' . $level . '\' isn\'t valid.');
103 return $this->logLevels[$level];
107 * Writes a message to the syslog
108 * @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters
110 * @param int $priority The Priority
111 * @param string $message The message of the log
113 * @throws InternalServerErrorException if syslog cannot be used
115 private function write($priority, $message)
117 if (!openlog(self::IDENT, $this->logOpts, $this->logFacility)) {
118 throw new InternalServerErrorException('Can\'t open syslog for ident "' . $this->channel . '" and facility "' . $this->logFacility . '""');
121 syslog($priority, $message);
127 public function close()
133 * Formats a log record for the syslog output
135 * @param int $level The loglevel/priority
136 * @param string $message The message
137 * @param array $context The context of this call
139 * @return string the formatted syslog output
141 private function formatLog($level, $message, $context = [])
143 $record = $this->introspection->getRecord();
144 $record = array_merge($record, ['uid' => $this->logUid]);
147 $logMessage .= $this->channel . ' ';
148 $logMessage .= '[' . $this->logToString[$level] . ']: ';
149 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
150 $logMessage .= @json_encode($context) . ' - ';
151 $logMessage .= @json_encode($record);
157 * Adds a new entry to the syslog
160 * @param string $message
161 * @param array $context
163 * @throws InternalServerErrorException if the syslog isn't available
165 protected function addEntry($level, $message, $context = [])
167 $logLevel = $this->mapLevelToPriority($level);
169 if ($logLevel >= $this->logLevel) {
173 $formattedLog = $this->formatLog($level, $message, $context);
174 $this->write($level, $formattedLog);