sql.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * $Header: /repository/pear/Log/Log/sql.php,v 1.29 2004/01/19 08:02:40 jon Exp $
  4. * $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $
  5. *
  6. * @version $Revision: 1.29 $
  7. * @package Log
  8. */
  9. /** PEAR's DB package */
  10. require_once 'DB.php';
  11. /**
  12. * The Log_sql class is a concrete implementation of the Log::
  13. * abstract class which sends messages to an SQL server. Each entry
  14. * occupies a separate row in the database.
  15. *
  16. * This implementation uses PHP's PEAR database abstraction layer.
  17. *
  18. * CREATE TABLE log_table (
  19. * id INT NOT NULL,
  20. * logtime TIMESTAMP NOT NULL,
  21. * ident CHAR(16) NOT NULL,
  22. * priority INT NOT NULL,
  23. * message VARCHAR(200),
  24. * PRIMARY KEY (id)
  25. * );
  26. *
  27. * @author Jon Parise <jon@php.net>
  28. * @since Horde 1.3
  29. * @since Log 1.0
  30. * @package Log
  31. *
  32. * @example sql.php Using the SQL handler.
  33. */
  34. class Log_sql extends Log {
  35. /**
  36. * Array containing the dsn information.
  37. * @var string
  38. * @access private
  39. */
  40. var $_dsn = '';
  41. /**
  42. * Object holding the database handle.
  43. * @var object
  44. * @access private
  45. */
  46. var $_db = null;
  47. /**
  48. * Flag indicating that we're using an existing database connection.
  49. * @var boolean
  50. * @access private
  51. */
  52. var $_existingConnection = false;
  53. /**
  54. * String holding the database table to use.
  55. * @var string
  56. * @access private
  57. */
  58. var $_table = 'log_table';
  59. /**
  60. * Constructs a new sql logging object.
  61. *
  62. * @param string $name The target SQL table.
  63. * @param string $ident The identification field.
  64. * @param array $conf The connection configuration array.
  65. * @param int $level Log messages up to and including this level.
  66. * @access public
  67. */
  68. function Log_sql($name, $ident = '', $conf = array(),
  69. $level = PEAR_LOG_DEBUG)
  70. {
  71. $this->_id = md5(microtime());
  72. $this->_table = $name;
  73. $this->_ident = $ident;
  74. $this->_mask = Log::UPTO($level);
  75. /* If an existing database connection was provided, use it. */
  76. if (isset($conf['db'])) {
  77. $this->_db = &$conf['db'];
  78. $this->_existingConnection = true;
  79. $this->_opened = true;
  80. } else {
  81. $this->_dsn = $conf['dsn'];
  82. }
  83. }
  84. /**
  85. * Opens a connection to the database, if it has not already
  86. * been opened. This is implicitly called by log(), if necessary.
  87. *
  88. * @return boolean True on success, false on failure.
  89. * @access public
  90. */
  91. function open()
  92. {
  93. if (!$this->_opened) {
  94. $this->_db = &DB::connect($this->_dsn, true);
  95. if (DB::isError($this->_db)) {
  96. return false;
  97. }
  98. $this->_opened = true;
  99. }
  100. return $this->_opened;
  101. }
  102. /**
  103. * Closes the connection to the database if it is still open and we were
  104. * the ones that opened it. It is the caller's responsible to close an
  105. * existing connection that was passed to us via $conf['db'].
  106. *
  107. * @return boolean True on success, false on failure.
  108. * @access public
  109. */
  110. function close()
  111. {
  112. if ($this->_opened && !$this->_existingConnection) {
  113. $this->_opened = false;
  114. return $this->_db->disconnect();
  115. }
  116. return ($this->_opened === false);
  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 mixed $message String or object containing the message to log.
  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. * @return boolean True on success or false on failure.
  129. * @access public
  130. */
  131. function log($message, $priority = null)
  132. {
  133. /* If a priority hasn't been specified, use the default value. */
  134. if ($priority === null) {
  135. $priority = $this->_priority;
  136. }
  137. /* Abort early if the priority is above the maximum logging level. */
  138. if (!$this->_isMasked($priority)) {
  139. return false;
  140. }
  141. /* If the connection isn't open and can't be opened, return failure. */
  142. if (!$this->_opened && !$this->open()) {
  143. return false;
  144. }
  145. /* Extract the string representation of the message. */
  146. $message = $this->_extractMessage($message);
  147. /* Build the SQL query for this log entry insertion. */
  148. $id = $this->_db->nextId('log_id');
  149. $q = sprintf('insert into %s (id, logtime, ident, priority, message)' .
  150. 'values(%d, CURRENT_TIMESTAMP, %s, %d, %s)',
  151. $this->_table, $id, $this->_db->quote($this->_ident),
  152. $priority, $this->_db->quote($message));
  153. $result = $this->_db->query($q);
  154. if (DB::isError($result)) {
  155. return false;
  156. }
  157. $this->_announce(array('priority' => $priority, 'message' => $message));
  158. return true;
  159. }
  160. }
  161. ?>