我一直在使用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);
}
}
}
?>
我想你应该尝试的Logging → Writing to logs部分:'CakeLog ::写(” info',$ model。$ logEntry);' – jimmymadon
很确定我上面的评论不应该有所作为。这两种方式都适用于我。如果你使用'$ this-> Request-> log(“$ model $ logEntry”,'info');'? – jimmymadon