1 |
<?php |
<?php |
2 |
// $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $ |
// $Id$ |
3 |
|
// $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $ |
4 |
require_once 'DB.php'; |
|
5 |
|
require_once 'DB.php'; |
6 |
/** |
|
7 |
* The Log_sql class is a concrete implementation of the Log:: |
/** |
8 |
* abstract class which sends messages to an SQL server. Each entry |
* The Log_sql class is a concrete implementation of the Log:: |
9 |
* occupies a separate row in the database. |
* abstract class which sends messages to an SQL server. Each entry |
10 |
* |
* occupies a separate row in the database. |
11 |
* This implementation uses PHP's PEAR database abstraction layer. |
* |
12 |
* |
* This implementation uses PHP's PEAR database abstraction layer. |
13 |
* CREATE TABLE log_table ( |
* |
14 |
* unixtime int NOT NULL, |
* CREATE TABLE log_table ( |
15 |
* ident char(16) NOT NULL, |
* logtime TIMESTAMP NOT NULL, |
16 |
* priority int, |
* ident char(16) NOT NULL, |
17 |
* message varchar(200), |
* priority int, |
18 |
* primary key (unixtime, ident) |
* message varchar(200), |
19 |
* ); |
* primary key (logtime, ident) |
20 |
* |
* ); |
21 |
* @author Jon Parise <jon@csh.rit.edu> |
* |
22 |
* @version $Revision$ |
* @author Jon Parise <jon@php.net> |
23 |
* @since Horde 1.3 |
* @version $Revision$ |
24 |
*/ |
* @since Horde 1.3 |
25 |
class Log_sql extends Log { |
* @package Log |
26 |
|
*/ |
27 |
// {{{ properties |
class Log_sql extends Log { |
28 |
|
|
29 |
/** Array containing the dsn information. */ |
/** |
30 |
var $dsn = ''; |
* Array containing the dsn information. |
31 |
|
* @var string |
32 |
/** Object holding the database handle. */ |
*/ |
33 |
var $db = ''; |
var $_dsn = ''; |
34 |
|
|
35 |
/** String holding the database table to use. */ |
/** |
36 |
var $table = 'log_table'; |
* Object holding the database handle. |
37 |
|
* @var string |
38 |
/** Boolean indicating the current connection state. */ |
*/ |
39 |
var $opened = false; |
var $_db = ''; |
40 |
|
|
41 |
// }}} |
/** |
42 |
|
* Flag indicating that we're using an existing database connection. |
43 |
// {{{ constructor |
* @var boolean |
44 |
/** |
*/ |
45 |
* Constructs a new sql logging object. |
var $_existingConnection = false; |
46 |
* |
|
47 |
* @param $log_name The target SQL table. |
/** |
48 |
* @param $ident (optional) The identification field. |
* String holding the database table to use. |
49 |
* @param $conf The connection configuration array. |
* @var string |
50 |
*/ |
*/ |
51 |
function Log_sql($log_name, $ident = '', $conf) |
var $_table = 'log_table'; |
52 |
{ |
|
53 |
$this->table = $log_name; |
|
54 |
$this->ident = $ident; |
/** |
55 |
$this->dsn = $conf['dsn']; |
* Constructs a new sql logging object. |
56 |
} |
* |
57 |
// }}} |
* @param string $name The target SQL table. |
58 |
|
* @param string $ident The identification field. |
59 |
// {{{ open() |
* @param array $conf The connection configuration array. |
60 |
/** |
* @param int $maxLevel Maximum level at which to log. |
61 |
* Opens a connection to the database, if it has not already |
* @access public |
62 |
* been opened. This is implicitly called by log(), if necessary. |
*/ |
63 |
* |
function Log_sql($name, $ident = '', $conf = array(), |
64 |
* @return True on success, false on failure. |
$maxLevel = PEAR_LOG_DEBUG) |
65 |
*/ |
{ |
66 |
function open() |
$this->_table = $name; |
67 |
{ |
$this->_ident = $ident; |
68 |
if (!$this->opened) { |
$this->_maxLevel = $maxLevel; |
69 |
$this->db = &DB::connect($this->dsn, true); |
|
70 |
if (DB::isError($this->db) || DB::isWarning($this->db)) { |
/* If an existing database connection was provided, use it. */ |
71 |
return false; |
if (isset($conf['db'])) { |
72 |
} |
$this->_db = &$conf['db']; |
73 |
$this->opened = true; |
$this->_existingConnection = true; |
74 |
} |
$this->_opened = true; |
75 |
return true; |
} else { |
76 |
} |
$this->_dsn = $conf['dsn']; |
77 |
// }}} |
} |
78 |
|
} |
79 |
// {{{ close() |
|
80 |
/** |
/** |
81 |
* Closes the connection to the database, if it is open. |
* Opens a connection to the database, if it has not already |
82 |
* |
* been opened. This is implicitly called by log(), if necessary. |
83 |
* @return True on success, false on failure. |
* |
84 |
*/ |
* @return boolean True on success, false on failure. |
85 |
function close() |
* @access public |
86 |
{ |
*/ |
87 |
if ($this->opened) { |
function open() |
88 |
$this->opened = false; |
{ |
89 |
return $this->db->disconnect(); |
if (!$this->_opened) { |
90 |
} |
$this->_db = &DB::connect($this->_dsn, true); |
91 |
return true; |
if (DB::isError($this->_db)) { |
92 |
} |
return false; |
93 |
// }}} |
} |
94 |
|
$this->_opened = true; |
95 |
// {{{ log() |
} |
96 |
/** |
|
97 |
* Inserts $message to the currently open database. Calls open(), |
return true; |
98 |
* if necessary. Also passes the message along to any Log_observer |
} |
99 |
* instances that are observing this Log. |
|
100 |
* |
/** |
101 |
* @param $message The textual message to be logged. |
* Closes the connection to the database if it is still open and we were |
102 |
* @param $priority (optional) The priority of the message. Valid |
* the ones that opened it. It is the caller's responsible to close an |
103 |
* values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, |
* existing connection that was passed to us via $conf['db']. |
104 |
* LOG_ERR, * LOG_WARNING, LOG_NOTICE, LOG_INFO, |
* |
105 |
* and LOG_DEBUG. The default is LOG_INFO. |
* @return boolean True on success, false on failure. |
106 |
*/ |
* @access public |
107 |
function log($message, $priority = LOG_INFO) |
*/ |
108 |
{ |
function close() |
109 |
if (!$this->opened) $this->open(); |
{ |
110 |
|
if ($this->_opened && !$this->_existingConnection) { |
111 |
$timestamp = time(); |
$this->_opened = false; |
112 |
$q = "insert into $this->table |
return $this->_db->disconnect(); |
113 |
values($timestamp, '$this->ident', $priority, '$message')"; |
} |
114 |
$this->db->query($q); |
|
115 |
$this->notifyAll(array('priority' => $priority, 'message' => $message)); |
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 |
|
?> |