Skip to content

Instantly share code, notes, and snippets.

@gridsane
Created April 14, 2014 08:06
Show Gist options
  • Save gridsane/10626340 to your computer and use it in GitHub Desktop.
Save gridsane/10626340 to your computer and use it in GitHub Desktop.
Symfony + Monolog, sending logs to email
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: grouped
grouped:
type: group
members: [streamed, buffered]
streamed:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: error
buffered:
type: buffer
handler: email
level: critical
email:
type: service
id: monolog.handler.swift_html
services:
monolog.handler.swift_html:
class: Foo\Bar\SwiftMailerHtmlHandler
arguments:
- @swiftmailer.mailer
- %email.robot% # from email address
- %email.admin% # to email address
- 'title goes here'
calls:
- [setFormatter, [@monolog.formatter.html]]
<?php
namespace Foo\Bar;
use Monolog\Handler\SwiftMailerHandler;
use Monolog\Logger;
/**
* SwiftMailerHtmlHandler uses Swift_Mailer to send emails with text/html body
*/
class SwiftMailerHtmlHandler extends SwiftMailerHandler
{
/**
* @param \Swift_Mailer $mailer The mailer to use
* @param string $from The address of sender
* @param string $to The address of receiver
* @param string $subject The subject of message
* @param integer $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct(\Swift_Mailer $mailer, $from, $to, $subject, $level = Logger::ERROR, $bubble = true)
{
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($to)
->setContentType('text/html');
parent::__construct($mailer, $message, $level, $bubble);
}
}
@gridsane
Copy link
Author

Monolog must be at least 1.8.0, to use monolog.formatter.html

@flip111
Copy link

flip111 commented Apr 29, 2014

Thanks so much, i hope you leave the gist public for a long time !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment