/[cvs]/nfo/php/libs/net.php.pear/Log/syslog.php
ViewVC logotype

Diff of /nfo/php/libs/net.php.pear/Log/syslog.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by cvsjoko, Fri Oct 25 15:15:23 2002 UTC revision 1.2 by joko, Wed Feb 5 21:48:31 2003 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Horde: horde/lib/Log/syslog.php,v 1.6 2000/06/28 21:36:13 jon Exp $  // $Id$
3    // $Horde: horde/lib/Log/syslog.php,v 1.6 2000/06/28 21:36:13 jon Exp $
4  /**  
5   * The Log_syslog class is a concrete implementation of the Log::  /**
6   * abstract class which sends messages to syslog on UNIX-like machines   * The Log_syslog class is a concrete implementation of the Log::
7   * (PHP emulates this with the Event Log on Windows machines).   * abstract class which sends messages to syslog on UNIX-like machines
8   *   * (PHP emulates this with the Event Log on Windows machines).
9   * @author  Chuck Hagenbuch <chuck@horde.org>   *
10   * @version $Revision$   * @author  Chuck Hagenbuch <chuck@horde.org>
11   * @since   Horde 1.3   * @version $Revision$
12   */   * @since   Horde 1.3
13  class Log_syslog extends Log {   * @package Log
14         */
15      // {{{ properties  class Log_syslog extends Log {
16        
17      /** Integer holding the log facility to use. */      /**
18      var $name = LOG_SYSLOG;      * Integer holding the log facility to use.
19            * @var string
20      // }}}      */
21            var $_name = LOG_SYSLOG;
22        
23      // {{{ constructor      
24      /**      /**
25       * Constructs a new syslog object.       * Constructs a new syslog object.
26       *       *
27       * @param $log_name (optional) The syslog facility.       * @param string $name     The syslog facility.
28       * @param $ident    (optional) The identity string.       * @param string $ident    The identity string.
29       * @param $conf     (optional) The configuration array.       * @param array  $conf     The configuration array.
30       */       * @param int    $maxLevel Maximum level at which to log.
31      function Log_syslog ($log_name = LOG_SYSLOG, $ident = '', $conf = false) {       * @access public
32          $this->name = $log_name;       */
33          $this->ident = $ident;      function Log_syslog($name, $ident = '', $conf = array(),
34      }                          $maxLevel = PEAR_LOG_DEBUG)
35      // }}}      {    
36                /* Ensure we have a valid integer value for $name. */
37                if (empty($name) || !is_int($name)) {
38      // {{{ open()              $name = LOG_SYSLOG;
39      /**          }
40       * Opens a connection to the system logger, if it has not already  
41       * been opened.  This is implicitly called by log(), if necessary.          $this->_name = $name;
42       */          $this->_ident = $ident;
43      function open () {          $this->_maxLevel = $maxLevel;
44          if (!$this->opened) {      }
45              openlog($this->ident, LOG_PID, $this->name);  
46              $this->opened = true;      /**
47          }       * Opens a connection to the system logger, if it has not already
48      }       * been opened.  This is implicitly called by log(), if necessary.
49      // }}}       * @access public
50             */
51      // {{{ close()      function open()
52      /**      {
53       * Closes the connection to the system logger, if it is open.          if (!$this->_opened) {
54       */              openlog($this->_ident, LOG_PID, $this->_name);
55      function close () {              $this->_opened = true;
56          if ($this->opened) {          }
57              closelog();      }
58              $this->opened = false;  
59          }      /**
60      }       * Closes the connection to the system logger, if it is open.
61      // }}}       * @access public    
62             */
63      // {{{ log()      function close()
64      /**      {
65       * Sends $message to the currently open syslog * connection.  Calls          if ($this->_opened) {
66       * open() if necessary. Also passes the message along to any Log_observer              closelog();
67       * instances that are observing this Log.              $this->_opened = false;
68       *          }
69       * @param $message  The textual message to be logged.      }
70       * @param $priority (optional) The priority of the message.  Valid  
71       *                  values are: LOG_EMERG, LOG_ALERT, LOG_CRIT,      /**
72       *                  LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO,       * Sends $message to the currently open syslog connection.  Calls
73       *                  and LOG_DEBUG.  The default is LOG_INFO.       * open() if necessary. Also passes the message along to any Log_observer
74       */       * instances that are observing this Log.
75      function log ($message, $priority = LOG_INFO) {       *
76          if (!$this->opened)       * @param string $message  The textual message to be logged.
77              $this->open();       * @param int $priority (optional) The priority of the message.  Valid
78                 *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
79          syslog($priority, $message);       *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
80          $this->notifyAll(array('priority' => $priority, 'message' => $message));       *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
81      }       *                  The default is PEAR_LOG_INFO.
82      // }}}       * @return boolean  True on success or false on failure.
83             * @access public    
84  }       */
85        function log($message, $priority = PEAR_LOG_INFO)
86  ?>      {
87            /* Abort early if the priority is above the maximum logging level. */
88            if ($priority > $this->_maxLevel) {
89                return false;
90            }
91    
92            if (!$this->_opened) {
93                $this->open();
94            }
95    
96            if (!syslog($this->_toSyslog($priority), $message)) {
97                return false;
98            }
99    
100            $this->notifyAll(array('priority' => $priority, 'message' => $message));
101    
102            return true;
103        }
104    
105        /**
106         * Converts a PEAR_LOG_* constant into a syslog LOG_* constant.
107         *
108         * This function exists because, under Windows, not all of the LOG_*
109         * constants have unique values.  Instead, the PEAR_LOG_* were introduced
110         * for global use, with the conversion to the LOG_* constants kept local to
111         * to the syslog driver.
112         *
113         * @param int $priority     PEAR_LOG_* value to convert to LOG_* value.
114         *
115         * @return  The LOG_* representation of $priority.
116         *
117         * @access private
118         */
119        function _toSyslog($priority)
120        {
121            static $priorities = array(
122                PEAR_LOG_EMERG   => LOG_EMERG,
123                PEAR_LOG_ALERT   => LOG_ALERT,
124                PEAR_LOG_CRIT    => LOG_CRIT,
125                PEAR_LOG_ERR     => LOG_ERR,
126                PEAR_LOG_WARNING => LOG_WARNING,
127                PEAR_LOG_NOTICE  => LOG_NOTICE,
128                PEAR_LOG_INFO    => LOG_INFO,
129                PEAR_LOG_DEBUG   => LOG_DEBUG
130            );
131    
132            return $priorities[$priority];
133        }
134    
135    }
136    ?>

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed