【问题标题】:Zend Enable SQL Query loggingZend 启用 SQL 查询日志记录
【发布时间】:2011-11-13 21:42:50
【问题描述】:

我正在使用它来检索数据库连接 atm。

    $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

例如,我想将所有内容输出到 sql.log。这可以应用于默认适配器吗?例如通过设置,所以我可以在生产环境中忽略它?

非常感谢。

我确实看过:How to enable SQL output to log file with Zend_Db?,但它似乎没有涵盖我的问题。

/马库斯

【问题讨论】:

    标签: zend-framework zend-db zend-db-table


    【解决方案1】:

    有一个扩展 Zend_Db_Profiler 的示例,因此您可以将查询写入 /logs/db-queries.log 文件。

    所以你必须做到以下几点:

    1. 在库文件夹中创建 My_Db_Profiler_Log 类
    2. 将以下行添加到 application.ini

    resources.db.params.profiler.enabled = true

    resources.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】:

      扩展 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 上记录所有查询吗?

      【讨论】:

      • 你能稍微探索一下吗?
      【解决方案3】:

      这将让您看到对网页的 sql 查询,可能偏离主题但它很有帮助

      我强烈推荐你使用 ZF 调试栏,它会给你非常方便的信息 我用它来查看我的学说查询,它也支持zend db

      https://github.com/jokkedk/ZFDebug

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-18
        • 2021-03-25
        • 2021-09-17
        • 1970-01-01
        相关资源
        最近更新 更多