--- nfo/php/libs/net.php.pear/Log/sql.php 2002/10/25 15:15:23 1.1 +++ nfo/php/libs/net.php.pear/Log/sql.php 2003/02/05 21:48:31 1.2 @@ -1,120 +1,157 @@ - - * @version $Revision: 1.1 $ - * @since Horde 1.3 - */ -class Log_sql extends Log { - - // {{{ properties - - /** Array containing the dsn information. */ - var $dsn = ''; - - /** Object holding the database handle. */ - var $db = ''; - - /** String holding the database table to use. */ - var $table = 'log_table'; - - /** Boolean indicating the current connection state. */ - var $opened = false; - - // }}} - - // {{{ constructor - /** - * Constructs a new sql logging object. - * - * @param $log_name The target SQL table. - * @param $ident (optional) The identification field. - * @param $conf The connection configuration array. - */ - function Log_sql($log_name, $ident = '', $conf) - { - $this->table = $log_name; - $this->ident = $ident; - $this->dsn = $conf['dsn']; - } - // }}} - - // {{{ open() - /** - * Opens a connection to the database, if it has not already - * been opened. This is implicitly called by log(), if necessary. - * - * @return True on success, false on failure. - */ - function open() - { - if (!$this->opened) { - $this->db = &DB::connect($this->dsn, true); - if (DB::isError($this->db) || DB::isWarning($this->db)) { - return false; - } - $this->opened = true; - } - return true; - } - // }}} - - // {{{ close() - /** - * Closes the connection to the database, if it is open. - * - * @return True on success, false on failure. - */ - function close() - { - if ($this->opened) { - $this->opened = false; - return $this->db->disconnect(); - } - return true; - } - // }}} - - // {{{ log() - /** - * Inserts $message to the currently open database. Calls open(), - * if necessary. Also passes the message along to any Log_observer - * instances that are observing this Log. - * - * @param $message The textual message to be logged. - * @param $priority (optional) The priority of the message. Valid - * values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, - * LOG_ERR, * LOG_WARNING, LOG_NOTICE, LOG_INFO, - * and LOG_DEBUG. The default is LOG_INFO. - */ - function log($message, $priority = LOG_INFO) - { - if (!$this->opened) $this->open(); - - $timestamp = time(); - $q = "insert into $this->table - values($timestamp, '$this->ident', $priority, '$message')"; - $this->db->query($q); - $this->notifyAll(array('priority' => $priority, 'message' => $message)); - } - // }}} -} - -?> + + * @version $Revision: 1.2 $ + * @since Horde 1.3 + * @package Log + */ +class Log_sql extends Log { + + /** + * Array containing the dsn information. + * @var string + */ + var $_dsn = ''; + + /** + * Object holding the database handle. + * @var string + */ + var $_db = ''; + + /** + * Flag indicating that we're using an existing database connection. + * @var boolean + */ + var $_existingConnection = false; + + /** + * String holding the database table to use. + * @var string + */ + var $_table = 'log_table'; + + + /** + * Constructs a new sql logging object. + * + * @param string $name The target SQL table. + * @param string $ident The identification field. + * @param array $conf The connection configuration array. + * @param int $maxLevel Maximum level at which to log. + * @access public + */ + function Log_sql($name, $ident = '', $conf = array(), + $maxLevel = PEAR_LOG_DEBUG) + { + $this->_table = $name; + $this->_ident = $ident; + $this->_maxLevel = $maxLevel; + + /* If an existing database connection was provided, use it. */ + if (isset($conf['db'])) { + $this->_db = &$conf['db']; + $this->_existingConnection = true; + $this->_opened = true; + } else { + $this->_dsn = $conf['dsn']; + } + } + + /** + * Opens a connection to the database, if it has not already + * been opened. This is implicitly called by log(), if necessary. + * + * @return boolean True on success, false on failure. + * @access public + */ + function open() + { + if (!$this->_opened) { + $this->_db = &DB::connect($this->_dsn, true); + if (DB::isError($this->_db)) { + return false; + } + $this->_opened = true; + } + + return true; + } + + /** + * Closes the connection to the database if it is still open and we were + * the ones that opened it. It is the caller's responsible to close an + * existing connection that was passed to us via $conf['db']. + * + * @return boolean True on success, false on failure. + * @access public + */ + function close() + { + if ($this->_opened && !$this->_existingConnection) { + $this->_opened = false; + return $this->_db->disconnect(); + } + + return true; + } + + /** + * Inserts $message to the currently open database. Calls open(), + * if necessary. Also passes the message along to any Log_observer + * instances that are observing this Log. + * + * @param string $message The textual message to be logged. + * @param string $priority The priority of the message. Valid + * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT, + * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING, + * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG. + * The default is PEAR_LOG_INFO. + * @return boolean True on success or false on failure. + * @access public + */ + function log($message, $priority = PEAR_LOG_INFO) + { + /* Abort early if the priority is above the maximum logging level. */ + if ($priority > $this->_maxLevel) return; + + if (!$this->_opened) { + $this->open(); + } + + /* Build the SQL query for this log entry insertion. */ + $q = sprintf("insert into %s values(NOW(), %s, %d, %s)", + $this->_table, $this->_db->quote($this->_ident), + $priority, $this->_db->quote($message)); + + $result = $this->_db->query($q); + if (DB::isError($result)) { + return false; + } + + $this->notifyAll(array('priority' => $priority, 'message' => $message)); + + return true; + } +} + +?>