Logger/SyslogWriter: Define configuration defaults here
Usage of closelog() is optional so I removed the explicit calls.
This commit is contained in:
parent
04a8df54cd
commit
d2d653209f
|
@ -4,27 +4,24 @@
|
||||||
|
|
||||||
namespace Icinga\Logger\Writer;
|
namespace Icinga\Logger\Writer;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Zend_Config;
|
use Zend_Config;
|
||||||
use Icinga\Logger\Logger;
|
use Icinga\Logger\Logger;
|
||||||
use Icinga\Logger\LogWriter;
|
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
|
class SyslogWriter extends LogWriter
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The facility where to write messages to
|
* Syslog facility
|
||||||
*
|
*
|
||||||
* @var string
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $facility;
|
protected $facility;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The prefix to prepend to each message
|
* Prefix to prepend to each message
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
|
@ -35,79 +32,42 @@ class SyslogWriter extends LogWriter
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $facilities = array(
|
public static $facilities = array(
|
||||||
'LOG_USER' => LOG_USER
|
'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)
|
public function __construct(Zend_Config $config)
|
||||||
{
|
{
|
||||||
if (!array_key_exists($config->facility, $this->facilities)) {
|
$this->ident = $config->get('application', 'icingaweb');
|
||||||
throw new ConfigurationError(
|
$this->facility = static::$facilities['user'];
|
||||||
'Cannot create syslog writer with unknown facility "%s"',
|
|
||||||
$config->facility
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->ident = $config->application;
|
|
||||||
$this->facility = $this->facilities[$config->facility];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log a message with the given severity
|
* Log a message
|
||||||
*
|
*
|
||||||
* @param int $severity The severity to use
|
* @param int $level The logging level
|
||||||
* @param string $message The message to log
|
* @param string $message The log message
|
||||||
*
|
|
||||||
* @throws Exception In case the given severity cannot be mapped to a valid syslog priority
|
|
||||||
*/
|
*/
|
||||||
public function log($severity, $message)
|
public function log($level, $message)
|
||||||
{
|
{
|
||||||
$priorities = array(
|
openlog($this->ident, LOG_PID, $this->facility);
|
||||||
Logger::$ERROR => LOG_ERR,
|
syslog(static::$severityMap[$level], $message);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue