Question

I have been using CakePHP 2.4.4 to build the interactive web part of my app and that is going very well. CakePHP is awesome.

I am now doing some supporting background processes. The Console and Shell seems to be the way to do it as it has access to the Models.

I have written the code and have it working but I am trying to write to the same log that I use for the Models. In the models I have an afterSave function to log all the database changes and I just used the $this->log("$model $logEntry", 'info'); to write to the log.

That doesn't work in the Shell but I thought the CakeLog::write('info', "$model $logEntry"); might work but it doesn't either.

Do I need to initialise the CakeLog to point to the correct log files?

<?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);
        }
    }
}
?>
Was it helpful?

Solution

Did you set up a default listener/scope for those log types? If not, they will not get logged.

// Setup a 'default' cache configuration for use in the application.
Cache::config('default', array('engine' => 'File'));

In your bootstrap.php for example

See http://book.cakephp.org/2.0/en/appendices/2-2-migration-guide.html#log

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top