I'd also like to see a laracast on best practices and ideas for logging
Advance Logging with Laravel and Monolog
Hello everyone.
At the moment my only configuration on Logging events is Log::useDailyFiles(storage_path().'/logs/laravel_main.log');
But I think that I can do more than that. My main goal is to separate logging files for example for Log::info i'll use a file called laravel_info.log for Log::error I'll use a file called laravel_error.log etc.
How can I achieve this with the built-in logging functionality?
True. Alright heres a direct answer to the question being asked in this thread (for L5):
You can override the Illuminate\Foundation\Bootstrap\ConfigureLogging Class in the Bootstrap directory (as the documentation states).
Steps are:
- Create a ConfigureLogging Class in the /bootstrap directory that implements the configureHandlers() method from Illuminate\Foundation\Bootstrap\ConfigureLogging.
- Open App/Console/Kernal.php. It extends
ConsoleKernal, which has a$bootstrappersattribute. Overwrite that attribute in App/Console/Kernal.php and register your new /bootstrap/ConfigureLogging class:
// App/Console/Kernal.php
...
class Kernel extends ConsoleKernel {
/**
* OVERRIDING PARENT CLASS
* The bootstrap classes for the application.
*
* @var array
*/
protected $bootstrappers = [
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
'Illuminate\Foundation\Bootstrap\LoadConfiguration',
'Illuminate\Foundation\Bootstrap\HandleExceptions',
'Illuminate\Foundation\Bootstrap\RegisterFacades',
'Illuminate\Foundation\Bootstrap\SetRequestForConsole',
'Illuminate\Foundation\Bootstrap\RegisterProviders',
'Illuminate\Foundation\Bootstrap\BootProviders',
'Bootstrap\ConfigureLogging' // custom logger bootstrapper
];
...
-
Do the same for
app/Http/Kernal.php - Register your new new bootstrap namespace in composer.json and run
composer dumpautoload. My namespace is Bootstrap.
// composer.json
...
"autoload": {
"psr-4": {
"Bootstrap\\": "bootstrap/"
}
},
...
- Implement the custom handlers you want in the
configureHandlersmethod in /bootstrap/ConfigureLogging. Something like:
// bootstrap/ConfigureLogging
<?php namespace Bootstrap;
use Monolog\Logger as Monolog;
use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseConfigureLogging;
use Monolog\Handler\StreamHandler;
class ConfigureLogging extends BaseConfigureLogging{
/**
* OVERRIDE PARENT
* Configure the Monolog handlers for the application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureHandlers(Application $app, Writer $log)
{
$bubble = false;
// Stream Handlers
$infoStreamHandler = new StreamHandler( storage_path("/logs/laravel_info.log"), Monolog::INFO, $bubble);
$warningStreamHandler = new StreamHandler( storage_path("/logs/laravel_warning.log"), Monolog::WARNING, $bubble);
$errorStreamHandler = new StreamHandler( storage_path("/logs/laravel_error.log"), Monolog::ERROR, $bubble);
// Get monolog instance and push handlers
$monolog = $log->getMonolog();
$monolog->pushHandler($infoStreamHandler);
$monolog->pushHandler($warningStreamHandler);
$monolog->pushHandler($errorStreamHandler);
$log->useDailyFiles($app->storagePath().'/logs/daily.log');
}
One note on that snippet, the order you push the streamhandlers on is important: push the most restrictive on last, so that it is hit first. Otherwise, say if you pushed the infoStreamHandler on last, it would log everything above it also and an error wouldn't make it to the error handler.
This will result in 3 log files:
- laravel_error.log - just errors
- laravel_info.log - just info
- laravel_warning.log - just warnings *daily log file - all
A related question was asked which I also answered.
Please or to participate in this conversation.