2011-11-13 36 views
6

Tôi đang sử dụng tính năng này để truy xuất kết nối cơ sở dữ liệu.Zend Bật ghi nhật ký truy vấn SQL

$db = Zend_Db_Table::getDefaultAdapter(); 

tôi thiết lập trong cấu hình của tôi như thế này:

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 

Tôi muốn đầu ra tất cả mọi thứ đến một sql.log ví dụ. Điều này có thể áp dụng trên bộ điều hợp mặc định không? ví dụ thông qua các thiết lập, vì vậy tôi có thể bỏ qua nó trong môi trường sản xuất?

Nhiều ứng dụng được đánh giá cao.

Tôi đã xem xét: How to enable SQL output to log file with Zend_Db? nhưng dường như không đề cập đến vấn đề của tôi.

/Marcus

Trả lời

2

Mở rộng Zend_Db_Profiler để viết thư cho một SQL.log và đính kèm các hồ sơ để chuyển đổi db của bạn

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

đã không kiểm tra nó, nhưng nó phải làm các trick. Hãy thử và cho tôi biết.

Btw bạn biết rằng bạn có thể đăng nhập tất cả các truy vấn trên mysql không?

+0

Bạn có thể khám phá một chút được không? – Oldek

+0

Tuyệt vời, nhưng vui lòng cho biết cách sử dụng mẫu này. – Vlado

12

Có ví dụ về việc mở rộng Zend_Db_Profiler để bạn có thể ghi các truy vấn vào /logs/db-queries.log.

Vì vậy, bạn phải làm như sau:

  1. Tạo lớp My_Db_Profiler_Log trong thư mục thư viện
  2. Thêm các dòng sau vào application.ini

resources.db.params.profiler .enabled = true

resources.db.params.profiler.class = My_Db_Profiler_Log

Lưu ý: lưu ý rằng tệp nhật ký sẽ trở nên rất lớn, rất sớm! Vì vậy, nó là một ý tưởng tốt để đăng nhập chỉ các truy vấn mà bạn quan tâm. Và ví dụ này nên được coi là chỉ là một điểm khởi đầu trong việc thực hiện một hệ thống đăng nhập như vậy.

Đây là mã cho lớp tùy chỉnh hồ sơ:

<?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

này sẽ cho phép bạn xem các truy vấn sql vào trang web, nó có thể là OFF TOPIC nhưng nó hữu ích

Tôi rất khuyên bạn sử dụng thanh gỡ lỗi ZF, nó sẽ cung cấp cho bạn thông tin rất tiện dụng tôi đang sử dụng nó để xem các truy vấn học thuyết của tôi và nó đã hỗ trợ cho zend db quá

https://github.com/jokkedk/ZFDebug

Các vấn đề liên quan