1 |
<?php |
2 |
// $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $ |
3 |
|
4 |
require_once 'DB.php'; |
5 |
|
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 |
9 |
* occupies a separate row in the database. |
10 |
* |
11 |
* This implementation uses PHP's PEAR database abstraction layer. |
12 |
* |
13 |
* CREATE TABLE log_table ( |
14 |
* unixtime int NOT NULL, |
15 |
* ident char(16) NOT NULL, |
16 |
* priority int, |
17 |
* message varchar(200), |
18 |
* primary key (unixtime, ident) |
19 |
* ); |
20 |
* |
21 |
* @author Jon Parise <jon@csh.rit.edu> |
22 |
* @version $Revision: 1.1 $ |
23 |
* @since Horde 1.3 |
24 |
*/ |
25 |
class Log_sql extends Log { |
26 |
|
27 |
// {{{ properties |
28 |
|
29 |
/** Array containing the dsn information. */ |
30 |
var $dsn = ''; |
31 |
|
32 |
/** Object holding the database handle. */ |
33 |
var $db = ''; |
34 |
|
35 |
/** String holding the database table to use. */ |
36 |
var $table = 'log_table'; |
37 |
|
38 |
/** Boolean indicating the current connection state. */ |
39 |
var $opened = false; |
40 |
|
41 |
// }}} |
42 |
|
43 |
// {{{ constructor |
44 |
/** |
45 |
* Constructs a new sql logging object. |
46 |
* |
47 |
* @param $log_name The target SQL table. |
48 |
* @param $ident (optional) The identification field. |
49 |
* @param $conf The connection configuration array. |
50 |
*/ |
51 |
function Log_sql($log_name, $ident = '', $conf) |
52 |
{ |
53 |
$this->table = $log_name; |
54 |
$this->ident = $ident; |
55 |
$this->dsn = $conf['dsn']; |
56 |
} |
57 |
// }}} |
58 |
|
59 |
// {{{ open() |
60 |
/** |
61 |
* Opens a connection to the database, if it has not already |
62 |
* been opened. This is implicitly called by log(), if necessary. |
63 |
* |
64 |
* @return True on success, false on failure. |
65 |
*/ |
66 |
function open() |
67 |
{ |
68 |
if (!$this->opened) { |
69 |
$this->db = &DB::connect($this->dsn, true); |
70 |
if (DB::isError($this->db) || DB::isWarning($this->db)) { |
71 |
return false; |
72 |
} |
73 |
$this->opened = true; |
74 |
} |
75 |
return true; |
76 |
} |
77 |
// }}} |
78 |
|
79 |
// {{{ close() |
80 |
/** |
81 |
* Closes the connection to the database, if it is open. |
82 |
* |
83 |
* @return True on success, false on failure. |
84 |
*/ |
85 |
function close() |
86 |
{ |
87 |
if ($this->opened) { |
88 |
$this->opened = false; |
89 |
return $this->db->disconnect(); |
90 |
} |
91 |
return true; |
92 |
} |
93 |
// }}} |
94 |
|
95 |
// {{{ log() |
96 |
/** |
97 |
* Inserts $message to the currently open database. Calls open(), |
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. |
102 |
* @param $priority (optional) The priority of the message. Valid |
103 |
* values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, |
104 |
* LOG_ERR, * LOG_WARNING, LOG_NOTICE, LOG_INFO, |
105 |
* and LOG_DEBUG. The default is LOG_INFO. |
106 |
*/ |
107 |
function log($message, $priority = LOG_INFO) |
108 |
{ |
109 |
if (!$this->opened) $this->open(); |
110 |
|
111 |
$timestamp = time(); |
112 |
$q = "insert into $this->table |
113 |
values($timestamp, '$this->ident', $priority, '$message')"; |
114 |
$this->db->query($q); |
115 |
$this->notifyAll(array('priority' => $priority, 'message' => $message)); |
116 |
} |
117 |
// }}} |
118 |
} |
119 |
|
120 |
?> |