-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwiftMailerPsrLoggerPlugin.php
228 lines (209 loc) · 7.12 KB
/
SwiftMailerPsrLoggerPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace gcrico\SwiftMailerPsrLoggerPlugin;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* Logs swiftmailer activity with a (PSR-3) logger.
*
* Warning! The logger must not sends emails with the same mailer, or you will run in a dead loop!
*
* Message sending success are logged with the INFO level.
* Message sending failure are logged with the ERROR level.
* TransportException are logged with ERROR level, before been rethrown.
* Plubing events (Commands, Responses, Transport) are logged with DEBUG level.
*
*/
class SwiftMailerPsrLoggerPlugin implements
\Swift_Events_SendListener,
\Swift_Events_CommandListener,
\Swift_Events_ResponseListener,
\Swift_Events_TransportChangeListener,
\Swift_Events_TransportExceptionListener
{
/**
* The PSR-3 logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* Prefix for messages.
*
* @var string
*/
protected $prefix = '[MAILER] ';
/**
* Map of events to log-levels.
*
* @var array
*/
protected $levels = array(
'sendPerformed.SUCCESS' => LogLevel::INFO,
'sendPerformed.TENTATIVE' => LogLevel::WARNING,
'sendPerformed.NOT_SUCCESS' => LogLevel::ERROR,
'sendPerformed.PENDING' => LogLevel::DEBUG,
'sendPerformed.SPOOLED' => LogLevel::DEBUG,
'exceptionThrown' => LogLevel::ERROR,
'beforeSendPerformed' => LogLevel::DEBUG,
'commandSent' => LogLevel::DEBUG,
'responseReceived' => LogLevel::DEBUG,
'beforeTransportStarted' => LogLevel::DEBUG,
'transportStarted' => LogLevel::DEBUG,
'beforeTransportStopped' => LogLevel::DEBUG,
'transportStopped' => LogLevel::DEBUG,
);
/**
* Constructor.
*
* @param LoggerInterface $logger
* @param array $levels
*/
public function __construct(LoggerInterface $logger, $levels=array())
{
$this->logger = $logger;
foreach ($levels as $event => $level) {
$this->levels[$event] = $level;
}
}
/**
* Adds the message prefix and invokes the logger->log() method.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return void
*/
protected function log($level, $message, array $context = array())
{
// Using a falsy level disables logging
if ($level) {
$this->logger->log($level, $this->prefix . $message, $context);
}
}
/**
* Invoked immediately before the Message is sent.
*
* @param \Swift_Events_SendEvent $evt
*/
public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
{
$level = $this->levels['beforeSendPerformed'];
$this->log($level, 'MESSAGE (beforeSend): ', array(
'message' => $evt->getMessage()->toString(),
));
}
/**
* Invoked immediately after the Message is sent.
*
* @param \Swift_Events_SendEvent $evt
*/
public function sendPerformed(\Swift_Events_SendEvent $evt)
{
$result = $evt->getResult();
$failed_recipients = $evt->getFailedRecipients();
$message = $evt->getMessage();
switch ($result){
case \Swift_Events_SendEvent::RESULT_PENDING:
$level = $this->levels['sendPerformed.PENDING'];
break;
case \Swift_Events_SendEvent::RESULT_SPOOLED:
$level = $this->levels['sendPerformed.SPOOLED'];
break;
case \Swift_Events_SendEvent::RESULT_TENTATIVE:
$level = $this->levels['sendPerformed.TENTATIVE'];
break;
case \Swift_Events_SendEvent::RESULT_SUCCESS:
$level = $this->levels['sendPerformed.SUCCESS'];
break;
default:
$level = $this->levels['sendPerformed.NOT_SUCCESS'];
break;
}
$this->log($level, 'MESSAGE (sendPerformed): ', array(
'result' => $result,
'failed_recipients' => $failed_recipients,
'message' => $message->toString(),
));
}
/**
* Invoked immediately following a command being sent.
*
* @param \Swift_Events_CommandEvent $evt
*/
public function commandSent(\Swift_Events_CommandEvent $evt)
{
$level = $this->levels['commandSent'];
$command = $evt->getCommand();
$this->log($level, sprintf(">> %s", $command));
}
/**
* Invoked immediately following a response coming back.
*
* @param \Swift_Events_ResponseEvent $evt
*/
public function responseReceived(\Swift_Events_ResponseEvent $evt)
{
$level = $this->levels['responseReceived'];
$response = $evt->getResponse();
$this->log($level, sprintf("<< %s", $response));
}
/**
* Invoked just before a Transport is started.
*
* @param \Swift_Events_TransportChangeEvent $evt
*/
public function beforeTransportStarted(\Swift_Events_TransportChangeEvent $evt)
{
$level = $this->levels['beforeTransportStarted'];
$transportName = get_class($evt->getSource());
$this->log($level, sprintf("++ Starting %s", $transportName));
}
/**
* Invoked immediately after the Transport is started.
*
* @param \Swift_Events_TransportChangeEvent $evt
*/
public function transportStarted(\Swift_Events_TransportChangeEvent $evt)
{
$level = $this->levels['transportStarted'];
$transportName = get_class($evt->getSource());
$this->log($level, sprintf("++ %s started", $transportName));
}
/**
* Invoked just before a Transport is stopped.
*
* @param \Swift_Events_TransportChangeEvent $evt
*/
public function beforeTransportStopped(\Swift_Events_TransportChangeEvent $evt)
{
$level = $this->levels['beforeTransportStopped'];
$transportName = get_class($evt->getSource());
$this->log($level, sprintf("++ Stopping %s", $transportName));
}
/**
* Invoked immediately after the Transport is stopped.
*
* @param \Swift_Events_TransportChangeEvent $evt
*/
public function transportStopped(\Swift_Events_TransportChangeEvent $evt)
{
$level = $this->levels['transportStopped'];
$transportName = get_class($evt->getSource());
$this->log($level, sprintf("++ %s stopped", $transportName));
}
/**
* Invoked as a TransportException is thrown in the Transport system.
*
* @param \Swift_Events_TransportExceptionEvent $evt
* @throws \Swift_TransportException
*/
public function exceptionThrown(\Swift_Events_TransportExceptionEvent $evt)
{
$e = $evt->getException();
$message = $e->getMessage();
$level = $this->levels['exceptionThrown'];
$this->log($level, sprintf("!! %s", $message));
$evt->cancelBubble();
throw new \Swift_TransportException($message);
}
}