Logger/SyslogWriter: Define configuration defaults here

Usage of closelog() is optional so I removed the explicit calls.
This commit is contained in:
Eric Lippmann 2014-10-16 15:56:38 +02:00
parent 04a8df54cd
commit d2d653209f
1 changed files with 29 additions and 69 deletions

View File

@ -4,27 +4,24 @@
namespace Icinga\Logger\Writer;
use Exception;
use Zend_Config;
use Icinga\Logger\Logger;
use Icinga\Logger\LogWriter;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\IcingaException;
/**
* Class to write messages to syslog
* Log to the syslog service
*/
class SyslogWriter extends LogWriter
{
/**
* The facility where to write messages to
* Syslog facility
*
* @var string
* @var int
*/
protected $facility;
/**
* The prefix to prepend to each message
* Prefix to prepend to each message
*
* @var string
*/
@ -35,79 +32,42 @@ class SyslogWriter extends LogWriter
*
* @var array
*/
protected $facilities = array(
'LOG_USER' => LOG_USER
public static $facilities = array(
'user' => LOG_USER
);
/**
* Create a new log writer initialized with the given configuration
* Log level to syslog severity map
*
* @var array
*/
public static $severityMap = array(
Logger::ERROR => LOG_ERR,
Logger::WARNING => LOG_WARNING,
Logger::INFO => LOG_INFO,
Logger::DEBUG => LOG_DEBUG
);
/**
* Create a new syslog log writer
*
* @param Zend_Config $config
*/
public function __construct(Zend_Config $config)
{
if (!array_key_exists($config->facility, $this->facilities)) {
throw new ConfigurationError(
'Cannot create syslog writer with unknown facility "%s"',
$config->facility
);
}
$this->ident = $config->application;
$this->facility = $this->facilities[$config->facility];
$this->ident = $config->get('application', 'icingaweb');
$this->facility = static::$facilities['user'];
}
/**
* Log a message with the given severity
* Log a message
*
* @param int $severity The severity to use
* @param string $message The message to log
*
* @throws Exception In case the given severity cannot be mapped to a valid syslog priority
* @param int $level The logging level
* @param string $message The log message
*/
public function log($severity, $message)
public function log($level, $message)
{
$priorities = array(
Logger::$ERROR => LOG_ERR,
Logger::$WARNING => LOG_WARNING,
Logger::$INFO => LOG_INFO,
Logger::$DEBUG => LOG_DEBUG
);
if (!array_key_exists($severity, $priorities)) {
throw new IcingaException(
'Severity "%s" cannot be mapped to a valid syslog priority',
$severity
);
}
$this->open();
$this->write($priorities[$severity], $message);
$this->close();
}
/**
* Open a new syslog connection
*/
protected function open()
{
openlog($this->ident, 0, $this->facility);
}
/**
* Write a message to the syslog connection
*
* @param int $priority The priority to use
* @param string $message The message to write
*/
protected function write($priority, $message)
{
syslog($priority, $message);
}
/**
* Close the syslog connection
*/
protected function close()
{
closelog();
openlog($this->ident, LOG_PID, $this->facility);
syslog(static::$severityMap[$level], $message);
}
}