【问题标题】:Writing Logs from Console Shell从控制台 Shell 写入日志
【发布时间】:2014-02-26 05:25:57
【问题描述】:

我一直在使用 CakePHP 2.4.4 来构建我的应用程序的交互式 Web 部分,并且进展顺利。 CakePHP 很棒。

我现在正在做一些支持性的后台进程。控制台和 Shell 似乎是执行此操作的方法,因为它可以访问模型。

我已经编写了代码并让它工作,但我正在尝试写入用于模型的同一日志。在模型中,我有一个 afterSave 函数来记录所有数据库更改,我只是使用 $this->log("$model $logEntry", 'info'); 写入日志。

这在 Shell 中不起作用,但我认为 CakeLog::write('info', "$model $logEntry"); 可能会起作用,但它也不起作用。

我是否需要初始化 CakeLog 以指向正确的日志文件?

<?php
App::uses('CakeTime', 'Utility');
App::uses('CakeLog', 'Utility');

class ProcessRequestShell extends AppShell {
    //Need to access the request and monitor tables
    public $uses = array('Request');

    private function updateRequest($data){
        $model = 'Request';
        $result = $this->Request->save($data);

        $logEntry = "UPDATE ProcessRequestShell ";
        foreach ($data[$model] AS $k => $v){$logEntry .= "$k='$v' ";}
        if ($result){
            //$this->log("$model $logEntry", 'info');
            CakeLog::write('info', "$model $logEntry");
        } else {
            //$this->log("$model FAILED $logEntry", 'error');
            CakeLog::write('error', "$model FAILED $logEntry");
        }
        return($result);
    }

    public function main() {
        $options = array('conditions' => array('state' => 0, 'next_state' => 1));
        $this->Request->recursive = 0;
        $requests = $this->Request->find('all', $options);

        //See if the apply_changes_on date/time is past
        foreach ($requests AS $request){
            $this->out("Updating request ".$request['Request']['id'], 1, Shell::NORMAL);

            //Update the next_state to "ready"
            $request['Request']['state'] = 1;
            $request['Request']['next_state'] = 2;
            $this->updateRequest($request);
        }
    }
}
?>

【问题讨论】:

  • 我觉得你应该试试:CakeLog::write('info', $model . $logEntry);
  • 很确定我上面的评论不应该有所作为。这两种方式都对我有用。如果您使用:$this-&gt;Request-&gt;log("$model $logEntry", 'info');,您会得到相同的行为吗?

标签: shell cakephp cakephp-2.4


【解决方案1】:

您是否为这些日志类型设置了默认侦听器/范围? 如果没有,他们将不会被记录。

// Setup a 'default' cache configuration for use in the application.
Cache::config('default', array('engine' => 'File'));

例如在你的 bootstrap.php 中

http://book.cakephp.org/2.0/en/appendices/2-2-migration-guide.html#log

【讨论】:

  • 我现在看到了问题。我确实已经设置了日志,它一直在写入日志,但有一组不同的日志。在 Controller/Model 中,它写入 AppName/tmp/logs,但在 Shell 中,它写入 AppName/app/tmp/logs。
【解决方案2】:

您需要设置default 日志流写入文件,最终在app/Config/bootstrap.php 中。

CakeLog 不再自动配置自己。结果日志文件 如果没有流在监听,将不再自动创建。确保 如果您想收听,您至少设置了一个default 流 所有类型和级别。通常,您可以只设置核心FileLog 类 输出到app/tmp/logs/:

CakeLog::config('default', array(
    'engine' => 'File'
));

参见 CookBook 2.x 的 Logging → Writing to logs 部分

【讨论】:

    猜你喜欢
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 2023-03-07
    • 2013-08-29
    • 2011-02-09
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多