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 * Adds a new entry to the syslog
92 * @param string $message
93 * @param array $context
95 * @throws InternalServerErrorException if the syslog isn't available
97 protected function addEntry($level, $message, $context = [])
99 $logLevel = $this->mapLevelToPriority($level);
101 if ($logLevel >= $this->logLevel) {
105 $formattedLog = $this->formatLog($logLevel, $message, $context);
106 $this->write($logLevel, $formattedLog);
110 * Maps the LogLevel (@see LogLevel ) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters )
112 * @param string $level A LogLevel
114 * @return int The SysLog priority
116 * @throws \Psr\Log\InvalidArgumentException If the loglevel isn't valid
118 public function mapLevelToPriority($level)
120 if (!array_key_exists($level, $this->logLevels)) {
121 throw new InvalidArgumentException('LogLevel \'' . $level . '\' isn\'t valid.');
124 return $this->logLevels[$level];
130 public function close()
136 * Writes a message to the syslog
137 * @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters
139 * @param int $priority The Priority
140 * @param string $message The message of the log
142 * @throws InternalServerErrorException if syslog cannot be used
144 private function write($priority, $message)
146 if (!openlog(self::IDENT, $this->logOpts, $this->logFacility)) {
147 throw new InternalServerErrorException('Can\'t open syslog for ident "' . $this->channel . '" and facility "' . $this->logFacility . '""');
150 syslog($priority, $message);
154 * Formats a log record for the syslog output
156 * @param int $level The loglevel/priority
157 * @param string $message The message
158 * @param array $context The context of this call
160 * @return string the formatted syslog output
162 private function formatLog($level, $message, $context = [])
164 $record = $this->introspection->getRecord();
165 $record = array_merge($record, ['uid' => $this->logUid]);
168 $logMessage .= $this->channel . ' ';
169 $logMessage .= '[' . $this->logToString[$level] . ']: ';
170 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
171 $logMessage .= @json_encode($context) . ' - ';
172 $logMessage .= @json_encode($record);