Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Zend Enable SQL Query logging
- $db = Zend_Db_Table::getDefaultAdapter();
- resources.db.adapter = pdo_mysql
- resources.db.isDefaultTableAdapter = true
- resources.db.params.host = localhost
- resources.db.params.username = root
- resources.db.params.password = password
- resources.db.params.dbname = db
- resources.db.params.profiler.enabled = true
- resources.db.params.profiler.class = Zend_Db_Profiler
- <?php
- class My_Db_Profiler_Log extends Zend_Db_Profiler {
- /**
- * Zend_Log instance
- * @var Zend_Log
- */
- protected $_log;
- /**
- * counter of the total elapsed time
- * @var double
- */
- protected $_totalElapsedTime;
- public function __construct($enabled = false) {
- parent::__construct($enabled);
- $this->_log = new Zend_Log();
- $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/logs/db-queries.log');
- $this->_log->addWriter($writer);
- }
- /**
- * Intercept the query end and log the profiling data.
- *
- * @param integer $queryId
- * @throws Zend_Db_Profiler_Exception
- * @return void
- */
- public function queryEnd($queryId) {
- $state = parent::queryEnd($queryId);
- if (!$this->getEnabled() || $state == self::IGNORED) {
- return;
- }
- // get profile of the current query
- $profile = $this->getQueryProfile($queryId);
- // update totalElapsedTime counter
- $this->_totalElapsedTime += $profile->getElapsedSecs();
- // create the message to be logged
- $message = "rnElapsed Secs: " . round($profile->getElapsedSecs(), 5) . "rn";
- $message .= "Query: " . $profile->getQuery() . "rn";
- // log the message as INFO message
- $this->_log->info($message);
- }
- }
- ?>
- <?php
- class File_Profiler extends Zend_Db_Profiler {
- /**
- * The filename to save the queries
- *
- * @var string
- */
- protected $_filename;
- /**
- * The file handle
- *
- * @var resource
- */
- protected $_handle = null;
- /**
- * Class constructor
- *
- * @param string $filename
- */
- public function __construct( $filename ) {
- $this->_filename = $filename;
- }
- /**
- * Change the profiler status. If the profiler is not enabled no
- * query will be written to the destination file
- *
- * @param boolean $enabled
- */
- public function setEnabled( $enabled ) {
- parent::setEnabled($enabled);
- if( $this->getEnabled() ) {
- if( !$this->_handle ) {
- if( !($this->_handle = @fopen($this->_filename, "a")) ) {
- throw new Exception("Unable to open filename {$this->_filename} for query profiling");
- }
- }
- }
- else {
- if( $this->_handle ) {
- @fclose($this->_handle);
- }
- }
- }
- /**
- * Intercept parent::queryEnd to catch the query and write it to a file
- *
- * @param int $queryId
- */
- public function queryEnd($queryId) {
- $state = parent::queryEnd($queryId);
- if(!$this->getEnabled() || $state == self::IGNORED) {
- return;
- }
- $profile = $this->getQueryProfile($queryId);
- @fwrite($this->_handle, round($profile->getElapsedSecs(),5) . " " . $profile->getQuery() . " " . ($params=$profile->getQueryParams())?$params:null);
- }
- }
Add Comment
Please, Sign In to add comment