goszowski / laravel-database-log-channel
Laravel database log channel
Installs: 2 791
Dependents: 0
Suggesters: 0
Security: 0
Stars: 15
Watchers: 3
Forks: 2
Open Issues: 0
Requires
- php: ^7.0|^7.4|^8.0
- illuminate/contracts: ^7.0|^7.4|^8.37
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^5.10
- nunomaduro/larastan: ^1.0
- orchestra/testbench: ^6.22
- pestphp/pest: ^1.10
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.4
- spatie/laravel-ray: ^1.26
README
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.