【发布时间】: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->Request->log("$model $logEntry", 'info');,您会得到相同的行为吗?
标签: shell cakephp cakephp-2.4