ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
Log_mdb2 Class Reference
+ Inheritance diagram for Log_mdb2:
+ Collaboration diagram for Log_mdb2:

Public Member Functions

 Log_mdb2 ($name, $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
 Constructs a new sql logging object. More...
 
 open ()
 Opens a connection to the database, if it has not already been opened. More...
 
 close ()
 Closes the connection to the database if it is still open and we were the ones that opened it. More...
 
 setIdent ($ident)
 Sets this Log instance's identification string. More...
 
 log ($message, $priority=null)
 Inserts $message to the currently open database. More...
 
 _createTable ()
 Create the log table in the database. More...
 
 _prepareStatement ()
 Prepare the SQL insertion statement. More...
 
- Public Member Functions inherited from Log
 _classExists ($class)
 Utility function which wraps PHP's class_exists() function to ensure consistent behavior between PHP versions 4 and 5. More...
 
factory ($handler, $name='', $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
 Attempts to return a concrete Log instance of type $handler. More...
 
singleton ($handler, $name='', $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
 Attempts to return a reference to a concrete Log instance of type $handler, only creating a new instance if no log instance with the same parameters currently exists. More...
 
 open ()
 Abstract implementation of the open() method. More...
 
 close ()
 Abstract implementation of the close() method. More...
 
 flush ()
 Abstract implementation of the flush() method. More...
 
 log ($message, $priority=null)
 Abstract implementation of the log() method. More...
 
 emerg ($message)
 A convenience function for logging a emergency event. More...
 
 alert ($message)
 A convenience function for logging an alert event. More...
 
 crit ($message)
 A convenience function for logging a critical event. More...
 
 err ($message)
 A convenience function for logging a error event. More...
 
 warning ($message)
 A convenience function for logging a warning event. More...
 
 notice ($message)
 A convenience function for logging a notice event. More...
 
 info ($message)
 A convenience function for logging a information event. More...
 
 debug ($message)
 A convenience function for logging a debug event. More...
 
 _extractMessage ($message)
 Returns the string representation of the message data. More...
 
 _getBacktraceVars ($depth)
 Using debug_backtrace(), returns the file, line, and enclosing function name of the source code context from which log() was invoked. More...
 
 _format ($format, $timestamp, $priority, $message)
 Produces a formatted log line based on a format string and a set of variables representing the current log record and state. More...
 
 priorityToString ($priority)
 Returns the string representation of a PEAR_LOG_* integer constant. More...
 
 stringToPriority ($name)
 Returns the the PEAR_LOG_* integer constant for the given string representation of a priority name. More...
 
 MASK ($priority)
 Calculate the log mask for the given priority. More...
 
 UPTO ($priority)
 Calculate the log mask for all priorities up to the given priority. More...
 
 MIN ($priority)
 Calculate the log mask for all priorities greater than or equal to the given priority. More...
 
 MAX ($priority)
 Calculate the log mask for all priorities less than or equal to the given priority. More...
 
 setMask ($mask)
 Set and return the level mask for the current Log instance. More...
 
 getMask ()
 Returns the current level mask. More...
 
 _isMasked ($priority)
 Check if the given priority is included in the current level mask. More...
 
 getPriority ()
 Returns the current default priority. More...
 
 setPriority ($priority)
 Sets the default priority to the specified value. More...
 
 attach (&$observer)
 Adds a Log_observer instance to the list of observers that are listening for messages emitted by this Log instance. More...
 
 detach ($observer)
 Removes a Log_observer instance from the list of observers. More...
 
 _announce ($event)
 Informs each registered observer instance that a new message has been logged. More...
 
 isComposite ()
 Indicates whether this is a composite class. More...
 
 setIdent ($ident)
 Sets this Log instance's identification string. More...
 
 getIdent ()
 Returns the current identification string. More...
 

Data Fields

 $_dsn = ''
 
 $_options = array('persistent' => true)
 
 $_db = null
 
 $_statement = null
 
 $_existingConnection = false
 
 $_table = 'log_table'
 
 $_sequence = 'log_id'
 
 $_identLimit = 16
 
 $_types
 
- Data Fields inherited from Log
 $_opened = false
 
 $_id = 0
 
 $_ident = ''
 
 $_priority = PEAR_LOG_INFO
 
 $_mask = PEAR_LOG_ALL
 
 $_listeners = array()
 
 $_formatMap
 

Detailed Description

Definition at line 34 of file mdb2.php.

Member Function Documentation

◆ _createTable()

Log_mdb2::_createTable ( )

Create the log table in the database.

Returns
boolean True on success or false on failure. private

Definition at line 310 of file mdb2.php.

References $result, and PEAR\isError().

Referenced by log().

311  {
312  $this->_db->loadModule('Manager', null, true);
313  $result = $this->_db->manager->createTable(
314  $this->_table,
315  array(
316  'id' => array('type' => $this->_types['id']),
317  'logtime' => array('type' => $this->_types['logtime']),
318  'ident' => array('type' => $this->_types['ident']),
319  'priority' => array('type' => $this->_types['priority']),
320  'message' => array('type' => $this->_types['message'])
321  )
322  );
323  if (PEAR::isError($result)) {
324  return false;
325  }
326 
327  $result = $this->_db->manager->createIndex(
328  $this->_table,
329  'unique_id',
330  array('fields' => array('id' => true), 'unique' => true)
331  );
332  if (PEAR::isError($result)) {
333  return false;
334  }
335 
336  return true;
337  }
$result
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _prepareStatement()

Log_mdb2::_prepareStatement ( )

Prepare the SQL insertion statement.

Returns
boolean True if the statement was successfully created.

private

Since
Log 1.9.0

Definition at line 347 of file mdb2.php.

References PEAR\isError(), and MDB2_PREPARE_MANIP.

Referenced by log(), and open().

348  {
349  $this->_statement = &$this->_db->prepare(
350  'INSERT INTO ' . $this->_table .
351  ' (id, logtime, ident, priority, message)' .
352  ' VALUES(:id, :logtime, :ident, :priority, :message)',
353  $this->_types, MDB2_PREPARE_MANIP);
354 
355  /* Return success if we didn't generate an error. */
356  return (PEAR::isError($this->_statement) === false);
357  }
const MDB2_PREPARE_MANIP
These are just helper constants to more verbosely express parameters to prepare() ...
Definition: MDB2.php:109
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ close()

Log_mdb2::close ( )

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'].

Returns
boolean True on success, false on failure. public

Definition at line 190 of file mdb2.php.

191  {
192  /* If we have a statement object, free it. */
193  if (is_object($this->_statement)) {
194  $this->_statement->free();
195  $this->_statement = null;
196  }
197 
198  /* If we opened the database connection, disconnect it. */
199  if ($this->_opened && !$this->_existingConnection) {
200  $this->_opened = false;
201  return $this->_db->disconnect();
202  }
203 
204  return ($this->_opened === false);
205  }

◆ log()

Log_mdb2::log (   $message,
  $priority = null 
)

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.

Parameters
mixed$messageString or object containing the message to log.
string$priorityThe 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.
Returns
boolean True on success or false on failure. public

Definition at line 235 of file mdb2.php.

References Log\$_ident, Log\$_priority, $result, Log\_announce(), _createTable(), Log\_extractMessage(), Log\_isMasked(), _prepareStatement(), PEAR\isError(), MDB2_ERROR_NOSUCHTABLE, MDB2_Date\mdbNow(), and open().

236  {
237  /* If a priority hasn't been specified, use the default value. */
238  if ($priority === null) {
239  $priority = $this->_priority;
240  }
241 
242  /* Abort early if the priority is above the maximum logging level. */
243  if (!$this->_isMasked($priority)) {
244  return false;
245  }
246 
247  /* If the connection isn't open and can't be opened, return failure. */
248  if (!$this->_opened && !$this->open()) {
249  return false;
250  }
251 
252  /* If we don't already have a statement object, create one. */
253  if (!is_object($this->_statement) && !$this->_prepareStatement()) {
254  return false;
255  }
256 
257  /* Extract the string representation of the message. */
258  $message = $this->_extractMessage($message);
259 
260  /* Build our set of values for this log entry. */
261  $values = array(
262  'id' => $this->_db->nextId($this->_sequence),
263  'logtime' => MDB2_Date::mdbNow(),
264  'ident' => $this->_ident,
265  'priority' => $priority,
266  'message' => $message
267  );
268 
269  /* Execute the SQL query for this log entry insertion. */
270  $this->_db->expectError(MDB2_ERROR_NOSUCHTABLE);
271  $result = &$this->_statement->execute($values);
272  $this->_db->popExpect();
273 
274  /* Attempt to handle any errors. */
275  if (PEAR::isError($result)) {
276  /* We can only handle MDB2_ERROR_NOSUCHTABLE errors. */
277  if ($result->getCode() != MDB2_ERROR_NOSUCHTABLE) {
278  return false;
279  }
280 
281  /* Attempt to create the target table. */
282  if (!$this->_createTable()) {
283  return false;
284  }
285 
286  /* Recreate our prepared statement resource. */
287  $this->_statement->free();
288  if (!$this->_prepareStatement()) {
289  return false;
290  }
291 
292  /* Attempt to re-execute the insertion query. */
293  $result = $this->_statement->execute($values);
294  if (PEAR::isError($result)) {
295  return false;
296  }
297  }
298 
299  $this->_announce(array('priority' => $priority, 'message' => $message));
300 
301  return true;
302  }
_announce($event)
Informs each registered observer instance that a new message has been logged.
Definition: Log.php:811
$result
open()
Opens a connection to the database, if it has not already been opened.
Definition: mdb2.php:161
_createTable()
Create the log table in the database.
Definition: mdb2.php:310
_prepareStatement()
Prepare the SQL insertion statement.
Definition: mdb2.php:347
$_priority
Definition: Log.php:69
_isMasked($priority)
Check if the given priority is included in the current level mask.
Definition: Log.php:726
_extractMessage($message)
Returns the string representation of the message data.
Definition: Log.php:417
$_ident
Definition: Log.php:61
mdbNow()
return the current datetime
Definition: Date.php:73
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
const MDB2_ERROR_NOSUCHTABLE
Definition: MDB2.php:85
+ Here is the call graph for this function:

◆ Log_mdb2()

Log_mdb2::Log_mdb2 (   $name,
  $ident = '',
  $conf = array(),
  $level = PEAR_LOG_DEBUG 
)

Constructs a new sql logging object.

Parameters
string$nameThe target SQL table.
string$identThe identification field.
array$confThe connection configuration array.
int$levelLog messages up to and including this level. public

Definition at line 115 of file mdb2.php.

References setIdent(), MDB2\singleton(), and Log\UPTO().

117  {
118  $this->_id = md5(microtime());
119  $this->_table = $name;
120  $this->_mask = Log::UPTO($level);
121 
122  /* If an options array was provided, use it. */
123  if (isset($conf['options']) && is_array($conf['options'])) {
124  $this->_options = $conf['options'];
125  }
126 
127  /* If a specific sequence name was provided, use it. */
128  if (!empty($conf['sequence'])) {
129  $this->_sequence = $conf['sequence'];
130  }
131 
132  /* If a specific sequence name was provided, use it. */
133  if (isset($conf['identLimit'])) {
134  $this->_identLimit = $conf['identLimit'];
135  }
136 
137  /* Now that the ident limit is confirmed, set the ident string. */
138  $this->setIdent($ident);
139 
140  /* If an existing database connection was provided, use it. */
141  if (isset($conf['db'])) {
142  $this->_db = &$conf['db'];
143  $this->_existingConnection = true;
144  $this->_opened = true;
145  } elseif (isset($conf['singleton'])) {
146  $this->_db = &MDB2::singleton($conf['singleton'], $this->_options);
147  $this->_existingConnection = true;
148  $this->_opened = true;
149  } else {
150  $this->_dsn = $conf['dsn'];
151  }
152  }
UPTO($priority)
Calculate the log mask for all priorities up to the given priority.
Definition: Log.php:642
setIdent($ident)
Sets this Log instance's identification string.
Definition: mdb2.php:217
& singleton($dsn=null, $options=false)
Returns a MDB2 connection with the requested DSN.
Definition: MDB2.php:481
+ Here is the call graph for this function:

◆ open()

Log_mdb2::open ( )

Opens a connection to the database, if it has not already been opened.

This is implicitly called by log(), if necessary.

Returns
boolean True on success, false on failure. public

Definition at line 161 of file mdb2.php.

References Log\$_opened, _prepareStatement(), MDB2\connect(), and PEAR\isError().

Referenced by log().

162  {
163  if (!$this->_opened) {
164  /* Use the DSN and options to create a database connection. */
165  $this->_db = &MDB2::connect($this->_dsn, $this->_options);
166  if (PEAR::isError($this->_db)) {
167  return false;
168  }
169 
170  /* Create a prepared statement for repeated use in log(). */
171  if (!$this->_prepareStatement()) {
172  return false;
173  }
174 
175  /* We now consider out connection open. */
176  $this->_opened = true;
177  }
178 
179  return $this->_opened;
180  }
_prepareStatement()
Prepare the SQL insertion statement.
Definition: mdb2.php:347
& connect($dsn, $options=false)
Create a new MDB2 connection object and connect to the specified database.
Definition: MDB2.php:431
$_opened
Definition: Log.php:45
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setIdent()

Log_mdb2::setIdent (   $ident)

Sets this Log instance's identification string.

Note that this SQL-specific implementation will limit the length of the $ident string to sixteen (16) characters.

Parameters
string$identThe new identification string.

public

Since
Log 1.8.5

Definition at line 217 of file mdb2.php.

Referenced by Log_mdb2().

218  {
219  $this->_ident = substr($ident, 0, $this->_identLimit);
220  }
+ Here is the caller graph for this function:

Field Documentation

◆ $_db

Log_mdb2::$_db = null

Definition at line 55 of file mdb2.php.

◆ $_dsn

Log_mdb2::$_dsn = ''

Definition at line 41 of file mdb2.php.

◆ $_existingConnection

Log_mdb2::$_existingConnection = false

Definition at line 69 of file mdb2.php.

◆ $_identLimit

Log_mdb2::$_identLimit = 16

Definition at line 91 of file mdb2.php.

◆ $_options

Log_mdb2::$_options = array('persistent' => true)

Definition at line 48 of file mdb2.php.

◆ $_sequence

Log_mdb2::$_sequence = 'log_id'

Definition at line 83 of file mdb2.php.

◆ $_statement

Log_mdb2::$_statement = null

Definition at line 62 of file mdb2.php.

◆ $_table

Log_mdb2::$_table = 'log_table'

Definition at line 76 of file mdb2.php.

◆ $_types

Log_mdb2::$_types
Initial value:
= array(
'id' => 'integer',
'logtime' => 'timestamp',
'ident' => 'text',
'priority' => 'text',
'message' => 'clob'
)

Definition at line 98 of file mdb2.php.


The documentation for this class was generated from the following file: