/protected/components/ezcomponents/EventLogDatabaseTiein/src/writers/writer_database.php

https://github.com/kamarulismail/kamarul-playground · PHP · 334 lines · 146 code · 22 blank · 166 comment · 6 complexity · be46132396ed8b3556a55f728ec76898 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcLogDatabaseWriter class.
  4. *
  5. * @package EventLogDatabaseTiein
  6. * @version 1.0.2
  7. * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. */
  10. /**
  11. * The ezcLogDatabaseWriter provides an implementation to write log messages to the database.
  12. *
  13. * Example to use the ezcLogDatabaseWriter:
  14. * <code>
  15. * // Get the database instance
  16. * $db = ezcDbInstance::get();
  17. *
  18. * // Get the log instance
  19. * $log = ezcLog::getInstance();
  20. *
  21. * // Create a new ezcLogDatabaseWriter object based on the database instance
  22. * // and with the default table name "log".
  23. * // The "log" table must exist already in the database, and must have a compatible structure,
  24. * // with any additional fields that you may require, eg. you can use this example schema,
  25. * // where the default fields are: id, category, message, severity, source, time
  26. * // and the additional fields are: file, line
  27. * // DROP TABLE IF EXISTS log;
  28. * // CREATE TABLE log (
  29. * // category varchar(255) NOT NULL,
  30. * // file varchar(255),
  31. * // id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY,
  32. * // line bigint,
  33. * // message varchar(255) NOT NULL,
  34. * // severity varchar(255) NOT NULL,
  35. * // source varchar(255) NOT NULL,
  36. * // time timestamp NOT NULL
  37. * // );
  38. * $writer = new ezcLogDatabaseWriter( $db, "log" );
  39. *
  40. * // Specify that log messages will be written to the database
  41. * $log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter, $writer, true ) );
  42. *
  43. * // Write a log entry ( message, severity, source, category )
  44. * $log->log( "File '/images/spacer.gif' does not exist.", ezcLog::WARNING,
  45. * array( "source" => "Application", "category" => "Design" ) );
  46. *
  47. * // Write a log entry ( message, severity, source, category, file, line )
  48. * $log->log( "File '/images/spacer.gif' does not exist.", ezcLog::WARNING,
  49. * array( "source" => "Application", "category" => "Design" ),
  50. * array( "file" => "/index.php", "line" => 123 ) );
  51. * </code>
  52. *
  53. * @property string $table
  54. * The table name.
  55. * @property string $message
  56. * The name of the column message.
  57. * @property string $datetime
  58. * The name of the column datetime.
  59. * @property string $severity
  60. * The name of the column severity.
  61. * @property string $source
  62. * The name of the column source.
  63. * @property string $category
  64. * The name of the column category.
  65. *
  66. * @package EventLogDatabaseTiein
  67. * @version 1.0.2
  68. * @mainclass
  69. */
  70. class ezcLogDatabaseWriter implements ezcLogWriter
  71. {
  72. /**
  73. * Holds the instance to the database handler.
  74. *
  75. * @var ezcDBHandler
  76. */
  77. private $db = null;
  78. /**
  79. * Holds the properties of this class.
  80. *
  81. * @var array(string=>mixed)
  82. */
  83. private $properties = array();
  84. /**
  85. * Holds the default column names in the log tables.
  86. *
  87. * @var array(string=>mixed)
  88. */
  89. private $defaultColumns = array();
  90. /**
  91. * Holds additional column names in the log tables.
  92. *
  93. * @var array(string=>mixed)
  94. */
  95. private $additionalColumns = array();
  96. /**
  97. * Maps tables to ezcLogFilter messages.
  98. *
  99. * @var ezcLogFilterSet
  100. */
  101. private $map;
  102. /**
  103. * Holds the default table name.
  104. *
  105. * @var string
  106. */
  107. private $defaultTable = false;
  108. /**
  109. * Construct a new database log-writer.
  110. *
  111. * If $databaseInstance is given, that instance will be used for writing. If it
  112. * is omitted the default database instance will be retrieved.
  113. *
  114. * This constructor is a tie-in.
  115. *
  116. * @param ezcDbHandler $databaseInstance
  117. * @param string $defaultTable
  118. */
  119. public function __construct( ezcDbHandler $databaseInstance, $defaultTable = false )
  120. {
  121. $this->db = $databaseInstance;
  122. $this->map = new ezcLogFilterSet();
  123. $this->defaultTable = $defaultTable;
  124. $this->message = "message";
  125. $this->datetime = "time";
  126. $this->severity = "severity";
  127. $this->source = "source";
  128. $this->category = "category";
  129. }
  130. /**
  131. * Sets the property $name to $value.
  132. *
  133. * @param string $name
  134. * @param mixed $value
  135. * @ignore
  136. */
  137. public function __set( $name, $value )
  138. {
  139. switch ( $name )
  140. {
  141. case 'table':
  142. $this->properties[$name] = $value;
  143. break;
  144. case 'message':
  145. case 'datetime':
  146. case 'severity':
  147. case 'source':
  148. case 'category':
  149. $this->defaultColumns[$name] = $value;
  150. break;
  151. default:
  152. $this->additionalColumns[$name] = $value;
  153. break;
  154. }
  155. }
  156. /**
  157. * Returns the property $name.
  158. *
  159. * @throws ezcBasePropertyNotFoundException
  160. * If the property $name does not exist
  161. * @param string $name
  162. * @return mixed
  163. * @ignore
  164. */
  165. public function __get( $name )
  166. {
  167. switch ( $name )
  168. {
  169. case 'table':
  170. return $this->properties[$name];
  171. case 'message':
  172. case 'datetime':
  173. case 'severity':
  174. case 'source':
  175. case 'category':
  176. return $this->defaultColumns[$name];
  177. default:
  178. if ( isset( $this->additionalColumns[$name] ) )
  179. {
  180. return $this->additionalColumns[$name];
  181. }
  182. else
  183. {
  184. throw new ezcBasePropertyNotFoundException( $name );
  185. }
  186. }
  187. }
  188. /**
  189. * Returns true if the property $name is set, otherwise false.
  190. *
  191. * @param string $name
  192. * @return bool
  193. * @ignore
  194. */
  195. public function __isset( $name )
  196. {
  197. switch ( $name )
  198. {
  199. case 'table':
  200. return isset( $this->properties[$name] );
  201. case 'message':
  202. case 'datetime':
  203. case 'severity':
  204. case 'source':
  205. case 'category':
  206. return isset( $this->defaultColumns[$name] );
  207. default:
  208. return isset( $this->additionalColumns[$name] );
  209. }
  210. }
  211. /**
  212. * Writes the message $message to the log.
  213. *
  214. * The writer can use the severity, source, and category to filter the
  215. * incoming messages and determine the location where the messages should
  216. * be written.
  217. *
  218. * $optional may contain extra information that can be added to the log. For example:
  219. * line numbers, file names, usernames, etc.
  220. *
  221. * @throws ezcLogWriterException
  222. * If the log writer was unable to write the log message
  223. * @param string $message
  224. * @param int $severity
  225. * ezcLog:: DEBUG, SUCCES_AUDIT, FAILED_AUDIT, INFO, NOTICE, WARNING, ERROR or FATAL.
  226. * $param string $source
  227. * @param string $category
  228. * @param array(string=>string) $optional
  229. */
  230. public function writeLogMessage( $message, $severity, $source, $category, $optional = array() )
  231. {
  232. $severityName = ezcLog::translateSeverityName( $severity );
  233. $tables = $this->map->get( $severity, $source, $category );
  234. $query = $this->db->createSelectQuery();
  235. if ( count( $tables ) > 0 )
  236. {
  237. foreach ( $tables as $t )
  238. {
  239. try
  240. {
  241. $q = $this->db->createInsertQuery();
  242. $q->insertInto( $t )
  243. ->set( $this->message, $q->bindValue( $message ) )
  244. ->set( $this->severity, $q->bindValue( $severityName ) )
  245. ->set( $this->source, $q->bindValue( $source ) )
  246. ->set( $this->category, $q->bindValue( $category ) )
  247. ->set( $this->datetime, $query->expr->now() );
  248. foreach ( $optional as $key => $val )
  249. {
  250. $q->set( ( isset( $this->additionalColumns[$key] ) ? $this->additionalColumns[$key] : $key ), $q->bindValue( $val ) );
  251. }
  252. $stmt = $q->prepare();
  253. $stmt->execute();
  254. }
  255. catch ( PDOException $e )
  256. {
  257. throw new ezcLogWriterException( $e );
  258. }
  259. }
  260. }
  261. else
  262. {
  263. if ( $this->defaultTable !== false )
  264. {
  265. try
  266. {
  267. $q = $this->db->createInsertQuery();
  268. $q->insertInto( $this->defaultTable )
  269. ->set( $this->message, $q->bindValue( $message ) )
  270. ->set( $this->severity, $q->bindValue( $severityName ) )
  271. ->set( $this->source, $q->bindValue( $source ) )
  272. ->set( $this->category, $q->bindValue( $category ) )
  273. ->set( $this->datetime, $query->expr->now() );
  274. foreach ( $optional as $key => $val )
  275. {
  276. $q->set( ( isset( $this->additionalColumns[$key] ) ? $this->additionalColumns[$key] : $key ), $q->bindValue( $val ) );
  277. }
  278. $stmt = $q->prepare();
  279. $stmt->execute();
  280. }
  281. catch ( PDOException $e )
  282. {
  283. throw new ezcLogWriterException( $e );
  284. }
  285. }
  286. }
  287. }
  288. /**
  289. * Returns an array that describes the coupling between the logMessage
  290. * information and the columns in the database.
  291. *
  292. * @return array(string=>string)
  293. */
  294. public function getColumnTranslations()
  295. {
  296. return array_merge( $this->defaultColumns, $this->additionalColumns );
  297. }
  298. /**
  299. * Maps the table $tableName to the messages specified by the {@link ezcLogFilter} $logFilter.
  300. *
  301. * Log messages that matches with the filter are written to the table $tableName.
  302. * This method works the same as {@link ezclog::map()}.
  303. *
  304. * @param ezcLogFilter $logFilter
  305. * @param string $tableName
  306. */
  307. public function setTable( ezcLogFilter $logFilter, $tableName )
  308. {
  309. $this->map->appendRule( new ezcLogFilterRule( $logFilter, $tableName, true ) );
  310. }
  311. }
  312. ?>