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

Contents of /nfo/php/libs/net.php.pear/Log/sql.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Wed Feb 5 21:48:31 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +157 -120 lines
+ updated to Log-1.5.3

1 <?php
2 // $Id: sql.php,v 1.14 2002/12/02 05:23:00 jon Exp $
3 // $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $
4
5 require_once 'DB.php';
6
7 /**
8 * The Log_sql class is a concrete implementation of the Log::
9 * abstract class which sends messages to an SQL server. Each entry
10 * occupies a separate row in the database.
11 *
12 * This implementation uses PHP's PEAR database abstraction layer.
13 *
14 * CREATE TABLE log_table (
15 * logtime TIMESTAMP NOT NULL,
16 * ident char(16) NOT NULL,
17 * priority int,
18 * message varchar(200),
19 * primary key (logtime, ident)
20 * );
21 *
22 * @author Jon Parise <jon@php.net>
23 * @version $Revision: 1.14 $
24 * @since Horde 1.3
25 * @package Log
26 */
27 class Log_sql extends Log {
28
29 /**
30 * Array containing the dsn information.
31 * @var string
32 */
33 var $_dsn = '';
34
35 /**
36 * Object holding the database handle.
37 * @var string
38 */
39 var $_db = '';
40
41 /**
42 * Flag indicating that we're using an existing database connection.
43 * @var boolean
44 */
45 var $_existingConnection = false;
46
47 /**
48 * String holding the database table to use.
49 * @var string
50 */
51 var $_table = 'log_table';
52
53
54 /**
55 * Constructs a new sql logging object.
56 *
57 * @param string $name The target SQL table.
58 * @param string $ident The identification field.
59 * @param array $conf The connection configuration array.
60 * @param int $maxLevel Maximum level at which to log.
61 * @access public
62 */
63 function Log_sql($name, $ident = '', $conf = array(),
64 $maxLevel = PEAR_LOG_DEBUG)
65 {
66 $this->_table = $name;
67 $this->_ident = $ident;
68 $this->_maxLevel = $maxLevel;
69
70 /* If an existing database connection was provided, use it. */
71 if (isset($conf['db'])) {
72 $this->_db = &$conf['db'];
73 $this->_existingConnection = true;
74 $this->_opened = true;
75 } else {
76 $this->_dsn = $conf['dsn'];
77 }
78 }
79
80 /**
81 * Opens a connection to the database, if it has not already
82 * been opened. This is implicitly called by log(), if necessary.
83 *
84 * @return boolean True on success, false on failure.
85 * @access public
86 */
87 function open()
88 {
89 if (!$this->_opened) {
90 $this->_db = &DB::connect($this->_dsn, true);
91 if (DB::isError($this->_db)) {
92 return false;
93 }
94 $this->_opened = true;
95 }
96
97 return true;
98 }
99
100 /**
101 * Closes the connection to the database if it is still open and we were
102 * the ones that opened it. It is the caller's responsible to close an
103 * existing connection that was passed to us via $conf['db'].
104 *
105 * @return boolean True on success, false on failure.
106 * @access public
107 */
108 function close()
109 {
110 if ($this->_opened && !$this->_existingConnection) {
111 $this->_opened = false;
112 return $this->_db->disconnect();
113 }
114
115 return true;
116 }
117
118 /**
119 * Inserts $message to the currently open database. Calls open(),
120 * if necessary. Also passes the message along to any Log_observer
121 * instances that are observing this Log.
122 *
123 * @param string $message The textual message to be logged.
124 * @param string $priority The priority of the message. Valid
125 * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
126 * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
127 * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
128 * The default is PEAR_LOG_INFO.
129 * @return boolean True on success or false on failure.
130 * @access public
131 */
132 function log($message, $priority = PEAR_LOG_INFO)
133 {
134 /* Abort early if the priority is above the maximum logging level. */
135 if ($priority > $this->_maxLevel) return;
136
137 if (!$this->_opened) {
138 $this->open();
139 }
140
141 /* Build the SQL query for this log entry insertion. */
142 $q = sprintf("insert into %s values(NOW(), %s, %d, %s)",
143 $this->_table, $this->_db->quote($this->_ident),
144 $priority, $this->_db->quote($message));
145
146 $result = $this->_db->query($q);
147 if (DB::isError($result)) {
148 return false;
149 }
150
151 $this->notifyAll(array('priority' => $priority, 'message' => $message));
152
153 return true;
154 }
155 }
156
157 ?>

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