双重日志声明

时间:2013-05-24 19:16:49

标签: cakephp ubuntu-12.04

我有一个奇怪的问题,它可能是愚蠢的但我无法找到问题所在。 我在cakephp 2.x上开发了一个应用程序,当我从控制器记录数据时,它在日志中出现两次。像这样:

  • 2013-05-24 11:50:19调试:上传excel文件
  • 2013-05-24 11:50:19调试:上传excel文件
  • 2013-05-24 11:50:19调试:防火测试
  • 2013-05-24 11:50:19调试:防火测试

只是增加一些乐趣,它不会发生在该控制器的所有功能中,只有六分之二。这让我很烦恼,我不知道应该采用什么方式去挖掘它。 有什么想法吗?

编辑: 好的,我发现当我在一个方法中登录到两个不同的文件时会发生这种情况。 当我更改行时:CakeLog :: write('time'....);到CakeLog :: write('debug'....); 一切正常。如下面的方法:

function file_upload() {
    if (!$this->request->data) {           
    } else {
        CakeLog::write('time', 'start working at: ' . date('m/d/Y', strtotime("now")));

        $data = Sanitize::clean($this->request->data);

        CakeLog::write('debug', 'test statement');

        if ($data['Scrap']['excel_submittedfile']['type'] === 'application/vnd.ms-excel' && $data['Scrap']['csv_submittedfile']['type'] === 'text/csv') {
            $tmp_xls_file = $data['Scrap']['excel_submittedfile']['tmp_name'];
            $xls_file = $data['Scrap']['excel_submittedfile']['name'];
            $tmp_csv_file = $data['Scrap']['csv_submittedfile']['tmp_name'];
            $csv_file = $data['Scrap']['csv_submittedfile']['name'];
            $upload_dir = WWW_ROOT . "/files/";
            if (file_exists($upload_dir) && is_writable($upload_dir)) {
                if (move_uploaded_file($tmp_xls_file, $upload_dir . $xls_file) && move_uploaded_file($tmp_csv_file, $upload_dir . $csv_file)) {

                    CakeLog::write('debug', 'excel file uploaded');

                    $this->redirect(array('action' => 'edit', $xls_file, $csv_file));
                } else {
                    echo 'upload failed';
                }
            } else {
                echo 'Upload directory is not writable, or does not exist.';
            }
        } else {
            echo 'make sure the files are in correct format';
        }
    }
}

我想这与bootstrap.php中的日志文件声明有关。所以这不是一件很烦人的问题。

1 个答案:

答案 0 :(得分:3)

这是因为您的电话

CakeLog::write('time', 'start working at: ' . date('m/d/Y', strtotime("now")));

将尝试写入类型的日志:“time”。由于没有配置处理流的流,因此CakeLog将为您创建一个“默认”流来处理此日志调用。

问题在于,从现在开始,您将配置一个“默认”流,它将捕获所有日志并将其加倍以用于调试和错误日志。

解决方案是在bootstrap.php文件中正确配置日志,如下所示:

CakeLog::config('time_stream', array(
    'engine' => 'FileLog',
    'types' => array( 'time' ), //<--here is the log type of 'time'
    'file' => 'time', //<-- this will go to time.log
) );

当然,如果您使用其他日志类型,您还需要为这些日志类型配置流,否则将为您配置默认的catch-all流,您将再次遇到相同的问题。

祝你好运!