- Logging design pattern in a Zend framework application
- function SendMailsAction(){
- $logger->log('Started sending mails.')
- ...
- ...
- ...Some Code...
- ...
- ...
- foreach ($mails as $mail){
- try{
- $logger->log('Trying to send')
- $mail->send()
- $logger->log('Mail sent successfully.')
- }catch(Exception $e){
- $logger->log('Failed to send mail.')
- }
- }
- ...
- ...
- ...Some Code...
- ...
- ...
- $logger->log('Finished sending mails.')
- }
- <?php
- namespace Foo;
- class MailService {
- public function attach( Observes $observer ) {
- $this->observers[] = $observer;
- }
- public function notify( $message, $type = 'notice' ) {
- foreach( $this->observers as $observer ) {
- $observer->notify( $message, $type );
- }
- }
- public function sendMail( ) {
- $this->notify( 'Started sending mails', 'debug' );
- $mails = array( );
- foreach( $mails as $mail ) {
- try {
- $this->notify( 'Trying to send', 'debug' );
- $mail->send( );
- $this->notify( 'Mail sent succesfully', 'debug' );
- }
- catch( Exception $e ) {
- $this->notify( 'Failed to send mail', 'notice' );
- }
- }
- $this->notify( 'Finished sending mail', 'debug' );
- }
- }
- interface Observes {
- public function notify( $message, $type = 'notice' );
- }
- abstract class Logger implements Observes {
- protected $types = array(
- 'debug' => 0,
- 'notice' => 1,
- 'warning' => 2,
- 'error' => 3
- );
- protected function code( $type ) {
- return isset( $this->types[$type] ) ? $this->types[$type] : 0;
- }
- }
- class FileLogger extends Logger implements Observes {
- public function __construct( $filename ) {
- $this->filename = $filename;
- }
- /**
- * @todo replace the method body with a call to, say, file_put_contents.
- */
- public function notify( $message, $type = 'notice' ) {
- if( $this->code( $type ) > $this->code( 'notice' ) ) { // only for warning and error.
- echo $message . "n";
- }
- }
- }
- class DebugLogger extends Logger implements Observes {
- public function notify( $message, $type = 'notice' ) {
- if( $this->code( $type ) === $this->code( 'debug' ) ) { // only show "debug" notices.
- echo $message . "n";
- }
- }
- }
- $service = new MailService( );
- $service->attach( new FileLogger( 'yourlog.txt' ) );
- $service->attach( new DebugLogger( ) );
- $service->sendMail( );
- //use register_shutdown_function to register your logger function
- function scanTrace(Zend_Log $logger, array $eventMap)
- {
- $trace = array_reverse(debug_backtrace());
- foreach ($trace as $step)
- {
- //1. extract the needed info
- //2. check if the event is in your eventMap
- //3. if yes, log it
- }
- }
- class logger {
- public function log ($value , $type , $bothlogger = false){
- $writer1 = new Zend_Log_Writer_Stream('/path/to/first/logfile');
- $writer2 = new Zend_Log_Writer_Stream('/path/to/second/logfile');
- $logger = new Zend_Log();
- $logger->addWriter($writer1);
- if($bothlogger === true){
- $logger->addWriter($writer2);
- }
- // goes to both writers
- $logger->info('Informational message');
- return true;
- }
- }