LoggerPlugin.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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
  18. implements Swift_Events_CommandListener, Swift_Events_ResponseListener,
  19. Swift_Events_TransportChangeListener, Swift_Events_TransportExceptionListener,
  20. Swift_Plugins_Logger
  21. {
  22. /** The logger which is delegated to */
  23. private $_logger;
  24. /**
  25. * Create a new LoggerPlugin using $logger.
  26. *
  27. * @param Swift_Plugins_Logger $logger
  28. */
  29. public function __construct(Swift_Plugins_Logger $logger)
  30. {
  31. $this->_logger = $logger;
  32. }
  33. /**
  34. * Add a log entry.
  35. *
  36. * @param string $entry
  37. */
  38. public function add($entry)
  39. {
  40. $this->_logger->add($entry);
  41. }
  42. /**
  43. * Clear the log contents.
  44. */
  45. public function clear()
  46. {
  47. $this->_logger->clear();
  48. }
  49. /**
  50. * Get this log as a string.
  51. *
  52. * @return string
  53. */
  54. public function dump()
  55. {
  56. return $this->_logger->dump();
  57. }
  58. /**
  59. * Invoked immediately following a command being sent.
  60. *
  61. * @param Swift_Events_ResponseEvent $evt
  62. */
  63. public function commandSent(Swift_Events_CommandEvent $evt)
  64. {
  65. $command = $evt->getCommand();
  66. $this->_logger->add(sprintf(">> %s", $command));
  67. }
  68. /**
  69. * Invoked immediately following a response coming back.
  70. *
  71. * @param Swift_Events_ResponseEvent $evt
  72. */
  73. public function responseReceived(Swift_Events_ResponseEvent $evt)
  74. {
  75. $response = $evt->getResponse();
  76. $this->_logger->add(sprintf("<< %s", $response));
  77. }
  78. /**
  79. * Invoked just before a Transport is started.
  80. *
  81. * @param Swift_Events_TransportChangeEvent $evt
  82. */
  83. public function beforeTransportStarted(Swift_Events_TransportChangeEvent $evt)
  84. {
  85. $transportName = get_class($evt->getSource());
  86. $this->_logger->add(sprintf("++ Starting %s", $transportName));
  87. }
  88. /**
  89. * Invoked immediately after the Transport is started.
  90. *
  91. * @param Swift_Events_TransportChangeEvent $evt
  92. */
  93. public function transportStarted(Swift_Events_TransportChangeEvent $evt)
  94. {
  95. $transportName = get_class($evt->getSource());
  96. $this->_logger->add(sprintf("++ %s started", $transportName));
  97. }
  98. /**
  99. * Invoked just before a Transport is stopped.
  100. *
  101. * @param Swift_Events_TransportChangeEvent $evt
  102. */
  103. public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt)
  104. {
  105. $transportName = get_class($evt->getSource());
  106. $this->_logger->add(sprintf("++ Stopping %s", $transportName));
  107. }
  108. /**
  109. * Invoked immediately after the Transport is stopped.
  110. *
  111. * @param Swift_Events_TransportChangeEvent $evt
  112. */
  113. public function transportStopped(Swift_Events_TransportChangeEvent $evt)
  114. {
  115. $transportName = get_class($evt->getSource());
  116. $this->_logger->add(sprintf("++ %s stopped", $transportName));
  117. }
  118. /**
  119. * Invoked as a TransportException is thrown in the Transport system.
  120. *
  121. * @param Swift_Events_TransportExceptionEvent $evt
  122. */
  123. public function exceptionThrown(Swift_Events_TransportExceptionEvent $evt)
  124. {
  125. $e = $evt->getException();
  126. $message = $e->getMessage();
  127. $this->_logger->add(sprintf("!! %s", $message));
  128. $message .= PHP_EOL;
  129. $message .= 'Log data:' . PHP_EOL;
  130. $message .= $this->_logger->dump();
  131. $evt->cancelBubble();
  132. throw new Swift_TransportException($message);
  133. }
  134. }