Guest User

Untitled

a guest
Aug 14th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. Zend Enable SQL Query logging
  2. $db = Zend_Db_Table::getDefaultAdapter();
  3.  
  4. resources.db.adapter = pdo_mysql
  5. resources.db.isDefaultTableAdapter = true
  6. resources.db.params.host = localhost
  7. resources.db.params.username = root
  8. resources.db.params.password = password
  9. resources.db.params.dbname = db
  10. resources.db.params.profiler.enabled = true
  11. resources.db.params.profiler.class = Zend_Db_Profiler
  12.  
  13. <?php
  14.  
  15. class My_Db_Profiler_Log extends Zend_Db_Profiler {
  16.  
  17. /**
  18. * Zend_Log instance
  19. * @var Zend_Log
  20. */
  21. protected $_log;
  22.  
  23. /**
  24. * counter of the total elapsed time
  25. * @var double
  26. */
  27. protected $_totalElapsedTime;
  28.  
  29.  
  30. public function __construct($enabled = false) {
  31. parent::__construct($enabled);
  32.  
  33. $this->_log = new Zend_Log();
  34. $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/logs/db-queries.log');
  35. $this->_log->addWriter($writer);
  36. }
  37.  
  38. /**
  39. * Intercept the query end and log the profiling data.
  40. *
  41. * @param integer $queryId
  42. * @throws Zend_Db_Profiler_Exception
  43. * @return void
  44. */
  45. public function queryEnd($queryId) {
  46. $state = parent::queryEnd($queryId);
  47.  
  48. if (!$this->getEnabled() || $state == self::IGNORED) {
  49. return;
  50. }
  51.  
  52. // get profile of the current query
  53. $profile = $this->getQueryProfile($queryId);
  54.  
  55.  
  56.  
  57. // update totalElapsedTime counter
  58. $this->_totalElapsedTime += $profile->getElapsedSecs();
  59.  
  60. // create the message to be logged
  61. $message = "rnElapsed Secs: " . round($profile->getElapsedSecs(), 5) . "rn";
  62. $message .= "Query: " . $profile->getQuery() . "rn";
  63.  
  64. // log the message as INFO message
  65. $this->_log->info($message);
  66.  
  67. }
  68.  
  69. }
  70.  
  71. ?>
  72.  
  73. <?php
  74.  
  75. class File_Profiler extends Zend_Db_Profiler {
  76. /**
  77. * The filename to save the queries
  78. *
  79. * @var string
  80. */
  81. protected $_filename;
  82.  
  83. /**
  84. * The file handle
  85. *
  86. * @var resource
  87. */
  88. protected $_handle = null;
  89.  
  90. /**
  91. * Class constructor
  92. *
  93. * @param string $filename
  94. */
  95. public function __construct( $filename ) {
  96. $this->_filename = $filename;
  97. }
  98.  
  99. /**
  100. * Change the profiler status. If the profiler is not enabled no
  101. * query will be written to the destination file
  102. *
  103. * @param boolean $enabled
  104. */
  105. public function setEnabled( $enabled ) {
  106. parent::setEnabled($enabled);
  107.  
  108. if( $this->getEnabled() ) {
  109. if( !$this->_handle ) {
  110. if( !($this->_handle = @fopen($this->_filename, "a")) ) {
  111. throw new Exception("Unable to open filename {$this->_filename} for query profiling");
  112. }
  113. }
  114. }
  115. else {
  116. if( $this->_handle ) {
  117. @fclose($this->_handle);
  118. }
  119. }
  120. }
  121.  
  122. /**
  123. * Intercept parent::queryEnd to catch the query and write it to a file
  124. *
  125. * @param int $queryId
  126. */
  127. public function queryEnd($queryId) {
  128. $state = parent::queryEnd($queryId);
  129.  
  130. if(!$this->getEnabled() || $state == self::IGNORED) {
  131. return;
  132. }
  133.  
  134. $profile = $this->getQueryProfile($queryId);
  135.  
  136. @fwrite($this->_handle, round($profile->getElapsedSecs(),5) . " " . $profile->getQuery() . " " . ($params=$profile->getQueryParams())?$params:null);
  137. }
  138. }
Add Comment
Please, Sign In to add comment