Skip to content

Instantly share code, notes, and snippets.

@cesargb
Last active April 5, 2023 14:18
Show Gist options
  • Save cesargb/d6ee27df4ce0c7e22ac7cc7c13daddba to your computer and use it in GitHub Desktop.
Save cesargb/d6ee27df4ce0c7e22ac7cc7c13daddba to your computer and use it in GitHub Desktop.
Laravel Log Processor

Laravel Log Add Processor

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, ...

Create a service provider

php artisan make:provider LogProcessorServiceProvider

Edit file app/Providers/LogProcessorServiceProvider.php

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()
    {

    }
}

Register the Service Provider in file config/app.php

Edit file app/config.php and register this service provider:

'providers' => [
    // ...
    App\Providers\LogProcessorServiceProvider::class,
];

Test

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"}

References

Proprosal

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,
            ],
        ],
<?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()
{
}
}
@ManojKiranA
Copy link

I think that be interesting in add this feature in Laravel framework, and permit define in config file, example:

Yeah its Cool Kindly make a PR for that. For the Next Release That is 6.0 on Sep 3 MayBe it will got merged

@cesargb
Copy link
Author

cesargb commented Aug 27, 2019

I think that be interesting in add this feature in Laravel framework, and permit define in config file, example:

Yeah its Cool Kindly make a PR for that. For the Next Release That is 6.0 on Sep 3 MayBe it will got merged

I was sent this proposal to maintainers Laravel Ideas
I hope you have a good acceptance to do the PR

@dnsbty
Copy link

dnsbty commented Apr 5, 2023

In case anyone else is searching for this, Laravel 10.x added this feature: https://laravel.com/docs/10.x/logging#monolog-processors

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