better syslog handler (thomas (at) gelf (dot) net, Bug #7845)
authorChuck Hagenbuch <chuck@horde.org>
Wed, 14 Jan 2009 20:31:25 +0000 (15:31 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Wed, 14 Jan 2009 20:31:25 +0000 (15:31 -0500)
framework/Log/lib/Horde/Log/Handler/Syslog.php

index db1fb82..d216451 100644 (file)
@@ -2,31 +2,25 @@
 /**
  * Horde Log package
  *
- * @category Horde
- * @package  Horde_Log
+ * @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
+ * @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
+ * @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
+ * @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
      */
@@ -38,6 +32,18 @@ class Horde_Log_Handler_Syslog extends Horde_Log_Handler_Base
     );
 
     /**
+     * Last ident set by a syslog-handler instance
+     * @var string
+     */
+    protected static $_lastIdent;
+
+    /**
+     * Last facility name set by a syslog-handler instance
+     * @var string
+     */
+    protected static $_lastFacility;
+
+    /**
      * Map of log levels to syslog priorities
      * @var array
      */
@@ -52,7 +58,6 @@ class Horde_Log_Handler_Syslog extends Horde_Log_Handler_Base
         Horde_Log::DEBUG   => LOG_DEBUG,
     );
 
-
     /**
      * Write a message to the log.
      *
@@ -61,10 +66,9 @@ class Horde_Log_Handler_Syslog extends Horde_Log_Handler_Base
      */
     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');
-            }
+        if ($this->_options['ident'] !== self::$_lastIdent ||
+            $this->_options['facility'] !== self::$_lastFacility) {
+            $this->_initializeSyslog();
         }
 
         $priority = $this->_toSyslog($event['level']);
@@ -90,4 +94,20 @@ class Horde_Log_Handler_Syslog extends Horde_Log_Handler_Base
         return $this->_options['defaultPriority'];
     }
 
+    /**
+     * Initialize syslog / set ident and facility
+     *
+     * @param  string  $ident         ident
+     * @param  string  $facility      syslog facility
+     * @return void
+     */
+    protected function _initializeSyslog()
+    {
+        self::$_lastIdent = $this->_options['ident'];
+        self::$_lastFacility = $this->_options['facility'];
+        if (! openlog($this->_options['ident'], $this->_options['openlogOptions'], $this->_options['facility'])) {
+            throw new Horde_Log_Exception('Unable to open syslog');
+        }
+    }
+
 }