icingaweb2/library/Icinga/Application/Logger/Writer/SyslogWriter.php

101 lines
2.5 KiB
PHP
Raw Normal View History

2014-02-26 10:50:06 +01:00
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
2014-02-26 10:50:06 +01:00
namespace Icinga\Application\Logger\Writer;
2014-02-26 10:50:06 +01:00
use Icinga\Data\ConfigObject;
use Icinga\Application\Logger;
use Icinga\Application\Logger\LogWriter;
use Icinga\Exception\ConfigurationError;
2014-02-26 10:50:06 +01:00
/**
* Log to the syslog service
2014-02-26 10:50:06 +01:00
*/
class SyslogWriter extends LogWriter
{
/**
* Syslog facility
2014-02-26 10:50:06 +01:00
*
* @var int
2014-02-26 10:50:06 +01:00
*/
protected $facility;
/**
* Prefix to prepend to each message
2014-02-26 10:50:06 +01:00
*
* @var string
*/
protected $ident;
/**
* Known syslog facilities
*
* @var array
*/
public static $facilities = array(
'auth' => LOG_AUTH,
'authpriv' => LOG_AUTHPRIV,
'cron' => LOG_CRON,
'daemon' => LOG_DAEMON,
'kern' => LOG_KERN,
'local0' => LOG_LOCAL0,
'local1' => LOG_LOCAL1,
'local2' => LOG_LOCAL2,
'local3' => LOG_LOCAL3,
'local4' => LOG_LOCAL4,
'local5' => LOG_LOCAL5,
'local6' => LOG_LOCAL6,
'local7' => LOG_LOCAL7,
'lpr' => LOG_LPR,
'mail' => LOG_MAIL,
'news' => LOG_NEWS,
'syslog' => LOG_SYSLOG,
'user' => LOG_USER,
'uucp' => LOG_UUCP
2014-02-26 10:50:06 +01:00
);
/**
* Log level to syslog severity map
2014-02-26 10:50:06 +01:00
*
* @var array
2014-02-26 10:50:06 +01:00
*/
public static $severityMap = array(
Logger::ERROR => LOG_ERR,
Logger::WARNING => LOG_WARNING,
Logger::INFO => LOG_INFO,
Logger::DEBUG => LOG_DEBUG
);
2014-02-26 10:50:06 +01:00
/**
* Create a new syslog log writer
2014-02-26 10:50:06 +01:00
*
* @param ConfigObject $config
2014-02-26 10:50:06 +01:00
*/
public function __construct(ConfigObject $config)
2014-02-26 10:50:06 +01:00
{
$this->ident = $config->get('application', 'icingaweb2');
$configuredFacility = $config->get('facility', 'user');
if (! isset(static::$facilities[$configuredFacility])) {
throw new ConfigurationError(
'Invalid logging facility: "%s" (expected one of: %s)',
$configuredFacility,
implode(', ', array_keys(static::$facilities))
);
}
$this->facility = static::$facilities[$configuredFacility];
2014-02-26 10:50:06 +01:00
}
/**
* Log a message
*
* @param int $level The logging level
* @param string $message The log message
2014-02-26 10:50:06 +01:00
*/
public function log($level, $message)
2014-02-26 10:50:06 +01:00
{
openlog($this->ident, LOG_PID, $this->facility);
syslog(static::$severityMap[$level], str_replace("\n", ' ', $message));
2014-02-26 10:50:06 +01:00
}
}