Scrivere registri da console shell
-
21-12-2019 - |
Domanda
Ho usato CakePhp 2.4.4 per costruire la parte web interattiva della mia app e questo sta andando molto bene.CakePHP è fantastico.
Ora sto facendo alcuni processi di sfondo a supporto.La console e la conchiglia sembra essere il modo di farlo come ha accesso ai modelli.
Ho scritto il codice e averlo funzionato ma sto cercando di scrivere allo stesso registro che uso per i modelli.Nei modelli ho una funzione dopoversa per registrare tutte le modifiche del database e ho appena usato il $this->log("$model $logEntry", 'info');
per scrivere sul registro.
che non funziona nella shell ma pensavo che il CakeLog::write('info', "$model $logEntry");
possa funzionare ma non è nemmeno.
Devo inizializzare il cakelog per puntare ai file di registro corretti?
<?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);
}
}
}
?>
. Soluzione
Hai impostato un ascoltatore / ambito predefinito per i tipi di registro? In caso contrario, non verranno registrati.
// Setup a 'default' cache configuration for use in the application.
Cache::config('default', array('engine' => 'File'));
.
nel tuo bootstrap.php per esempio
Vedi http://book.cakephp.org / 2.0 / it / appendici / 2-2-Migration-Guide.html # log
Altri suggerimenti
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 onedefault
stream set up, if you want to listen to all types and levels. Usually, you can just set the coreFileLog
class to output intoapp/tmp/logs/
:CakeLog::config('default', array( 'engine' => 'File' ));
See Logging → Writing to logs section of the CookBook 2.x