fl0od13 / monolog-mysql
Laravel 8 MySQL 驱动程序用于 Monolog
0.2.2
2021-08-23 13:35 UTC
Requires
- php: ^7.0
README
此包将错误日志记录到 MySQL 数据库中,而不是 storage/log/laravel.log 文件。
安装
composer require markhilton/monolog-mysql
打开 config/app.php
并找到 providers
键。
'providers' => array( // ... Logger\Laravel\Provider\MonologMysqlHandlerServiceProvider::class, );
使用 Laravel Artisan CLI 发布配置。
php artisan vendor:publish
迁移表 - 在此之前,你可能需要配置环境。
php artisan migrate
应用程序集成
在你的应用程序 config/logging.php
中添加
use Logger\Monolog\Handler\MysqlHandler; // ... 'channels' => [ // ... 'mysql' => [ 'driver' => 'monolog', 'handler' => MysqlHandler::class, 'level' => 'debug', ], ];
应用程序集成(Laravel >= 5.6)
在你的应用程序 config/logging.php
中添加
<?php // [...] 'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['mysql'], ], // [...] 'mysql' => [ 'driver' => 'custom', 'via' => App\Logging\CreateMySQLLogger::class, ], ],
在你的应用程序 app/Logging/CreateMySQLLogger.php
中添加
<?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; } }
环境配置
如果你希望更改默认的表名或数据库连接,用于写入日志,请在 .env 文件中使用以下定义
DB_LOG_TABLE=logs DB_LOG_CONNECTION=mysql
鸣谢
基于
- [Pedro Fornaza] (https://github.com/pedrofornaza/monolog-mysql)