Skip to content

Instantly share code, notes, and snippets.

@andybeak
Created May 11, 2016 08:35
Show Gist options
  • Save andybeak/fc40ba25dd5e61a69b18078db2fb6a74 to your computer and use it in GitHub Desktop.
Save andybeak/fc40ba25dd5e61a69b18078db2fb6a74 to your computer and use it in GitHub Desktop.
Laravel 5.2 Fingers Crossed logging setup
// set up default logging in Laravel 5.2
$formatter = new Monolog\Formatter\LineFormatter( "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\r\n", null, false, true);
if ($config->get('app.debug')) {
// we're in debug mode, so log everything
$handler = new Monolog\Handler\RotatingFileHandler(storage_path() . DIRECTORY_SEPARATOR . 'logs/laravel.log', Monolog\Logger::DEBUG);
$handler->setFormatter($formatter);
$monolog->pushHandler($handler);
} else {
// we're not in debug mode, so use fingers crossed logging (https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/FingersCrossedHandler.php)
$handler = new Monolog\Handler\RotatingFileHandler(storage_path() . DIRECTORY_SEPARATOR . 'logs/laravel.log', Monolog\Logger::DEBUG);
$handler->setFormatter($formatter);
$fingersCrossedHandler = new Monolog\Handler\FingersCrossedHandler($handler, null, 0 , false);
$monolog->pushHandler($fingersCrossedHandler);
}
<?php namespace Bootstrap;
// Laravel 5.1 custom logging file to be called from Kernel in place of the default logging bootstrap
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\FingersCrossedHandler;
use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseConfigureLogging;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\LogglyHandler;
class ConfigureLogging extends BaseConfigureLogging
{
protected function configureDailyHandler(Application $app, Writer $monolog)
{
/*
|--------------------------------------------------------------------------
| Setup custom Monolog handling
|--------------------------------------------------------------------------
|
| We push error logs to Loggly. This allows us to create custom alarms and
| monitor application errors alongside errors that occur elsewhere in the
| stack.
|
*/
$logger = $monolog->getMonolog();
// log more severe messages to loggly
$config = $app->make('config');
$logglyToken = $config->get('app.loggly_token');
$tag = 'application-' . $config->get('app.base_domain');
$logger->pushHandler(new LogglyHandler($logglyToken . '/tag/' . $tag, Logger::ERROR));
// set up default logging
$formatter = new LineFormatter( "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\r\n", null, false, true);
if ($config->get('app.debug')) {
// we're in debug mode, so log everything
$handler = new RotatingFileHandler(storage_path() . DIRECTORY_SEPARATOR . 'logs/laravel.log', Logger::DEBUG);
$handler->setFormatter($formatter);
$logger->pushHandler($handler);
} else {
// we're not in debug mode, so use fingers crossed logging (https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/FingersCrossedHandler.php)
$handler = new RotatingFileHandler(storage_path() . DIRECTORY_SEPARATOR . 'logs/laravel.log', Logger::DEBUG);
$handler->setFormatter($formatter);
$fingersCrossedHandler = new FingersCrossedHandler($handler, null, 0 , false);
$logger->pushHandler($fingersCrossedHandler);
}
}
}
// laravel 5.1 kernel (http and console) to bootstrap using the custom ConfigureLogging class
public function __construct(Application $app, Router $router)
{
parent::__construct($app, $router);
array_walk($this->bootstrappers, function(&$bootstrapper)
{
if($bootstrapper === 'Illuminate\Foundation\Bootstrap\ConfigureLogging')
{
$bootstrapper = 'Bootstrap\ConfigureLogging';
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment