From 05cb0cb87a6cf32152783250e5090307b8083eb0 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 26 Feb 2014 10:50:06 +0100 Subject: [PATCH] Add syslog writer refs #5683 --- library/Icinga/Logger/Writer/SyslogWriter.php | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 library/Icinga/Logger/Writer/SyslogWriter.php diff --git a/library/Icinga/Logger/Writer/SyslogWriter.php b/library/Icinga/Logger/Writer/SyslogWriter.php new file mode 100644 index 000000000..e78971304 --- /dev/null +++ b/library/Icinga/Logger/Writer/SyslogWriter.php @@ -0,0 +1,108 @@ + LOG_USER + ); + + /** + * Create a new log writer initialized with the given configuration + */ + public function __construct(Zend_Config $config) + { + if (!array_key_exists($config->facility, $this->facilities)) { + throw new ConfigurationError( + 'Cannot create syslog writer with unknown facility "' . $config->facility . '"' + ); + } + + $this->ident = $config->application; + $this->facility = $this->facilities[$config->facility]; + } + + /** + * Log a message with the given severity + * + * @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 + */ + public function log($severity, $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 Exception('Severity "' . $severity . '" cannot be mapped to a valid syslog priority'); + } + + $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(); + } +}