PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/propel_15/vendor/propel/docs/guide/08-Logging.txt

http://github.com/eventhorizonpl/forked-php-orm-benchmark
Plain Text | 448 lines | 359 code | 89 blank | 0 comment | 0 complexity | 1aec29fa2a9569348c92a5feef95800e MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. = Logging And Debugging =
  2. [[PageOutline]]
  3. Propel provides tools to monitor and debug your model. Whether you need to check the SQL code of slow queries, or to look for error messages previously thrown, Propel is your best friend for finding and fixing problems.
  4. == Propel Logs ==
  5. Propel uses the logging facility configured in `runtime-conf.xml` to record errors, warnings, and debug information.
  6. By default Propel will attempt to use the Log framework that is distributed with PEAR. If you are not familiar with it, check its [http://www.indelible.org/php/Log/guide.html online documentation]. It is also easy to configure Propel to use your own logging framework -- or none at all.
  7. === Logger Configuration ===
  8. The Propel log handler is configured in the `<log>` section of your project's `runtime-conf.xml` file. Here is the accepted format for this section with the default values that Propel uses:
  9. {{{
  10. #!xml
  11. <?xml version="1.0" encoding="ISO-8859-1"?>
  12. <config>
  13. <log>
  14. <type>file</type>
  15. <name>./propel.log</name>
  16. <ident>propel</ident>
  17. <level>7</level> <!-- PEAR_LOG_DEBUG -->
  18. <conf></conf>
  19. </log>
  20. <propel>
  21. ...
  22. </propel>
  23. </config>
  24. }}}
  25. Using these parameters, Propel creates a ''file'' Log handler in the background, and keeps it for later use:
  26. {{{
  27. #!php
  28. <?php
  29. Propel::$logger = Log::singleton($type = 'file', $name = './propel.log', $ident = 'propel', $conf = array(), $level = PEAR_LOG_DEBUG);
  30. }}}
  31. The meaning of each of the `<log>` nested elements may vary, depending on which log handler you are using. Refer to the [http://www.indelible.org/php/Log/guide.html#standard-log-handlers PEAR::Log] documentation for more details on log handlers configuration and options.
  32. Note that the `<level>` tag needs to correspond to the integer represented by one of the `PEAR_LOG_*` constants:
  33. ||'''Constant'''||'''Value'''||'''Description'''
  34. ||PEAR_LOG_EMERG||0||System is unusable||
  35. ||PEAR_LOG_ALERT||1||Immediate action required||
  36. ||PEAR_LOG_CRIT||2||Critical conditions||
  37. ||PEAR_LOG_ERR||3||Error conditions||
  38. ||PEAR_LOG_WARNING||4||Warning conditions||
  39. ||PEAR_LOG_NOTICE||5||Normal but significant||
  40. ||PEAR_LOG_INFO||6||Informational||
  41. ||PEAR_LOG_DEBUG||7||Debug-level messages||
  42. === Logging Messages ===
  43. Use the static `Propel::log()` method to log a message using the configured log handler:
  44. {{{
  45. #!php
  46. <?php
  47. $myObj = new MyObj();
  48. $myObj->setName('foo');
  49. Propel::log('uh-oh, something went wrong with ' . $myObj->getName(), Propel::LOG_ERROR);
  50. }}}
  51. You can log your own messages from the generated model objects by using their `log()` method, inherited from `BaseObject`:
  52. {{{
  53. #!php
  54. <?php
  55. $myObj = new MyObj();
  56. $myObj->log('uh-oh, something went wrong', Propel::LOG_ERROR);
  57. }}}
  58. The log messages will show up in the log handler defined in `runtime-conf.xml` (`propel.log` file by default) as follows:
  59. {{{
  60. Oct 04 00:00:18 [error] uh-oh, something went wrong with foo
  61. Oct 04 00:00:18 [error] MyObj: uh-oh, something went wrong
  62. }}}
  63. Tip: All serious errors coming from the Propel core do not only issue a log message, they are also thrown as `PropelException`.
  64. === Using An Alternative PEAR Log Handler ===
  65. In many cases you may wish to integrate Propel's logging facility with the rest of your web application. In `runtime-conf.xml`, you can customize a different PEAR logger. Here are a few examples:
  66. '''Example 1:''' Using 'display' container (for output to HTML)
  67. {{{
  68. #!xml
  69. <log>
  70. <type>display</type>
  71. <level>6</level> <!-- PEAR_LOG_INFO -->
  72. </log>
  73. }}}
  74. '''Example 2:''' Using 'syslog' container
  75. {{{
  76. #!xml
  77. <log>
  78. <type>syslog</type>
  79. <name>8</name> <!-- LOG_USER -->
  80. <ident>propel</ident>
  81. <level>6</level>
  82. </log>
  83. }}}
  84. === Using A Custom Logger ===
  85. If you omit the `<log>` section of your `runtime-conf.xml`, then Propel will not setup ''any'' logging for you. In this case, you can set a custom logging facility and pass it to Propel at runtime.
  86. Here's an example of how you could configure your own logger and then set Propel to use it:
  87. {{{
  88. #!php
  89. <?php
  90. require_once 'MyLogger.php';
  91. $logger = new MyLogger();
  92. require_once 'propel/Propel.php';
  93. Propel::setLogger($logger);
  94. Propel::init('/path/to/runtime-conf.php');
  95. }}}
  96. Your custom logger could be any object that implements a basic logger interface. Check the `BasicLogger` interface provided with the Propel runtime to see the methods that a logger must implement in order to be compatible with Propel. You do not actually have to implement this interface, but all the specified methods must be present in your container.
  97. Let's see an example of a simple log container suitable for use with Propel:
  98. {{{
  99. #!php
  100. <?php
  101. class MyLogger implements BasicLogger
  102. {
  103. public function emergency($m)
  104. {
  105. $this->log($m, Propel::LOG_EMERG);
  106. }
  107. public function alert($m)
  108. {
  109. $this->log($m, Propel::LOG_ALERT);
  110. }
  111. public function crit($m)
  112. {
  113. $this->log($m, Propel::LOG_CRIT);
  114. }
  115. public function err($m)
  116. {
  117. $this->log($m, Propel::LOG_ERR);
  118. }
  119. public function warning($m)
  120. {
  121. $this->log($m, Propel::LOG_WARNING);
  122. }
  123. public function notice($m)
  124. {
  125. $this->log($m, Propel::LOG_NOTICE);
  126. }
  127. public function info($m)
  128. {
  129. $this->log($m, Propel::LOG_INFO);
  130. }
  131. public function debug($m)
  132. {
  133. $this->log($m, Propel::LOG_DEBUG);
  134. }
  135. public function log($message, $priority)
  136. {
  137. $color = $this->priorityToColor($priority);
  138. echo '<p style="color: ' . $color . '">$message</p>';
  139. }
  140. private function priorityToColor($priority)
  141. {
  142. switch($priority) {
  143. case Propel::LOG_EMERG:
  144. case Propel::LOG_ALERT:
  145. case Propel::LOG_CRIT:
  146. case Propel::LOG_ERR:
  147. return 'red';
  148. break;
  149. case Propel::LOG_WARNING:
  150. return 'orange';
  151. break;
  152. case Propel::LOG_NOTICE:
  153. return 'green';
  154. break;
  155. case Propel::LOG_INFO:
  156. return 'blue';
  157. break;
  158. case Propel::LOG_DEBUG:
  159. return 'grey';
  160. break;
  161. }
  162. }
  163. }
  164. }}}
  165. Tip: There is also a bundled `MojaviLogAdapter` class which allows you to use a Mojavi logger with Propel.
  166. == Debugging Database Activity ==
  167. By default, Propel uses `PropelPDO` for database connections. This class, which extends PHP's `PDO`, offers a debug mode to keep track of all the database activity, including all the executed queries.
  168. === Enabling The Debug Mode ===
  169. The debug mode is disabled by default, but you can enable it at runtime as follows:
  170. {{{
  171. #!php
  172. <?php
  173. $con = Propel::getConnection(MyObjPeer::DATABASE_NAME);
  174. $con->useDebug(true);
  175. }}}
  176. You can also disable the debug mode at runtime, by calling `PropelPDO::useDebug(false)`. Using this method, you can choose to enable the debug mode for only one particular query, or for all queries.
  177. Alternatively, you can ask Propel to always enable the debug mode for a particular connection by using the `DebugPDO` class instead of the default `PropelPDO` class. This is accomplished in the `runtime-conf.xml` file, in the `<classname>` tag of a given datasource connection (see the [wiki:Documentation/1.5/RuntimeConfiguration runtime configuration reference] for more details).
  178. {{{
  179. #!xml
  180. <?xml version="1.0"?>
  181. <config>
  182. <propel>
  183. <datasources default="bookstore">
  184. <datasource id="bookstore">
  185. <adapter>sqlite</adapter>
  186. <connection>
  187. <!-- the classname that Propel should instantiate, must be PropelPDO subclass -->
  188. <classname>DebugPDO</classname>
  189. }}}
  190. '''Tip''': You can use your own connection class there, but make sure that it extends `PropelPDO` and not only `PDO`. Propel requires certain fixes to PDO API that are provided by `PropelPDO`.
  191. === Counting Queries ===
  192. In debug mode, `PropelPDO` keeps track of the number of queries that are executed. Use `PropelPDO::getQueryCount()` to retrieve this number:
  193. {{{
  194. #!php
  195. <?php
  196. $con = Propel::getConnection(MyObjPeer::DATABASE_NAME);
  197. $myObjs = MyObjPeer::doSelect(new Criteria(), $con);
  198. echo $con->getQueryCount(); // 1
  199. }}}
  200. Tip: You cannot use persistent connections if you want the query count to work. Actually, the debug mode in general requires that you don't use persistent connections in order for it to correctly log bound values and count executed statements.
  201. === Retrieving The Latest Executed Query ===
  202. For debugging purposes, you may need the SQL code of the latest executed query. It is available at runtime in debug mode using `PropelPDO::getLastExecutedQuery()`, as follows:
  203. {{{
  204. #!php
  205. <?php
  206. $con = Propel::getConnection(MyObjPeer::DATABASE_NAME);
  207. $myObjs = MyObjPeer::doSelect(new Criteria(), $con);
  208. echo $con->getLastExecutedQuery(); // 'SELECT * FROM my_obj';
  209. }}}
  210. Tip: You can also get a decent SQL representation of the criteria being used in a SELECT query by using the `Criteria->toString()` method.
  211. Propel also keeps track of the queries executed directly on the connection object, and displays the bound values correctly.
  212. {{{
  213. #!php
  214. <?php
  215. $con = Propel::getConnection(MyObjPeer::DATABASE_NAME);
  216. $stmt = $con->prepare('SELECT * FROM my_obj WHERE name = :p1');
  217. $stmt->bindValue(':p1', 'foo');
  218. $stmt->execute();
  219. echo $con->getLastExecutedQuery(); // 'SELECT * FROM my_obj where name = "foo"';
  220. }}}
  221. '''Tip''': The debug mode is intended for development use only. Do not use it in production environment, it logs too much information for a production server, and adds a small overhead to the database queries.
  222. == Full Query Logging ==
  223. The combination of the debug mode and a logging facility provides a powerful debugging tool named ''full query logging''. If you have properly configured a log handler, enabling the debug mode (or using `DebugPDO`) automatically logs the executed queries into Propel's default log file:
  224. {{{
  225. Oct 04 00:00:18 propel-bookstore [debug] INSERT INTO publisher (`ID`,`NAME`) VALUES (NULL,'William Morrow')
  226. Oct 04 00:00:18 propel-bookstore [debug] INSERT INTO author (`ID`,`FIRST_NAME`,`LAST_NAME`) VALUES (NULL,'J.K.','Rowling')
  227. Oct 04 00:00:18 propel-bookstore [debug] INSERT INTO book (`ID`,`TITLE`,`ISBN`,`PRICE`,`PUBLISHER_ID`,`AUTHOR_ID`) VALUES (NULL,'Harry Potter and the Order of the Phoenix','043935806X',10.99,53,58)
  228. Oct 04 00:00:18 propel-bookstore [debug] INSERT INTO review (`ID`,`REVIEWED_BY`,`REVIEW_DATE`,`RECOMMENDED`,`BOOK_ID`) VALUES (NULL,'Washington Post','2009-10-04',1,52)
  229. ...
  230. Oct 04 00:00:18 propel-bookstore [debug] SELECT bookstore_employee_account.EMPLOYEE_ID, bookstore_employee_account.LOGIN FROM `bookstore_employee_account` WHERE bookstore_employee_account.EMPLOYEE_ID=25
  231. }}}
  232. By default, Propel logs all SQL queries, together with the date of the query and the name of the connection.
  233. === Setting The Data To Log ===
  234. The full query logging feature can be configured either in the `runtime-conf.xml` configuration file, or using the runtime configuration API.
  235. In `runtime-conf.xml`, tweak the feature by adding a `<debugpdo>` tag under `<propel>`:
  236. {{{
  237. #!xml
  238. <?xml version="1.0"?>
  239. <config>
  240. <log>
  241. ...
  242. </log>
  243. <propel>
  244. <datasources default="bookstore">
  245. ...
  246. </datasources>
  247. <debugpdo>
  248. <logging>
  249. <details>
  250. <method>
  251. <enabled>true</enabled>
  252. </method>
  253. <time>
  254. <enabled>true</enabled>
  255. </time>
  256. <mem>
  257. <enabled>true</enabled>
  258. </mem>
  259. </details>
  260. </logging>
  261. </debugpdo>
  262. </propel>
  263. </config>
  264. }}}
  265. To accomplish the same configuration as above at runtime, change the settings in your main include file, after `Propel::init()`, as follows:
  266. {{{
  267. #!php
  268. <?php
  269. $config = Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT);
  270. $config->setParameter('debugpdo.logging.details.method.enabled', true);
  271. $config->setParameter('debugpdo.logging.details.time.enabled', true);
  272. $config->setParameter('debugpdo.logging.details.mem.enabled', true);
  273. }}}
  274. Let's see a few of the provided parameters.
  275. === Logging More Connection Messages ===
  276. `PropelPDO` can log queries, but also connection events (open and close), and transaction events (begin, commit and rollback). Since Propel can emulate nested transactions, you may need to know when an actual `COMMIT` or `ROLLBACK` is issued.
  277. To extend which methods of `PropelPDO` do log messages in debug mode, customize the `'debugpdo.logging.methods'` parameter, as follows:
  278. {{{
  279. #!php
  280. <?php
  281. $allMethods = array(
  282. 'PropelPDO::__construct', // logs connection opening
  283. 'PropelPDO::__destruct', // logs connection close
  284. 'PropelPDO::exec', // logs a query
  285. 'PropelPDO::query', // logs a query
  286. 'PropelPDO::prepare', // logs the preparation of a statement
  287. 'PropelPDO::beginTransaction', // logs a transaction begin
  288. 'PropelPDO::commit', // logs a transaction commit
  289. 'PropelPDO::rollBack', // logs a transaction rollBack (watch out for the capital 'B')
  290. 'DebugPDOStatement::execute', // logs a query from a prepared statement
  291. 'DebugPDOStatement::bindValue' // logs the value and type for each bind
  292. );
  293. $config = Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT);
  294. $config->setParameter('debugpdo.logging.methods', $allMethods);
  295. }}}
  296. By default, only the messages coming from `PropelPDO::exec`, `PropelPDO::query`, and `DebugPDOStatement::execute` are logged.
  297. === Logging Execution Time And Memory ===
  298. In debug mode, Propel counts the time and memory necessary for each database query. This very valuable data can be added to the log messages on demand, by adding the following configuration:
  299. {{{
  300. #!php
  301. <?php
  302. $config = Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT);
  303. $config->setParameter('debugpdo.logging.details.time.enabled', true);
  304. $config->setParameter('debugpdo.logging.details.mem.enabled', true);
  305. }}}
  306. Enabling the options shown above, you get log output along the lines of:
  307. {{{
  308. Feb 23 16:41:04 Propel [debug] time: 0.000 sec | mem: 1.4 MB | SET NAMES 'utf8'
  309. Feb 23 16:41:04 Propel [debug] time: 0.002 sec | mem: 1.6 MB | SELECT COUNT(tags.NAME) FROM tags WHERE tags.IMAGEID = 12
  310. Feb 23 16:41:04 Propel [debug] time: 0.012 sec | mem: 2.4 MB | SELECT tags.NAME, image.FILENAME FROM tags LEFT JOIN image ON tags.IMAGEID = image.ID WHERE image.ID = 12
  311. }}}
  312. The order in which the logging details are enabled is significant, since it determines the order in which they will appear in the log file.
  313. === Complete List Of Logging Options ===
  314. The following settings can be customized at runtime or in the configuration file:
  315. ||'''Parameter'''||'''Default'''||'''Meaning'''||
  316. ||`debugpdo.logging.enabled`||`true`||Should any logging take place||
  317. ||`debugpdo.logging.innerglue`||`": "`||String to use for combining the title of a detail and its value||
  318. ||`debugpdo.logging.outerglue`||`" | "`||String to use for combining details together on a log line||
  319. ||`debugpdo.logging.realmemoryusage`||`false`||Parameter to [http://www.php.net/manual/en/function.memory-get-usage.php memory_get_usage()] and [http://www.php.net/manual/en/function.memory-get-peak-usage.php memory_get_peak_usage()] calls||
  320. ||`debugpdo.logging.methods`||[http://propel.propelorm.org/browser/branches/1.5/runtime/classes/propel/util/DebugPDO.php#L151 array(...)]||An array of method names `Class::method`) to be included in method call logging||
  321. ||`debugpdo.logging.details.slow.enabled`||`false`||Enables flagging of slow method calls||
  322. ||`debugpdo.logging.details.slow.threshold`||`0.1`||Method calls taking more seconds than this threshold are considered slow||
  323. ||`debugpdo.logging.details.time.enabled`||`false`||Enables logging of method execution times||
  324. ||`debugpdo.logging.details.time.precision`||`3`||Determines the precision of the execution time logging||
  325. ||`debugpdo.logging.details.time.pad`||`10`||How much horizontal space to reserve for the execution time on a log line||
  326. ||`debugpdo.logging.details.mem.enabled`||`false`||Enables logging of the instantaneous PHP memory consumption||
  327. ||`debugpdo.logging.details.mem.precision`||`1`||Determines the precision of the memory consumption logging||
  328. ||`debugpdo.logging.details.mem.pad`||`9`||How much horizontal space to reserve for the memory consumption on a log line||
  329. ||`debugpdo.logging.details.memdelta.enabled`||`false`||Enables logging differences in memory consumption before and after the method call||
  330. ||`debugpdo.logging.details.memdelta.precision`||`1`||Determines the precision of the memory difference logging||
  331. ||`debugpdo.logging.details.memdelta.pad`||`10`||How much horizontal space to reserve for the memory difference on a log line||
  332. ||`debugpdo.logging.details.mempeak.enabled`||`false`||Enables logging the peak memory consumption thus far by the currently executing PHP script||
  333. ||`debugpdo.logging.details.mempeak.precision`||`1`||Determines the precision of the memory peak logging||
  334. ||`debugpdo.logging.details.mempeak.pad`||`9`||How much horizontal space to reserve for the memory peak on a log line||
  335. ||`debugpdo.logging.details.querycount.enabled`||`false`||Enables logging of the number of queries performed by the DebugPDO instance thus far||
  336. ||`debugpdo.logging.details.querycount.pad`||`2`||How much horizontal space to reserve for the query count on a log line||
  337. ||`debugpdo.logging.details.method.enabled`||`false`||Enables logging of the name of the method call||
  338. ||`debugpdo.logging.details.method.pad`||`28`||How much horizontal space to reserve for the method name on a log line||
  339. === Changing the Log Level ===
  340. By default the connection log messages are logged at the `Propel::LOG_DEBUG` level. This can be changed by calling the `setLogLevel()` method on the connection object:
  341. {{{
  342. #!php
  343. <?php
  344. $con = Propel::getConnection(MyObjPeer::DATABASE_NAME);
  345. $con->setLogLevel(Propel::LOG_INFO);
  346. }}}
  347. Now all queries and bind param values will be logged at the INFO level.
  348. === Configuring a Different Full Query Logger ===
  349. By default the `PropelPDO` connection logs queries and binds param values using the `Propel::log()` static method. As explained above, this method uses the log storage configured by the `<log>` tag in the `runtime-conf.xml` file.
  350. If you would like the queries to be logged using a different logger (e.g. to a different file, or with different ident, etc.), you can set a logger explicitly on the connection at runtime, using `Propel::setLogger()`:
  351. {{{
  352. #!php
  353. <?php
  354. $con = Propel::getConnection(MyObjPeer::DATABASE_NAME);
  355. $logger = Log::factory('syslog', LOG_LOCAL0, 'propel', array(), PEAR_LOG_INFO);
  356. $con->setLogger($logger);
  357. }
  358. }}}
  359. This will not affect the general Propel logging, but only the full query logging. That way you can log the Propel error and warnings in one file, and the SQL queries in another file.