Created
April 14, 2014 08:06
-
-
Save gridsane/10626340 to your computer and use it in GitHub Desktop.
Symfony + Monolog, sending logs to email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
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
Monolog must be at least 1.8.0, to use
monolog.formatter.html