markhilton/monolog-mysql

Laravel 8 MySQL 驱动程序 for Monolog

0.1.6 2017-05-02 15:05 UTC

This package is not auto-updated.

Last update: 2024-09-27 22:41:06 UTC


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

致谢

基于