質問

私は私のアプリの対話型Web部分を構築するためにCakePHP 2.4.4を使用しています。CakePHPは素晴らしいです。

私は今支援の背景プロセスをやっています。コンソールとシェルは、モデルにアクセスできるため、それを行う方法のようです。

コードを書いて、それを機能させましたが、私はモデルに使用するのと同じログに書き込もうとしています。モデルでは、すべてのデータベースの変更を記録するためのAftersave関数があり、$this->log("$model $logEntry", 'info');を使用してログに書き込むだけです。

シェルでは機能しませんが、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 /付録/ 2-2-migration-gualid.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