goszowski/laravel-database-log-channel

Laravel database log channel

v2.0.0 2021-12-21 15:13 UTC

This package is auto-updated.

Last update: 2024-12-21 21:31:33 UTC


README

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

The package provides the ability to write logs into the database synchronously or asynchronously, along with other logging channels.

Installation

You can install the package via composer:

composer require goszowski/laravel-database-log-channel

You can publish and run the migrations with:

php artisan vendor:publish --tag="database-log-channel-migrations"
php artisan migrate

Configure logging.php:

return [
    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily', 'database'], // Add "database" channel
            'ignore_exceptions' => false,
        ],

        // ...
        'database' => [
            'driver' => 'monolog',
            'handler' => Goszowski\DatabaseLogChannel\Logging\DatabaseLogHandler::class,
            'via' => Goszowski\DatabaseLogChannel\Logging\DatabaseLogger::class,

            'alternative-log-channel' => 'daily', // Use an alternate channel when it is not possible to write to the database
            'connection' => null, // Use default connection
            'table' => 'logs',
            'async' => true, // If true, will be sent to the queue
            'queue' => 'default', // Define a queue for asynchronous logging
            'level' => 'error',
        ],

    ],
];

Usage

use Log;

Log::error('My error message');

Data Pruning

Without pruning, the logs table can accumulate records very quickly. To mitigate this, you should schedule the database-logs:prune Artisan command to run daily:

$schedule->command('database-logs:prune')->daily();

By default, all entries older than 24 hours will be pruned. You may use the hours option when calling the command to determine how long to retain Logs data. For example, the following command will delete all records created over 48 hours ago:

$schedule->command('database-logs:prune --hours=48')->daily();

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.