ArrayLogger.php 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. * Logs to an Array backend.
  11. * @package Swift
  12. * @subpackage Transport
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Plugins_Loggers_ArrayLogger implements Swift_Plugins_Logger
  16. {
  17. /**
  18. * The log contents.
  19. * @var array
  20. * @access private
  21. */
  22. private $_log = array();
  23. /**
  24. * Max size of the log.
  25. * @var int
  26. * @access private
  27. */
  28. private $_size = 0;
  29. /**
  30. * Create a new ArrayLogger with a maximum of $size entries.
  31. * @var int $size
  32. */
  33. public function __construct($size = 50)
  34. {
  35. $this->_size = $size;
  36. }
  37. /**
  38. * Add a log entry.
  39. * @param string $entry
  40. */
  41. public function add($entry)
  42. {
  43. $this->_log[] = $entry;
  44. while (count($this->_log) > $this->_size)
  45. {
  46. array_shift($this->_log);
  47. }
  48. }
  49. /**
  50. * Clear the log contents.
  51. */
  52. public function clear()
  53. {
  54. $this->_log = array();
  55. }
  56. /**
  57. * Get this log as a string.
  58. * @return string
  59. */
  60. public function dump()
  61. {
  62. return implode(PHP_EOL, $this->_log);
  63. }
  64. }