PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/swift/Swift/Log.php

https://bitbucket.org/enurkov/prestashop
PHP | 152 lines | 54 code | 3 blank | 95 comment | 0 complexity | 9c3be6a3428b14115d9df4af63fff639 MD5 | raw file
  1. <?php
  2. /**
  3. * Swift Mailer Logging Layer base class.
  4. * Please read the LICENSE file
  5. * @author Chris Corbyn <chris@w3style.co.uk>
  6. * @package Swift_Log
  7. * @license GNU Lesser General Public License
  8. */
  9. /**
  10. * The Logger class/interface.
  11. * @package Swift_Log
  12. * @author Chris Corbyn <chris@w3style.co.uk>
  13. */
  14. abstract class Swift_Log
  15. {
  16. /**
  17. * A command type entry
  18. */
  19. const COMMAND = ">>";
  20. /**
  21. * A response type entry
  22. */
  23. const RESPONSE = "<<";
  24. /**
  25. * An error type entry
  26. */
  27. const ERROR = "!!";
  28. /**
  29. * A standard entry
  30. */
  31. const NORMAL = "++";
  32. /**
  33. * Logging is off.
  34. */
  35. const LOG_NOTHING = 0;
  36. /**
  37. * Only errors are logged.
  38. */
  39. const LOG_ERRORS = 1;
  40. /**
  41. * Errors + sending failures.
  42. */
  43. const LOG_FAILURES = 2;
  44. /**
  45. * All SMTP instructions + failures + errors.
  46. */
  47. const LOG_NETWORK = 3;
  48. /**
  49. * Runtime info + SMTP instructions + failures + errors.
  50. */
  51. const LOG_EVERYTHING = 4;
  52. /**
  53. * Failed recipients
  54. * @var array
  55. */
  56. protected $failedRecipients = array();
  57. /**
  58. * The maximum number of log entries
  59. * @var int
  60. */
  61. protected $maxSize = 50;
  62. /**
  63. * The level of logging currently set.
  64. * @var int
  65. */
  66. protected $logLevel = self::LOG_NOTHING;
  67. /**
  68. * Add a new entry to the log
  69. * @param string The information to log
  70. * @param string The type of entry (see the constants: COMMAND, RESPONSE, ERROR, NORMAL)
  71. */
  72. abstract public function add($text, $type = self::NORMAL);
  73. /**
  74. * Dump the contents of the log to the browser.
  75. * @param boolean True if the string should be returned rather than output.
  76. */
  77. abstract public function dump($return_only=false);
  78. /**
  79. * Empty the log contents
  80. */
  81. abstract public function clear();
  82. /**
  83. * Check if logging is enabled.
  84. */
  85. public function isEnabled()
  86. {
  87. return ($this->logLevel > self::LOG_NOTHING);
  88. }
  89. /**
  90. * Add a failed recipient to the list
  91. * @param string The address of the recipient
  92. */
  93. public function addFailedRecipient($address)
  94. {
  95. $this->failedRecipients[$address] = null;
  96. $this->add("Recipient '" . $address . "' rejected by connection.", self::ERROR);
  97. }
  98. /**
  99. * Get the list of failed recipients
  100. * @return array
  101. */
  102. public function getFailedRecipients()
  103. {
  104. return array_keys($this->failedRecipients);
  105. }
  106. /**
  107. * Set the maximum size of this log (zero is no limit)
  108. * @param int The maximum entries
  109. */
  110. public function setMaxSize($size)
  111. {
  112. $this->maxSize = (int) $size;
  113. }
  114. /**
  115. * Get the current maximum allowed log size
  116. * @return int
  117. */
  118. public function getMaxSize()
  119. {
  120. return $this->maxSize;
  121. }
  122. /**
  123. * Set the log level to one of the constants provided.
  124. * @param int Level
  125. */
  126. public function setLogLevel($level)
  127. {
  128. $level = (int)$level;
  129. $this->add("Log level changed to " . $level, self::NORMAL);
  130. $this->logLevel = $level;
  131. }
  132. /**
  133. * Get the current log level.
  134. * @return int
  135. */
  136. public function getLogLevel()
  137. {
  138. return $this->logLevel;
  139. }
  140. /**
  141. * Check if the log level includes the one given.
  142. * @param int Level
  143. * @return boolean
  144. */
  145. public function hasLevel($level)
  146. {
  147. return ($this->logLevel >= ((int)$level));
  148. }
  149. }