Adding Logger Tests

This commit is contained in:
Philipp Holzer 2019-03-04 23:49:37 +01:00 committed by Hypolite Petovan
parent aee348fa02
commit f63956851b
3 changed files with 38 additions and 3 deletions

View file

@ -66,6 +66,12 @@ class SyslogLogger extends AbstractLogger
*/
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
@ -141,8 +147,12 @@ class SyslogLogger extends AbstractLogger
*/
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));
}
$this->syslogWrapper($priority, $message);
@ -172,6 +182,11 @@ class SyslogLogger extends AbstractLogger
return $logMessage;
}
private function customErrorHandler($code, $msg)
{
$this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
}
/**
* A syslog wrapper to make syslog functionality testable
*
@ -180,6 +195,12 @@ class SyslogLogger extends AbstractLogger
*/
protected function syslogWrapper($level, $entry)
{
syslog($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));
}
}
}