ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
sql.php
Go to the documentation of this file.
1<?php
16if (!class_exists('DB')) {
17 require_once 'DB.php';
18}
19
43class Log_sql extends Log
44{
50 var $_dsn = '';
51
58 var $_sql = '';
59
65 var $_options = array('persistent' => true);
66
72 var $_db = null;
73
79 var $_statement = null;
80
87
93 var $_table = 'log_table';
94
100 var $_sequence = 'log_id';
101
108 var $_identLimit = 16;
109
110
120 function Log_sql($name, $ident = '', $conf = array(),
121 $level = PEAR_LOG_DEBUG)
122 {
123 $this->_id = md5(microtime());
124 $this->_table = $name;
125 $this->_mask = Log::UPTO($level);
126
127 /* Now that we have a table name, assign our SQL statement. */
128 if (!empty($conf['sql'])) {
129 $this->_sql = $conf['sql'];
130 } else {
131 $this->_sql = 'INSERT INTO ' . $this->_table .
132 ' (id, logtime, ident, priority, message)' .
133 ' VALUES(?, CURRENT_TIMESTAMP, ?, ?, ?)';
134 }
135
136 /* If an options array was provided, use it. */
137 if (isset($conf['options']) && is_array($conf['options'])) {
138 $this->_options = $conf['options'];
139 }
140
141 /* If a specific sequence name was provided, use it. */
142 if (!empty($conf['sequence'])) {
143 $this->_sequence = $conf['sequence'];
144 }
145
146 /* If a specific sequence name was provided, use it. */
147 if (isset($conf['identLimit'])) {
148 $this->_identLimit = $conf['identLimit'];
149 }
150
151 /* Now that the ident limit is confirmed, set the ident string. */
152 $this->setIdent($ident);
153
154 /* If an existing database connection was provided, use it. */
155 if (isset($conf['db'])) {
156 $this->_db = &$conf['db'];
157 $this->_existingConnection = true;
158 $this->_opened = true;
159 } else {
160 $this->_dsn = $conf['dsn'];
161 }
162 }
163
171 function open()
172 {
173 if (!$this->_opened) {
174 /* Use the DSN and options to create a database connection. */
175 $this->_db = &DB::connect($this->_dsn, $this->_options);
176 if (DB::isError($this->_db)) {
177 return false;
178 }
179
180 /* Create a prepared statement for repeated use in log(). */
181 if (!$this->_prepareStatement()) {
182 return false;
183 }
184
185 /* We now consider out connection open. */
186 $this->_opened = true;
187 }
188
189 return $this->_opened;
190 }
191
200 function close()
201 {
202 if ($this->_opened && !$this->_existingConnection) {
203 $this->_opened = false;
204 $this->_db->freePrepared($this->_statement);
205 return $this->_db->disconnect();
206 }
207
208 return ($this->_opened === false);
209 }
210
221 function setIdent($ident)
222 {
223 $this->_ident = substr($ident, 0, $this->_identLimit);
224 }
225
239 function log($message, $priority = null)
240 {
241 /* If a priority hasn't been specified, use the default value. */
242 if ($priority === null) {
243 $priority = $this->_priority;
244 }
245
246 /* Abort early if the priority is above the maximum logging level. */
247 if (!$this->_isMasked($priority)) {
248 return false;
249 }
250
251 /* If the connection isn't open and can't be opened, return failure. */
252 if (!$this->_opened && !$this->open()) {
253 return false;
254 }
255
256 /* If we don't already have our statement object yet, create it. */
257 if (!is_object($this->_statement) && !$this->_prepareStatement()) {
258 return false;
259 }
260
261 /* Extract the string representation of the message. */
262 $message = $this->_extractMessage($message);
263
264 /* Build our set of values for this log entry. */
265 $id = $this->_db->nextId($this->_sequence);
266 $values = array($id, $this->_ident, $priority, $message);
267
268 /* Execute the SQL query for this log entry insertion. */
269 $result =& $this->_db->execute($this->_statement, $values);
270 if (DB::isError($result)) {
271 return false;
272 }
273
274 $this->_announce(array('priority' => $priority, 'message' => $message));
275
276 return true;
277 }
278
288 {
289 $this->_statement = $this->_db->prepare($this->_sql);
290
291 /* Return success if we didn't generate an error. */
292 return (DB::isError($this->_statement) === false);
293 }
294}
$result
const PEAR_LOG_DEBUG
Definition: Log.php:17
Definition: sql.php:44
$_table
Definition: sql.php:93
$_sql
Definition: sql.php:58
setIdent($ident)
Sets this Log instance's identification string.
Definition: sql.php:221
Log_sql($name, $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
Constructs a new sql logging object.
Definition: sql.php:120
$_db
Definition: sql.php:72
_prepareStatement()
Prepare the SQL insertion statement.
Definition: sql.php:287
$_existingConnection
Definition: sql.php:86
$_statement
Definition: sql.php:79
$_options
Definition: sql.php:65
open()
Opens a connection to the database, if it has not already been opened.
Definition: sql.php:171
$_identLimit
Definition: sql.php:108
close()
Closes the connection to the database if it is still open and we were the ones that opened it.
Definition: sql.php:200
log($message, $priority=null)
Inserts $message to the currently open database.
Definition: sql.php:239
$_dsn
Definition: sql.php:50
$_sequence
Definition: sql.php:100
UPTO($priority)
Calculate the log mask for all priorities up to the given priority.
Definition: Log.php:642
$_priority
Definition: Log.php:69
_isMasked($priority)
Check if the given priority is included in the current level mask.
Definition: Log.php:726
$_opened
Definition: Log.php:45
_announce($event)
Informs each registered observer instance that a new message has been logged.
Definition: Log.php:811
_extractMessage($message)
Returns the string representation of the message data.
Definition: Log.php:417
The Log:: class implements both an abstraction for various logging mechanisms and the Subject end of ...