add syslog Horde_Log handler (#6989)
authorChuck Hagenbuch <chuck@horde.org>
Mon, 1 Dec 2008 01:34:27 +0000 (20:34 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Mon, 1 Dec 2008 01:34:27 +0000 (20:34 -0500)
framework/Log/lib/Horde/Log/Handler/Syslog.php [new file with mode: 0644]
framework/Log/package.xml

diff --git a/framework/Log/lib/Horde/Log/Handler/Syslog.php b/framework/Log/lib/Horde/Log/Handler/Syslog.php
new file mode 100644 (file)
index 0000000..db1fb82
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+/**
+ * Horde Log package
+ *
+ * @category Horde
+ * @package  Horde_Log
+ * @subpackage Handlers
+ * @author   Mike Naberezny <mike@maintainable.com>
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ */
+
+/**
+ * @category Horde
+ * @package  Horde_Log
+ * @subpackage Handlers
+ * @author   Mike Naberezny <mike@maintainable.com>
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ */
+class Horde_Log_Handler_Syslog extends Horde_Log_Handler_Base
+{
+    /**
+     * Whether openlog has been called
+     * @var bool
+     */
+    protected $_opened = false;
+
+    /**
+     * Options to be set by setOption().  Sets openlog and syslog options.
+     * @var array
+     */
+    protected $_options = array(
+        'ident'            => false,
+        'facility'         => LOG_USER,
+        'openlogOptions'   => false,
+        'defaultPriority'  => LOG_ERR,
+    );
+
+    /**
+     * Map of log levels to syslog priorities
+     * @var array
+     */
+    protected $_priorities = array(
+        Horde_Log::EMERG   => LOG_EMERG,
+        Horde_Log::ALERT   => LOG_ALERT,
+        Horde_Log::CRIT    => LOG_CRIT,
+        Horde_Log::ERR     => LOG_ERR,
+        Horde_Log::WARN    => LOG_WARNING,
+        Horde_Log::NOTICE  => LOG_NOTICE,
+        Horde_Log::INFO    => LOG_INFO,
+        Horde_Log::DEBUG   => LOG_DEBUG,
+    );
+
+
+    /**
+     * Write a message to the log.
+     *
+     * @param  array    $event    Log event
+     * @return bool               Always True
+     */
+    public function write($event)
+    {
+        if (! $this->_opened) {
+            if (! openlog($this->_options['ident'], $this->_options['openlogOptions'], $this->_options['facility'])) {
+                throw new Horde_Log_Exception('Unable to open syslog');
+            }
+        }
+
+        $priority = $this->_toSyslog($event['level']);
+        if (! syslog($priority, $event['message'])) {
+            throw new Horde_Log_Exception('Unable to log message');
+        }
+
+        return true;
+    }
+
+    /**
+     * Translate a log level to a syslog LOG_* priority.
+     *
+     * @param integer $level
+     *
+     * @return integer A LOG_* constant
+     */
+    protected function _toSyslog($level)
+    {
+        if (isset($this->_priorities[$level])) {
+            return $this->_priorities[$level];
+        }
+        return $this->_options['defaultPriority'];
+    }
+
+}
index 85e5aa4..e502fa2 100644 (file)
@@ -54,6 +54,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
        <file name="Firebug.php" role="php" />
        <file name="Null.php" role="php" />
        <file name="Stream.php" role="php" />
+       <file name="Syslog.php" role="php" />
       </dir> <!-- /lib/Horde/Log/Handler -->
       <file name="Exception.php" role="php" />
       <file name="Logger.php" role="php" />
@@ -87,6 +88,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Log/Handler/Firebug.php" as="Horde/Log/Handler/Firebug.php" />
    <install name="lib/Horde/Log/Handler/Null.php" as="Horde/Log/Handler/Null.php" />
    <install name="lib/Horde/Log/Handler/Stream.php" as="Horde/Log/Handler/Stream.php" />
+   <install name="lib/Horde/Log/Handler/Syslog.php" as="Horde/Log/Handler/Syslog.php" />
    <install name="lib/Horde/Log/Logger.php" as="Horde/Log/Logger.php" />
    <install name="lib/Horde/Log.php" as="Horde/Log.php" />
   </filelist>