With this service provider you can add more details in the logs of app Laravel.
For example, add in your logs the soure Ip Address or memory usage for script, ...
php artisan make:provider LogProcessorServiceProvider
Write this content in the file app/Providers/LogProcessorServiceProvider.php:
<?php
namespace App\Providers;
use App;
use Monolog\Processor\GitProcessor;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
use Monolog\Processor\MemoryUsageProcessor;
class LogProcessorServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$log = $this->app->get('log');
$log->pushProcessor(function ($record) {
$record['extra']['ip'] = request()->ip();
if (Auth::check()) {
$record['extra']['user_id'] = Auth::id();
}
return $record;
});
if (config('app.debug')) {
$log->pushProcessor(new MemoryUsageProcessor());
}
if (App::environment('local')) {
$log->pushProcessor(new GitProcessor());
}
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
}
Edit file app/config.php and register this service provider:
'providers' => [
// ...
App\Providers\LogProcessorServiceProvider::class,
];
Now you can write a log and see the result:
logger('test');
[2018-07-12 23:10:03] local.DEBUG: test {"git":{"branch":"master","commit":"97b6af409b6acad5fcd8c71b695453b3f905771b"},"memory_usage":"10 MB","ip":"127.0.0.1"}
I think that be interesting in add this feature in Laravel framework, and permit define in config file, example:
config/logger.php
//...
use Monolog\Processor\GitProcessor;
use Monolog\Processor\WebProcessor;
'channels' => [
// ...
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'processors' => [
WebProcessor:class,
],
],
'single_debug' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'processors' => [
GitProcessor:class,
],
],
I was sent this proposal to maintainers Laravel Ideas
I hope you have a good acceptance to do the PR