Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturmamedov/ecb5a87bda86c3c179e749bd506ef6b0 to your computer and use it in GitHub Desktop.
Save arturmamedov/ecb5a87bda86c3c179e749bd506ef6b0 to your computer and use it in GitHub Desktop.
Laravel Logger configuration for Daily logs, Email send on Warning Level, and optionally (comment out and add on setFormatter()) HTML Formater
<?php
// Put at the top of ./config/app.php
// Custom Monolog Configuration - https://laravel.com/docs/5.2/errors#configuration
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Illuminate\Mail\TransportManager;
$app->configureMonologUsing(function($monolog) use ($app) {
// * Email - Send email on error in production
if ($app['config']['app']['env'] === 'production')
{
// http://swiftmailer.org/docs/sending.html
$from = $app['config']['mail']['from'];
$developer = $app['config']['mail']['developer'];
$transport = new TransportManager($app);
$mailer = new Swift_Mailer($transport->driver());
$monolog->pushHandler(new Monolog\Handler\SwiftMailerHandler(
$mailer,
Swift_Message::newInstance('[Log] Warning - WiFi Mail')
->setFrom($from['address'], $from['name'])
->setTo($developer['address'], $developer['name']),
Logger::WARNING,
true
));
}
// * HTML - Formatter
// the default date format is "Y-m-d H:i:s" // the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
// $dateFormat = "Y-m-d H:i:s"; // $output = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
// finally, create a formatter
// $formatter = new Monolog\Formatter\HtmlFormatter();
// * FileRotator - Daily file rotator
// handler init, making days separated logs
$handler = new Monolog\Handler\RotatingFileHandler(storage_path('logs/laravel.log'), 0, Logger::DEBUG);
// formatter, ordering log rows
$handler->setFormatter(new Monolog\Formatter\LineFormatter());
// add handler to the logger
$monolog->pushHandler($handler);
// processor, adding URI, IP address etc. to the log
$monolog->pushProcessor(new Monolog\Processor\WebProcessor);
// processor, memory usage
$monolog->pushProcessor(new Monolog\Processor\MemoryUsageProcessor);
});
@abhi-mdotbuz
Copy link

abhi-mdotbuz commented Sep 23, 2016

I tried this but its not working.
Checked my config everything seems fine.. what might have gone wrong..? How ever I am not getting any errors

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