Creare i log con Zend



ritratto di mpagliarulo

Per creare un sistema di log con Zend Framework procedere come segue

//Creare un oggetto Zend_Log_Formatter_Simple per formattare i messaggi di log

$logFormat = new Zend_Log_Formatter_Simple('%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL);

// creare un writer del log, l'oggetto Zend_Log_Writer_Stream scrive su uno stream che può essere un file, l'output php 'PHP://output'

// oppure uno stream php 'PHP://nome_stream'

$writerLog = new Zend_Log_Writer_Stream(<percorso e nome file>);

// creare l'oggetto Zend_Log

$log = new Zend_Log();

//si applica la formattazione al writer

$writerLog->setFormatter($logFormat);

//si aggiunge il write al log, è possibile aggiungere n writer d un oggetto log

$log->addWriter($writerLog);

//per ogni messaggio di log basta richiamare il metodo log(messaggio, codice_priorità)

$log->log('messaggio', 3)

Le priorità predefinite in zend sono:

EMERG   = 0;  // Emergency: system is unusable
ALERT   = 1;  // Alert: action must be taken immediately
CRIT    = 2;  // Critical: critical conditions
ERR     = 3;  // Error: error conditions
WARN    = 4;  // Warning: warning conditions
NOTICE  = 5;  // Notice: normal but significant condition
INFO    = 6;  // Informational: informational messages
DEBUG   = 7;  // Debug: debug messages