Using the Logger Plugin

The Logger Plugin – like all plugins – is added with the Mailer class' registerPlugin() method. It accepts an instance of Swift_Plugins_Logger in its constructor.

To use the Logger plugin:

  1. Create an instance of the Mailer using any Transport you choose.
  2. Create an instance of the a Logger implementation of Swift_Plugins_Logger.
  3. Create an instance of the Swift_Plugins_LoggerPlugin class, passing the created Logger instance to its constructor.
  4. Register the plugin using the Mailer's registerPlugin() method.
  5. Continue using Swift Mailer to send messages as normal.
  6. Dump the contents of the log with the logger's dump() method.

When Swift Mailer sends messages it will keep a log of all the interactions with the underlying Transport being used. Depending upon the Logger that has been used the behaviour will differ, but all implementations offer a way to get the contents of the log.

<?php

require_once 'lib/swift_required.php';

//Create the Mailer using any Transport
$mailer = Swift_Mailer::newInstance(
  Swift_SmtpTransport::newInstance('smtp.example.org', 25)
);

//To use the ArrayLogger
$logger = new Swift_Plugins_Loggers_ArrayLogger();
$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));

//Or to use the Echo Logger
$logger = new Swift_Plugins_Loggers_EchoLogger();
$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));

//Continue sending as normal
for ($lotsOfRecipients as $recipient) {
  ...
  
  $mailer->send( ... );
}

// Dump the log contents
// NOTE: The EchoLogger dumps in realtime so dump() does nothing for it
echo $logger->dump();


NOTE: Documentation for version 3 is in the wiki.