<commit_before><?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
     */
    private $logger;

    /**
     * Prefix for messages.
     *
     * @var string
     */
    private $prefix = '[MAILER] ';

    /**
     * Map of events to log-levels.
     *
     * @var array
     */
    private $levels = array(
        'sendPerformed.SUCCESS'     => LogLevel::INFO,
        'sendPerformed.NOT_SUCCESS' => LogLevel::ERROR,
        '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->level[$event] = $level;
        }
    }

    /**
     * Adds the message prefix and invokes the logger->log() method.
     *
     * @param mixed $level
     * @param string $message
     * @param array $context
     * @return void
     */
    private 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)
    {<commit_msg>setting-levels fixed<commit_after><?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
     */
    private $logger;

    /**
     * Prefix for messages.
     *
     * @var string
     */
    private $prefix = '[MAILER] ';

    /**
     * Map of events to log-levels.
     *
     * @var array
     */
    private $levels = array(
        'sendPerformed.SUCCESS'     => LogLevel::INFO,
        'sendPerformed.NOT_SUCCESS' => LogLevel::ERROR,
        '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
     */
    private 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)
    {
{'fraction_non_alphanumeric': 0.11142533936651584, 'fraction_numerical': 0.0007541478129713424, 'mean_word_length': 2.3470031545741326, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5424352', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1507, 'n_words': 395}