LoggerPlugin.php 3.8KB

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