Skip to content

Instantly share code, notes, and snippets.

@adinauer
Created May 18, 2015 05:07
Show Gist options
  • Save adinauer/616438ebe1981e922492 to your computer and use it in GitHub Desktop.
Save adinauer/616438ebe1981e922492 to your computer and use it in GitHub Desktop.
Laravel JSON Logging
// add this to bootstrap/app.php to override the default logging configuration
$app->singleton(
'Illuminate\Foundation\Bootstrap\ConfigureLogging',
'App\Bootstrap\ConfigureLogging'
);
<?php namespace App\Bootstrap;
// app/Bootstrap/ConfigureLogging.php
use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseLoggingConfiguration;
use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\RotatingFileHandler;
class ConfigureLogging extends BaseLoggingConfiguration {
/**
* Configure the Monolog handlers for the application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureSingleHandler(Application $app, Writer $log)
{
//sets the path to custom app/log/single-xxxx-xx-xx.log file.
$log->useFiles(base_path() . '/log/single.log');
}
/**
* Configure the Monolog handlers for the application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureDailyHandler(Application $app, Writer $log)
{
//sets the path to custom app/log/daily-xxxx-xx-xx.log file.
$log->useDailyFiles(base_path() . '/logs/daily.log', 5);
$formatter = new JsonFormatter();
$handler = new RotatingFileHandler(base_path() . '/logs/daily.json', 5);
$handler->setFormatter($formatter);
$log->getMonolog()->pushHandler($handler);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment