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