ArrayLogger.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. array_shift($this->_log);
  46. }
  47. }
  48. /**
  49. * Clear the log contents.
  50. */
  51. public function clear()
  52. {
  53. $this->_log = array();
  54. }
  55. /**
  56. * Get this log as a string.
  57. * @return string
  58. */
  59. public function dump()
  60. {
  61. return implode(PHP_EOL, $this->_log);
  62. }
  63. }