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

我现在正在做一些支持的背景进程。控制台和shell似乎是这样做的方法,因为它可以访问模型。

我已经写了代码并使其工作,但我试图写入我用于模型的相同日志。在模型中,我有一个船舶的函数来记录所有数据库更改,我刚刚使用$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);
        }
    }
}
?>
.

有帮助吗?

解决方案

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

// 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 / chependices / 2-2-migration-guide.html#log

其他提示

You need to setup default log stream writing to file, eventually, in app/Config/bootstrap.php.

CakeLog does not auto-configure itself anymore. As a result log files will not be auto-created anymore if no stream is listening. Make sure you got at least one default stream set up, if you want to listen to all types and levels. Usually, you can just set the core FileLog class to output into app/tmp/logs/:

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

See Logging → Writing to logs section of the CookBook 2.x

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top