2014-02-26 92 views
1

我一直在使用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); 
     } 
    } 
} 
?> 
+0

我想你应该尝试的Logging → Writing to logs部分:'CakeLog ::写(” info',$ model。$ logEntry);' – jimmymadon

+0

很确定我上面的评论不应该有所作为。这两种方式都适用于我。如果你使用'$ this-> Request-> log(“$ model $ logEntry”,'info');'? – jimmymadon

回答

0

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

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

+0

我现在看到问题了。我确实已经有了日志设置,并且它一直写入日志,但有一组不同的日志。在Controller/Model中,它写入AppName/tmp/logs,但在Shell中写入AppName/app/tmp/logs。 – DuaneW

0

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

CakeLog不会自动进行自我配置了。因此如果没有流正在侦听,日志文件 将不会自动创建。确保 你至少得到了一个default流设置,如果你想听 所有类型和水平。通常情况下,你可以设置核心FileLog类 输出到app/tmp/logs/

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

查看菜谱2.x的