2014-02-26 10:50:06 +01:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-02-26 10:50:06 +01:00
|
|
|
|
2014-10-31 10:27:17 +01:00
|
|
|
namespace Icinga\Application\Logger\Writer;
|
2014-02-26 10:50:06 +01:00
|
|
|
|
2014-11-18 13:11:52 +01:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-10-31 10:27:17 +01:00
|
|
|
use Icinga\Application\Logger;
|
|
|
|
use Icinga\Application\Logger\LogWriter;
|
2014-02-26 10:50:06 +01:00
|
|
|
|
|
|
|
/**
|
2014-10-16 15:56:38 +02:00
|
|
|
* Log to the syslog service
|
2014-02-26 10:50:06 +01:00
|
|
|
*/
|
|
|
|
class SyslogWriter extends LogWriter
|
|
|
|
{
|
|
|
|
/**
|
2014-10-16 15:56:38 +02:00
|
|
|
* Syslog facility
|
2014-02-26 10:50:06 +01:00
|
|
|
*
|
2014-10-16 15:56:38 +02:00
|
|
|
* @var int
|
2014-02-26 10:50:06 +01:00
|
|
|
*/
|
|
|
|
protected $facility;
|
|
|
|
|
|
|
|
/**
|
2014-10-16 15:56:38 +02:00
|
|
|
* Prefix to prepend to each message
|
2014-02-26 10:50:06 +01:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $ident;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Known syslog facilities
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-10-16 15:56:38 +02:00
|
|
|
public static $facilities = array(
|
|
|
|
'user' => LOG_USER
|
2014-02-26 10:50:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2014-10-16 15:56:38 +02:00
|
|
|
* Log level to syslog severity map
|
2014-02-26 10:50:06 +01:00
|
|
|
*
|
2014-10-16 15:56:38 +02:00
|
|
|
* @var array
|
2014-02-26 10:50:06 +01:00
|
|
|
*/
|
2014-10-16 15:56:38 +02: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
|
|
|
|
|
|
|
/**
|
2014-10-16 15:56:38 +02:00
|
|
|
* Create a new syslog log writer
|
2014-02-26 10:50:06 +01:00
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @param ConfigObject $config
|
2014-02-26 10:50:06 +01:00
|
|
|
*/
|
2014-11-18 13:11:52 +01:00
|
|
|
public function __construct(ConfigObject $config)
|
2014-02-26 10:50:06 +01:00
|
|
|
{
|
2015-02-12 14:01:59 +01:00
|
|
|
$this->ident = $config->get('application', 'icingaweb2');
|
2014-10-16 15:56:38 +02:00
|
|
|
$this->facility = static::$facilities['user'];
|
2014-02-26 10:50:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-16 15:56:38 +02:00
|
|
|
* Log a message
|
|
|
|
*
|
|
|
|
* @param int $level The logging level
|
|
|
|
* @param string $message The log message
|
2014-02-26 10:50:06 +01:00
|
|
|
*/
|
2014-10-16 15:56:38 +02:00
|
|
|
public function log($level, $message)
|
2014-02-26 10:50:06 +01:00
|
|
|
{
|
2014-10-16 15:56:38 +02:00
|
|
|
openlog($this->ident, LOG_PID, $this->facility);
|
2015-04-24 10:28:45 +02:00
|
|
|
syslog(static::$severityMap[$level], str_replace("\n", ' ', $message));
|
2014-02-26 10:50:06 +01:00
|
|
|
}
|
|
|
|
}
|