use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\Introspection;
-use Psr\Log\InvalidArgumentException;
use Psr\Log\LogLevel;
/**
* A Logger instance for syslogging (fast, but simple)
* @see http://php.net/manual/en/function.syslog.php
*/
-class SyslogLogger extends AbstractFriendicaLogger
+class SyslogLogger extends AbstractLogger
{
const IDENT = 'Friendica';
*/
private $logLevel;
+ /**
+ * A error message of the current operation
+ * @var string
+ */
+ private $errorMessage;
+
/**
* {@inheritdoc}
* @param string $level The minimum loglevel at which this logger will be triggered
public function mapLevelToPriority($level)
{
if (!array_key_exists($level, $this->logLevels)) {
- throw new InvalidArgumentException('LogLevel \'' . $level . '\' isn\'t valid.');
+ throw new \InvalidArgumentException(sprintf('The level "%s" is not valid.', $level));
}
return $this->logLevels[$level];
*/
private function write($priority, $message)
{
- if (!openlog(self::IDENT, $this->logOpts, $this->logFacility)) {
- throw new InternalServerErrorException('Can\'t open syslog for ident "' . $this->channel . '" and facility "' . $this->logFacility . '""');
+ set_error_handler([$this, 'customErrorHandler']);
+ $opened = openlog(self::IDENT, $this->logOpts, $this->logFacility);
+ restore_error_handler();
+
+ if (!$opened) {
+ throw new \UnexpectedValueException(sprintf('Can\'t open syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
}
- syslog($priority, $message);
+ $this->syslogWrapper($priority, $message);
}
/**
return $logMessage;
}
+
+ private function customErrorHandler($code, $msg)
+ {
+ $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
+ }
+
+ /**
+ * A syslog wrapper to make syslog functionality testable
+ *
+ * @param int $level The syslog priority
+ * @param string $entry The message to send to the syslog function
+ */
+ protected function syslogWrapper($level, $entry)
+ {
+ set_error_handler([$this, 'customErrorHandler']);
+ $written = syslog($level, $entry);
+ restore_error_handler();
+
+ if (!$written) {
+ throw new \UnexpectedValueException(sprintf('Can\'t write into syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
+ }
+ }
}