yuriitatur/laravel-logger-boost

A simple package for boosting your logging experience in Laravel

v1.0.0 2025-07-27 12:26 UTC

This package is auto-updated.

Last update: 2025-07-27 13:43:54 UTC


README

Quality Gate Status Coverage

Laravel logger boost

Add useful processors to your app.

Installation

composer require yuriitatur/laravel-logger-boost

Testing

composer test

Usage

Disclaimer

This package is designed to work with a Laravel framework, but it can be used with/without any framework as the set of useful Monolog processors.

Laravel

Add BoostLogger to your channel config in config/logging.php

'channels' => [
    'single' => [
        'driver' => 'single',
        'tap' => [\YuriiTatur\MonologHandler\BoostLogger::class],
        'path' => storage_path('logs/laravel.log'),
        'level' => env('LOG_LEVEL', 'debug'),
    ],
],

Or you can add even more context with AddMoreContext

'channels' => [
    'single' => [
        'driver' => 'single',
        'tap' => [
            \YuriiTatur\MonologHandler\BoostLogger::class,
            \YuriiTatur\MonologHandler\AddMoreContext::class
        ],
        'path' => storage_path('logs/laravel.log'),
        'level' => env('LOG_LEVEL', 'debug'),
    ],
],

This will add multiple extra fields like hostname, is_cli, route or user. Thus, returning all that data in each record is overhead. So make use of a stack driver with bubble option

 'channels' => [

        'stack' => [
            'driver' => 'stack',
            'channels' => [ // this is our main channels
                'single-error', // single-error channel will not bubble message to single
                'single',
            ],
            'ignore_exceptions' => false,
        ],

        'single' => [ // all debug+ messages goes with only BoostLogger
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'formatter' => JsonFormatter::class,
            'tap' => [
                BoostLogger::class,
                BufferRecords::class . ':10', // optionally enable record buffering, with specified buffer size
            ],
        ],

        'single-error' => [ // if something happens, use AddMoreContext and bubble => false
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'error',
            'bubble' => false, // if this is true, the message will be duplicated
            'formatter' => JsonFormatter::class,
            'tap' => [
                BoostLogger::class,
                AddMoreContext::class,
            ],
        ],
}

Eloquent

You can also add log key to your eloquent models, to keep track of all that happened to them with one key. First, add log_key column to your table;

# in your migration
Schema::table('my_models', function (Blueprint $table) {
    $table->string('log_key')->nullable()->unique();
});

The reason, why it's not in a separate thing called AddLogKeyColumn class/method is because, requirements to this column may vary. Create your own how you like it. Next, add LogKeyAwareTrait trait in your model. You can use overtakeLogs and applyLogKey methods now.

# in your model
class Product extends Model 
{
    use LogKeyAwareTrait;
}

# in your service
public function createProduct($params) 
{
    $product = new Product($params);
    $product->applyLogKey(); # creating event is a good place to put this 
    $product->save();
}

public function updateProduct($id, $params) 
{
    $product = Product::findOrFail($id);
    $product->overtakeLogs(); # this will take log key from the model and apply to your logger 
    $product->update($params);
}

Queue Jobs

This package automatically registers new LoggerAwareWorkerDecorator queue worker. That now resets all processors between each job, this is done specifically to reload log keys, so each job can be traced using its own id.

License

This code is under MIT license, read more in the LICENSE file.