3 namespace Friendica\Util\Logger;
5 use Friendica\Network\HTTPException\InternalServerErrorException;
6 use Psr\Log\InvalidArgumentException;
7 use Psr\Log\LoggerInterface;
11 * A Logger instance for syslogging (fast, but simple)
12 * @see http://php.net/manual/en/function.syslog.php
14 class SyslogLogger implements LoggerInterface
17 * Translates LogLevel log levels to syslog log priorities.
19 private $logLevels = [
20 LogLevel::DEBUG => LOG_DEBUG,
21 LogLevel::INFO => LOG_INFO,
22 LogLevel::NOTICE => LOG_NOTICE,
23 LogLevel::WARNING => LOG_WARNING,
24 LogLevel::ERROR => LOG_ERR,
25 LogLevel::CRITICAL => LOG_CRIT,
26 LogLevel::ALERT => LOG_ALERT,
27 LogLevel::EMERGENCY => LOG_EMERG,
31 * The standard ident of the syslog (added to each message)
37 * Indicates what logging options will be used when generating a log message
38 * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
45 * Used to specify what type of program is logging the message
46 * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
53 * The minimum loglevel at which this logger will be triggered
59 * The Introspector for the current call
62 private $introspection;
65 * @param string $channel The channel (Syslog ident)
66 * @param string $level The minimum loglevel at which this logger will be triggered
67 * @param int $logOpts Indicates what logging options will be used when generating a log message
68 * @param int $logFacility Used to specify what type of program is logging the message
70 * @throws InternalServerErrorException if the loglevel isn't valid
72 public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
74 $this->ident = $channel;
75 $this->logOpts = $logOpts;
76 $this->logFacility = $logFacility;
77 $this->logLevel = $this->mapLevelToPriority($level);
78 $this->introspection = $introspection;
82 * Maps the LogLevel (@see LogLevel ) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters )
84 * @param string $level A LogLevel
86 * @return int The SysLog priority
88 * @throws \Psr\Log\InvalidArgumentException If the loglevel isn't valid
90 public function mapLevelToPriority($level)
92 if (!array_key_exists($level, $this->logLevels)) {
93 throw new InvalidArgumentException('LogLevel \'' . $level . '\' isn\'t valid.');
96 return $this->logLevels[$level];
100 * Writes a message to the syslog
102 * @param int $priority The Priority ( @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters )
103 * @param string $message The message of the log
104 * @throws InternalServerErrorException if syslog cannot be used
106 private function write($priority, $message)
108 if (!openlog($this->ident, $this->logOpts, $this->logFacility)) {
109 throw new InternalServerErrorException('Can\'t open syslog for ident "' . $this->ident . '" and facility "' . $this->logFacility . '""');
112 syslog($priority, $message);
115 public function close()
120 private function formatLog($level, $message, $context = [])
124 $logMessage .= $this->ident . ' ';
125 $logMessage .= '[' . $level . ']: ';
126 $logMessage .= $message . ' ';
127 $logMessage .= json_encode($context) . ' - ';
128 $logMessage .= json_encode($this->introspection->getRecord());
133 private function addEntry($level, $message, $context = [])
135 if ($level >= $this->logLevel) {
139 $formattedLog = $this->formatLog($level, $message, $context);
140 $this->write($level, $formattedLog);
146 public function emergency($message, array $context = array())
148 $this->addEntry(LOG_EMERG, $message, $context);
154 public function alert($message, array $context = array())
156 $this->addEntry(LOG_ALERT, $message, $context);
162 public function critical($message, array $context = array())
164 $this->addEntry(LOG_CRIT, $message, $context);
170 public function error($message, array $context = array())
172 $this->addEntry(LOG_ERR, $message, $context);
178 public function warning($message, array $context = array())
180 $this->addEntry(LOG_WARNING, $message, $context);
186 public function notice($message, array $context = array())
188 $this->addEntry(LOG_NOTICE, $message, $context);
194 public function info($message, array $context = array())
196 $this->addEntry(LOG_INFO, $message, $context);
202 public function debug($message, array $context = array())
204 $this->addEntry(LOG_DEBUG, $message, $context);
210 public function log($level, $message, array $context = array())
212 $logLevel = $this->mapLevelToPriority($level);
213 $this->addEntry($logLevel, $message, $context);