2011-11-13 11 views
6

मैं डेटाबेस कनेक्शन एटीएम पुनर्प्राप्त करने के लिए इसका उपयोग कर रहा हूं।ज़ेड SQL क्वेरी लॉगिंग सक्षम करें

$db = Zend_Db_Table::getDefaultAdapter(); 

मैं इस तरह मेरी config में इस सेट करूँ अप:

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 

मैं उदाहरण के लिए एक sql.log के उत्पादन सब कुछ करना चाहते हैं। क्या यह डिफ़ॉल्ट एडाप्टर पर लागू करना संभव है? उदाहरण के लिए सेटिंग्स के माध्यम से, तो मैं इसे उत्पादन पर्यावरण में अनदेखा कर सकते हैं?

बहुत सराहना की।

मैंने देखा: How to enable SQL output to log file with Zend_Db? लेकिन यह मेरी समस्या को कवर नहीं करता था।

/मार्कस

उत्तर

2

Zend_Db_Profiler एक SQL.log लिखने के लिए विस्तृत और अपने डाटाबेस एडाप्टर

<?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); 
} 
} 

यह परीक्षण नहीं किया है करने के लिए प्रोफाइलर देते हैं, लेकिन यह चाल करना चाहिए। इसे आज़माएं और मुझे बताएं।

बीटीडब्ल्यू आप जानते हैं कि आप mysql पर सभी प्रश्नों को भी लॉग कर सकते हैं?

+0

आपको लगता है कि एक सा कृपया का पता लगाने सकता है? – Oldek

+0

ग्रेट, लेकिन कृपया इसका उपयोग नमूना दिखाएं। – Vlado

12

Zend_Db_Profiler को विस्तारित करने का एक उदाहरण है ताकि आप /logs/db-queries.log फ़ाइल पर प्रश्न लिख सकें।

  1. पुस्तकालय फ़ोल्डर में My_Db_Profiler_Log कक्षा बनाएं
  2. application.ini

resources.db.params.profiler में निम्नलिखित पंक्तियां जोड़ें:

तो आप निम्न क्या करना है .enabled = true

resource.db.params.profiler.class = My_Db_Profiler_Log

नोट: ध्यान रखें, लॉग फ़ाइल बहुत बड़ी हो जाएगी, बहुत जल्द! इसलिए केवल उन प्रश्नों को लॉग करना एक अच्छा विचार है, जिनमें आप रुचि रखते हैं। और इस उदाहरण को केवल लॉगिंग सिस्टम के कार्यान्वयन में शुरुआती बिंदु माना जाना चाहिए।

यहां कस्टम प्रोफाइलर वर्ग के लिए कोड है:

<?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 = "\r\nElapsed Secs: " . round($profile->getElapsedSecs(), 5) . "\r\n"; 
     $message .= "Query: " . $profile->getQuery() . "\r\n"; 

     // log the message as INFO message 
     $this->_log->info($message); 

} 

} 

?> 
2

enter image description here

यह आपके वेब पेज, को एसक्यूएल क्वेरी दिखाई इसे बंद विषय हो सकता है दूँगी लेकिन यह उपयोगी

मैं आपको ज़ेडएफ डीबग बार का उपयोग करने की अत्यधिक अनुशंसा करता हूं, यह आपको बहुत आसान जानकारी देगा मैं इसे अपने सिद्धांतों के प्रश्न देखने के लिए उपयोग कर रहा हूं, और इसेके लिए समर्थन मिलाभी

https://github.com/jokkedk/ZFDebug

संबंधित मुद्दे