Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 3.30 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Logging design pattern in a Zend framework application
  2. function SendMailsAction(){
  3.       $logger->log('Started sending mails.')
  4. ...
  5. ...
  6. ...Some Code...
  7. ...
  8. ...
  9.  
  10. foreach ($mails as $mail){
  11.   try{      
  12.        $logger->log('Trying to send')
  13.        $mail->send()
  14.        $logger->log('Mail sent successfully.')
  15.   }catch(Exception $e){
  16.        $logger->log('Failed to send mail.')
  17.       }      
  18.  }
  19.  
  20. ...
  21. ...
  22. ...Some Code...
  23. ...
  24. ...
  25.        $logger->log('Finished sending mails.')
  26. }
  27.        
  28. <?php
  29.  
  30. namespace Foo;
  31.  
  32. class MailService {
  33.     public function attach( Observes $observer ) {
  34.         $this->observers[] = $observer;
  35.     }
  36.  
  37.     public function notify( $message, $type = 'notice' ) {
  38.         foreach( $this->observers as $observer ) {
  39.             $observer->notify( $message, $type );
  40.         }
  41.     }
  42.  
  43.     public function sendMail( ) {
  44.         $this->notify( 'Started sending mails', 'debug' );
  45.         $mails = array( );
  46.         foreach( $mails as $mail ) {
  47.             try {
  48.                 $this->notify( 'Trying to send', 'debug' );
  49.                 $mail->send( );
  50.                 $this->notify( 'Mail sent succesfully', 'debug' );
  51.             }
  52.             catch( Exception $e ) {
  53.                 $this->notify( 'Failed to send mail', 'notice' );
  54.             }
  55.         }
  56.         $this->notify( 'Finished sending mail', 'debug' );
  57.     }
  58. }
  59.  
  60. interface Observes {
  61.     public function notify( $message, $type = 'notice' );
  62. }
  63.  
  64. abstract class Logger implements Observes {
  65.     protected $types = array(
  66.         'debug' => 0,
  67.         'notice' => 1,
  68.         'warning' => 2,
  69.         'error' => 3
  70.     );
  71.  
  72.     protected function code( $type ) {
  73.         return isset( $this->types[$type] ) ? $this->types[$type] : 0;
  74.     }
  75. }
  76.  
  77. class FileLogger extends Logger implements Observes {
  78.  
  79.     public function __construct( $filename ) {
  80.         $this->filename = $filename;
  81.     }
  82.  
  83.     /**
  84.      * @todo replace the method body with a call to, say, file_put_contents.
  85.      */
  86.     public function notify( $message, $type = 'notice' ) {
  87.         if( $this->code( $type ) > $this->code( 'notice' ) ) { // only for warning and error.
  88.             echo $message . "n";
  89.         }
  90.     }
  91.  
  92.  
  93. }
  94.  
  95. class DebugLogger extends Logger implements Observes {
  96.     public function notify( $message, $type = 'notice' ) {
  97.         if( $this->code( $type ) === $this->code( 'debug' ) ) { // only show "debug" notices.
  98.             echo $message . "n";
  99.         }
  100.     }
  101. }
  102.  
  103.  
  104. $service = new MailService( );
  105. $service->attach( new FileLogger( 'yourlog.txt' ) );
  106. $service->attach( new DebugLogger( ) );
  107. $service->sendMail( );
  108.        
  109. //use register_shutdown_function to register your logger function
  110.  
  111. function scanTrace(Zend_Log $logger, array $eventMap)
  112. {
  113.     $trace = array_reverse(debug_backtrace());
  114.  
  115.     foreach ($trace as $step)
  116.     {
  117.         //1. extract the needed info
  118.         //2. check if the event is in your eventMap
  119.         //3. if yes, log it
  120.     }
  121. }
  122.        
  123. class logger {
  124.   public function log ($value , $type  , $bothlogger = false){
  125.       $writer1 = new Zend_Log_Writer_Stream('/path/to/first/logfile');
  126.       $writer2 = new Zend_Log_Writer_Stream('/path/to/second/logfile');
  127.  
  128.       $logger = new Zend_Log();
  129.       $logger->addWriter($writer1);
  130.  
  131.       if($bothlogger === true){
  132.         $logger->addWriter($writer2);
  133.       }
  134.       // goes to both writers
  135.        $logger->info('Informational message');
  136.  
  137.        return true;
  138.   }
  139.  
  140. }