diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:39:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:39:39 +0000 |
commit | 8ca6cc32b2c789a3149861159ad258f2cb9491e3 (patch) | |
tree | 2492de6f1528dd44eaa169a5c1555026d9cb75ec /library/Icinga/Application/Logger/Writer/SyslogWriter.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-8ca6cc32b2c789a3149861159ad258f2cb9491e3.tar.xz icingaweb2-8ca6cc32b2c789a3149861159ad258f2cb9491e3.zip |
Adding upstream version 2.11.4.upstream/2.11.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Icinga/Application/Logger/Writer/SyslogWriter.php')
-rw-r--r-- | library/Icinga/Application/Logger/Writer/SyslogWriter.php | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/library/Icinga/Application/Logger/Writer/SyslogWriter.php b/library/Icinga/Application/Logger/Writer/SyslogWriter.php new file mode 100644 index 0000000..93efc2a --- /dev/null +++ b/library/Icinga/Application/Logger/Writer/SyslogWriter.php @@ -0,0 +1,90 @@ +<?php +/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Application\Logger\Writer; + +use Icinga\Data\ConfigObject; +use Icinga\Application\Logger; +use Icinga\Application\Logger\LogWriter; +use Icinga\Exception\ConfigurationError; + +/** + * Log to the syslog service + */ +class SyslogWriter extends LogWriter +{ + /** + * Syslog facility + * + * @var int + */ + protected $facility; + + /** + * Prefix to prepend to each message + * + * @var string + */ + protected $ident; + + /** + * Known syslog facilities + * + * @var array + */ + public static $facilities = array( + 'user' => LOG_USER, + 'local0' => LOG_LOCAL0, + 'local1' => LOG_LOCAL1, + 'local2' => LOG_LOCAL2, + 'local3' => LOG_LOCAL3, + 'local4' => LOG_LOCAL4, + 'local5' => LOG_LOCAL5, + 'local6' => LOG_LOCAL6, + 'local7' => LOG_LOCAL7 + ); + + /** + * 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 ConfigObject $config + */ + public function __construct(ConfigObject $config) + { + $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]; + } + + /** + * Log a message + * + * @param int $level The logging level + * @param string $message The log message + */ + public function log($level, $message) + { + openlog($this->ident, LOG_PID, $this->facility); + syslog(static::$severityMap[$level], str_replace("\n", ' ', $message)); + } +} |