Skip to content

Instantly share code, notes, and snippets.

@hissy
Last active July 24, 2018 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hissy/7aef3d2e47d491fc2658848e1b1d09e0 to your computer and use it in GitHub Desktop.
Save hissy/7aef3d2e47d491fc2658848e1b1d09e0 to your computer and use it in GitHub Desktop.
[concrete5] [v8]Add logging handler
<?php
/** @var \Concrete\Core\Logging\Logger $logger */
$logger = $app->make('log');
$logger->pushHandler(new \Application\Logging\Handler\ConcreteMailerHandler(
'example@example.com', 'Error Notification', 'example@example.com', \Concrete\Core\Logging\Logger::ERROR));
<?php
$classLoader = new \Symfony\Component\ClassLoader\Psr4ClassLoader();
$classLoader->addPrefix('Application\\Logging', DIR_APPLICATION . '/' . DIRNAME_CLASSES . '/Logging');
$classLoader->register();
<?php
namespace Application\Logging\Handler;
use Concrete\Core\Logging\Logger;
use Concrete\Core\Mail\Service;
use Concrete\Core\Support\Facade\Application;
use Monolog\Handler\MailHandler;
class ConcreteMailerHandler extends MailHandler
{
protected $to;
protected $subject;
protected $from;
public function __construct($to, $subject, $from, $level = Logger::ERROR, $bubble = true)
{
parent::__construct($level, $bubble);
$this->to = is_array($to) ? $to : array($to);
$this->subject = $subject;
$this->from = $from;
}
/**
* @inheritDoc
*/
protected function send($content, array $records)
{
$app = Application::getFacadeApplication();
/** @var Service $mh */
$mh = $app->make('mail');
foreach ($this->to as $to) {
$mh->to($to);
}
if ($this->subject) {
$mh->setSubject($this->subject);
}
if ($this->from) {
$mh->from($this->from);
}
$mh->setBody($content);
$mh->sendMail();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment