meetfree/laravel-mysql-log

Monolog 的 Laravel 8 MySQL 驱动程序

dev-master 2022-06-14 07:51 UTC

This package is auto-updated.

Last update: 2024-09-14 12:52:00 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

鸣谢

基于