PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/swift/lib/classes/Swift/Plugins/LoggerPlugin.php

http://zoop.googlecode.com/
PHP | 160 lines | 65 code | 16 blank | 79 comment | 0 complexity | 0d82480281a06040fe46cdb7aeb04443 MD5 | raw file
  1<?php
  2
  3/*
  4 * This file is part of SwiftMailer.
  5 * (c) 2004-2009 Chris Corbyn
  6 *
  7 * For the full copyright and license information, please view the LICENSE
  8 * file that was distributed with this source code.
  9 */
 10
 11//@require 'Swift/Events/CommandListener.php';
 12//@require 'Swift/Events/CommandEvent.php';
 13//@require 'Swift/Events/ResponseListener.php';
 14//@require 'Swift/Events/ResponseEvent.php';
 15//@require 'Swift/Events/TransportChangeListener.php';
 16//@require 'Swift/Events/TransportChangeEvent.php';
 17//@require 'Swift/Events/TransportExceptionEvent.php';
 18//@require 'Swift/Events/TransportExceptionListener.php';
 19//@require 'Swift/Events/TransportException.php';
 20//@require 'Swift/Plugins/Logger.php';
 21
 22/**
 23 * Does real time logging of Transport level information.
 24 * 
 25 * @package Swift
 26 * @subpackage Plugins
 27 * 
 28 * @author Chris Corbyn
 29 */
 30class Swift_Plugins_LoggerPlugin
 31  implements Swift_Events_CommandListener, Swift_Events_ResponseListener,
 32  Swift_Events_TransportChangeListener, Swift_Events_TransportExceptionListener,
 33  Swift_Plugins_Logger
 34{
 35  
 36  /** The logger which is delegated to */
 37  private $_logger;
 38  
 39  /**
 40   * Create a new LoggerPlugin using $logger.
 41   * 
 42   * @param Swift_Plugins_Logger $logger
 43   */
 44  public function __construct(Swift_Plugins_Logger $logger)
 45  {
 46    $this->_logger = $logger;
 47  }
 48  
 49  /**
 50   * Add a log entry.
 51   * 
 52   * @param string $entry
 53   */
 54  public function add($entry)
 55  {
 56    $this->_logger->add($entry);
 57  }
 58  
 59  /**
 60   * Clear the log contents.
 61   */
 62  public function clear()
 63  {
 64    $this->_logger->clear();
 65  }
 66  
 67  /**
 68   * Get this log as a string.
 69   * 
 70   * @return string
 71   */
 72  public function dump()
 73  {
 74    return $this->_logger->dump();
 75  }
 76  
 77  /**
 78   * Invoked immediately following a command being sent.
 79   * 
 80   * @param Swift_Events_ResponseEvent $evt
 81   */
 82  public function commandSent(Swift_Events_CommandEvent $evt)
 83  {
 84    $command = $evt->getCommand();
 85    $this->_logger->add(sprintf(">> %s", $command));
 86  }
 87  
 88  /**
 89   * Invoked immediately following a response coming back.
 90   * 
 91   * @param Swift_Events_ResponseEvent $evt
 92   */
 93  public function responseReceived(Swift_Events_ResponseEvent $evt)
 94  {
 95    $response = $evt->getResponse();
 96    $this->_logger->add(sprintf("<< %s", $response));
 97  }
 98  
 99  /**
100   * Invoked just before a Transport is started.
101   * 
102   * @param Swift_Events_TransportChangeEvent $evt
103   */
104  public function beforeTransportStarted(Swift_Events_TransportChangeEvent $evt)
105  {
106    $transportName = get_class($evt->getSource());
107    $this->_logger->add(sprintf("++ Starting %s", $transportName));
108  }
109  
110  /**
111   * Invoked immediately after the Transport is started.
112   * 
113   * @param Swift_Events_TransportChangeEvent $evt
114   */
115  public function transportStarted(Swift_Events_TransportChangeEvent $evt)
116  {
117    $transportName = get_class($evt->getSource());
118    $this->_logger->add(sprintf("++ %s started", $transportName));
119  }
120  
121  /**
122   * Invoked just before a Transport is stopped.
123   * 
124   * @param Swift_Events_TransportChangeEvent $evt
125   */
126  public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt)
127  {
128    $transportName = get_class($evt->getSource());
129    $this->_logger->add(sprintf("++ Stopping %s", $transportName));
130  }
131  
132  /**
133   * Invoked immediately after the Transport is stopped.
134   * 
135   * @param Swift_Events_TransportChangeEvent $evt
136   */
137  public function transportStopped(Swift_Events_TransportChangeEvent $evt)
138  {
139    $transportName = get_class($evt->getSource());
140    $this->_logger->add(sprintf("++ %s stopped", $transportName));
141  }
142  
143  /**
144   * Invoked as a TransportException is thrown in the Transport system.
145   * 
146   * @param Swift_Events_TransportExceptionEvent $evt
147   */
148  public function exceptionThrown(Swift_Events_TransportExceptionEvent $evt)
149  {
150    $e = $evt->getException();
151    $message = $e->getMessage();
152    $this->_logger->add(sprintf("!! %s", $message));
153    $message .= PHP_EOL;
154    $message .= 'Log data:' . PHP_EOL;
155    $message .= $this->_logger->dump();
156    $evt->cancelBubble();
157    throw new Swift_TransportException($message);
158  }
159  
160}