LoggerAppenderDb.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. * @subpackage appenders
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
  24. require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
  25. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  26. require_once('DB.php');
  27. /**
  28. * Appends log events to a db table using PEAR::DB class.
  29. *
  30. * <p>This appender uses a table in a database to log events.</p>
  31. * <p>Parameters are {@link $dsn}, {@link $createTable}, {@link table} and {@link $sql}.</p>
  32. * <p>See examples in test directory.</p>
  33. *
  34. * @author VxR <vxr@vxr.it>
  35. * @version $Revision: 1.1 $
  36. * @package log4php
  37. * @subpackage appenders
  38. * @since 0.3
  39. */
  40. class LoggerAppenderDb extends LoggerAppenderSkeleton {
  41. /**
  42. * Create the log table if it does not exists (optional).
  43. * @var boolean
  44. */
  45. var $createTable = true;
  46. /**
  47. * PEAR::Db Data source name. Read PEAR::Db for dsn syntax (mandatory).
  48. * @var string
  49. */
  50. var $dsn;
  51. /**
  52. * A {@link LoggerPatternLayout} string used to format a valid insert query (mandatory).
  53. * @var string
  54. */
  55. var $sql;
  56. /**
  57. * Table name to write events. Used only if {@link $createTable} is true.
  58. * @var string
  59. */
  60. var $table;
  61. /**
  62. * @var object PEAR::Db instance
  63. * @access private
  64. */
  65. var $db = null;
  66. /**
  67. * @var boolean used to check if all conditions to append are true
  68. * @access private
  69. */
  70. var $canAppend = true;
  71. /**
  72. * @access private
  73. */
  74. var $requiresLayout = false;
  75. /**
  76. * Constructor.
  77. *
  78. * @param string $name appender name
  79. */
  80. function LoggerAppenderDb($name)
  81. {
  82. $this->LoggerAppenderSkeleton($name);
  83. }
  84. /**
  85. * Setup db connection.
  86. * Based on defined options, this method connects to db defined in {@link $dsn}
  87. * and creates a {@link $table} table if {@link $createTable} is true.
  88. * @return boolean true if all ok.
  89. */
  90. function activateOptions()
  91. {
  92. $this->db = DB::connect($this->dsn);
  93. if (DB::isError($this->db)) {
  94. LoggerLog::debug("LoggerAppenderDb::activateOptions() DB Connect Error [".$this->db->getMessage()."]");
  95. $this->db = null;
  96. $this->closed = true;
  97. $this->canAppend = false;
  98. } else {
  99. $this->layout = LoggerLayout::factory('LoggerPatternLayout');
  100. $this->layout->setConversionPattern($this->getSql());
  101. // test if log table exists
  102. $tableInfo = $this->db->tableInfo($this->table, $mode = null);
  103. if (DB::isError($tableInfo) and $this->getCreateTable()) {
  104. $query = "CREATE TABLE {$this->table} (timestamp varchar(32),logger varchar(32),level varchar(32),message varchar(64),thread varchar(32),file varchar(64),line varchar(4) );";
  105. LoggerLog::debug("LoggerAppenderDb::activateOptions() creating table '{$this->table}'... using sql='$query'");
  106. $result = $this->db->query($query);
  107. if (DB::isError($result)) {
  108. LoggerLog::debug("LoggerAppenderDb::activateOptions() error while creating '{$this->table}'. Error is ".$result->getMessage());
  109. $this->canAppend = false;
  110. return;
  111. }
  112. }
  113. $this->canAppend = true;
  114. }
  115. }
  116. function append($event)
  117. {
  118. if ($this->canAppend) {
  119. $query = $this->layout->format($event);
  120. LoggerLog::debug("LoggerAppenderDb::append() query='$query'");
  121. $this->db->query($query);
  122. }
  123. }
  124. function close()
  125. {
  126. if ($this->db !== null)
  127. $this->db->disconnect();
  128. $this->closed = true;
  129. }
  130. /**
  131. * @return boolean
  132. */
  133. function getCreateTable()
  134. {
  135. return $this->createTable;
  136. }
  137. /**
  138. * @return string the defined dsn
  139. */
  140. function getDsn()
  141. {
  142. return $this->dsn;
  143. }
  144. /**
  145. * @return string the sql pattern string
  146. */
  147. function getSql()
  148. {
  149. return $this->sql;
  150. }
  151. /**
  152. * @return string the table name to create
  153. */
  154. function getTable()
  155. {
  156. return $this->table;
  157. }
  158. function setCreateTable($flag)
  159. {
  160. $this->createTable = LoggerOptionConverter::toBoolean($flag, true);
  161. }
  162. function setDsn($newDsn)
  163. {
  164. $this->dsn = $newDsn;
  165. }
  166. function setSql($sql)
  167. {
  168. $this->sql = $sql;
  169. }
  170. function setTable($table)
  171. {
  172. $this->table = $table;
  173. }
  174. }
  175. ?>