This package will log errors into MySQL database instead storage/log/laravel.log file.
composer require markhilton/monolog-mysql
Open up config/app.php
and find the providers
key.
'providers' => array(
// ...
Logger\Laravel\Provider\MonologMysqlHandlerServiceProvider::class,
);
Publish config using Laravel Artisan CLI.
php artisan vendor:publish
Migrate tables - you may want to configure enviornment beforehand.
php artisan migrate
In your application config/logging.php
add:
use Logger\Monolog\Handler\MysqlHandler;
// ...
'channels' => [
// ...
'mysql' => [
'driver' => 'monolog',
'handler' => MysqlHandler::class,
'level' => 'debug',
],
];
In your application config/logging.php
add:
<?php
// [...]
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['mysql'],
],
// [...]
'mysql' => [
'driver' => 'custom',
'via' => App\Logging\CreateMySQLLogger::class,
],
],
In your application app/Logging/CreateMySQLLogger.php
add:
<?php
namespace App\Logging;
use Exception;
use Monolog\Logger;
use Logger\Monolog\Handler\MysqlHandler;
class CreateMySQLLogger
{
/**
* Create a custom Monolog instance.
*
* @param array $config
* @return Logger
* @throws Exception
*/
public function __invoke(array $config)
{
$channel = $config['name'] ?? env('APP_ENV');
$monolog = new Logger($channel);
$monolog->pushHandler(new MysqlHandler());
return $monolog;
}
}
If you wish to change default table name to write the log into or database connection use following definitions in your .env file
DB_LOG_TABLE=logs
DB_LOG_CONNECTION=mysql
Based on:
- [Pedro Fornaza] (https://github.com/pedrofornaza/monolog-mysql)