ArrayLogger.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_Plugins_Loggers_ArrayLogger implements Swift_Plugins_Logger
  15. {
  16. /**
  17. * The log contents.
  18. *
  19. * @var array
  20. */
  21. private $_log = array();
  22. /**
  23. * Max size of the log.
  24. *
  25. * @var int
  26. */
  27. private $_size = 0;
  28. /**
  29. * Create a new ArrayLogger with a maximum of $size entries.
  30. *
  31. * @var int $size
  32. */
  33. public function __construct($size = 50)
  34. {
  35. $this->_size = $size;
  36. }
  37. /**
  38. * Add a log entry.
  39. *
  40. * @param string $entry
  41. */
  42. public function add($entry)
  43. {
  44. $this->_log[] = $entry;
  45. while (count($this->_log) > $this->_size) {
  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. *
  59. * @return string
  60. */
  61. public function dump()
  62. {
  63. return implode(PHP_EOL, $this->_log);
  64. }
  65. }