PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/php/Log/sql.php

https://bitbucket.org/adarshj/convenient_website
PHP | 225 lines | 79 code | 28 blank | 118 comment | 13 complexity | 08c9fc2c5b46b36311c76830a75899b6 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * $Header: /repository/pear/Log/Log/sql.php,v 1.34 2004/08/19 06:35:57 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.34 $
  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. * String holding the name of the ID sequence.
  61. * @var string
  62. * @access private
  63. */
  64. var $_sequence = 'log_id';
  65. /**
  66. * Maximum length of the $ident string. This corresponds to the size of
  67. * the 'ident' column in the SQL table.
  68. * @var integer
  69. * @access private
  70. */
  71. var $_identLimit = 16;
  72. /**
  73. * Constructs a new sql logging object.
  74. *
  75. * @param string $name The target SQL table.
  76. * @param string $ident The identification field.
  77. * @param array $conf The connection configuration array.
  78. * @param int $level Log messages up to and including this level.
  79. * @access public
  80. */
  81. function Log_sql($name, $ident = '', $conf = array(),
  82. $level = PEAR_LOG_DEBUG)
  83. {
  84. $this->_id = md5(microtime());
  85. $this->_table = $name;
  86. $this->_mask = Log::UPTO($level);
  87. /* If a specific sequence name was provided, use it. */
  88. if (!empty($conf['sequence'])) {
  89. $this->_sequence = $conf['sequence'];
  90. }
  91. /* If a specific sequence name was provided, use it. */
  92. if (isset($conf['identLimit'])) {
  93. $this->_identLimit = $conf['identLimit'];
  94. }
  95. /* Now that the ident limit is confirmed, set the ident string. */
  96. $this->setIdent($ident);
  97. /* If an existing database connection was provided, use it. */
  98. if (isset($conf['db'])) {
  99. $this->_db = &$conf['db'];
  100. $this->_existingConnection = true;
  101. $this->_opened = true;
  102. } else {
  103. $this->_dsn = $conf['dsn'];
  104. }
  105. }
  106. /**
  107. * Opens a connection to the database, if it has not already
  108. * been opened. This is implicitly called by log(), if necessary.
  109. *
  110. * @return boolean True on success, false on failure.
  111. * @access public
  112. */
  113. function open()
  114. {
  115. if (!$this->_opened) {
  116. $this->_db = &DB::connect($this->_dsn, true);
  117. if (DB::isError($this->_db)) {
  118. return false;
  119. }
  120. $this->_opened = true;
  121. }
  122. return $this->_opened;
  123. }
  124. /**
  125. * Closes the connection to the database if it is still open and we were
  126. * the ones that opened it. It is the caller's responsible to close an
  127. * existing connection that was passed to us via $conf['db'].
  128. *
  129. * @return boolean True on success, false on failure.
  130. * @access public
  131. */
  132. function close()
  133. {
  134. if ($this->_opened && !$this->_existingConnection) {
  135. $this->_opened = false;
  136. return $this->_db->disconnect();
  137. }
  138. return ($this->_opened === false);
  139. }
  140. /**
  141. * Sets this Log instance's identification string. Note that this
  142. * SQL-specific implementation will limit the length of the $ident string
  143. * to sixteen (16) characters.
  144. *
  145. * @param string $ident The new identification string.
  146. *
  147. * @access public
  148. * @since Log 1.8.5
  149. */
  150. function setIdent($ident)
  151. {
  152. $this->_ident = substr($ident, 0, $this->_identLimit);
  153. }
  154. /**
  155. * Inserts $message to the currently open database. Calls open(),
  156. * if necessary. Also passes the message along to any Log_observer
  157. * instances that are observing this Log.
  158. *
  159. * @param mixed $message String or object containing the message to log.
  160. * @param string $priority The priority of the message. Valid
  161. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  162. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  163. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  164. * @return boolean True on success or false on failure.
  165. * @access public
  166. */
  167. function log($message, $priority = null)
  168. {
  169. /* If a priority hasn't been specified, use the default value. */
  170. if ($priority === null) {
  171. $priority = $this->_priority;
  172. }
  173. /* Abort early if the priority is above the maximum logging level. */
  174. if (!$this->_isMasked($priority)) {
  175. return false;
  176. }
  177. /* If the connection isn't open and can't be opened, return failure. */
  178. if (!$this->_opened && !$this->open()) {
  179. return false;
  180. }
  181. /* Extract the string representation of the message. */
  182. $message = $this->_extractMessage($message);
  183. /* Build the SQL query for this log entry insertion. */
  184. $id = $this->_db->nextId($this->_sequence);
  185. $q = sprintf('insert into %s (id, logtime, ident, priority, message)' .
  186. 'values(%d, CURRENT_TIMESTAMP, %s, %d, %s)',
  187. $this->_table, $id, $this->_db->quote($this->_ident),
  188. $priority, $this->_db->quote($message));
  189. $result = $this->_db->query($q);
  190. if (DB::isError($result)) {
  191. return false;
  192. }
  193. $this->_announce(array('priority' => $priority, 'message' => $message));
  194. return true;
  195. }
  196. }
  197. ?>