gupalo/monolog-dbal-logger

DBAL 的 Monolog 处理器

1.5.0 2024-07-26 18:50 UTC

This package is auto-updated.

Last update: 2024-09-26 19:14:47 UTC


README

安装

通过 composer 将其添加到您的项目中

composer require gupalo/monolog-dbal-logger

创建日志表。

CREATE TABLE `_log` (
    `id`                    bigint UNSIGNED NOT NULL AUTO_INCREMENT,
    `created_at`            datetime NOT NULL,
    `level`                 smallint NOT NULL DEFAULT 0,
    `level_name`            enum('debug','info','notice','warning','error','critical','alert','emergency') NULL DEFAULT NULL,
    `channel`               varchar(255) NOT NULL DEFAULT '',
    `message`               varchar(1024) NOT NULL DEFAULT '',
    `context`               text NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX `log_created_at_level` (`created_at`, `level`) USING BTREE
) DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_general_ci;

示例

$connectionDoctrine\DBAL\Connection

$handler = new \Gupalo\MonologDbalLogger\MonologDbalLogger($connection);
$logger = new \Monolog\Logger();
$logger->pushHandler($handler);

$logger->addWarning('You might not read the docs', ['page' => 17, 'username' => 'guest']);

Symfony

配置示例

services.yaml

Gupalo\MonologDbalLogger\Symfony\ErrorLogListener:
    tags: [ {name: 'kernel.event_subscriber'} ]

monolog.dbal_handler:
    class: 'Gupalo\MonologDbalLogger\MonologDbalLogger'
    public: true
    bind:
        $connection: '@doctrine.dbal.default_connection'
        $level: 200
    tags: ['monolog.logger']

monolog.yaml

monolog:
    handlers:
        db:
            type: service
            id: 'monolog.dbal_handler' # note - no "@"
            channels: ["!event", "!request", "!security"]

配置

MonologDbalLogger::__construct 中的参数

  • string $table = '_log': 表名
  • int $maxRows = 100000: 如果行数超过 $maxRows,则清理器最终(千分之一的机会)将删除它们

扩展

您可以通过扩展 MonologDbalLogger 并添加自己的逻辑来扩展它。以 MyMonologDbalLogger 为例。如果您添加了额外的字段,别忘了更改创建表的 SQL 语句。

CREATE TABLE `_log` (
    `id`                    bigint UNSIGNED NOT NULL AUTO_INCREMENT,
    `created_at`            datetime NOT NULL,
    `level`                 smallint NOT NULL DEFAULT 0,
    `level_name`            enum('debug','info','notice','warning','error','critical','alert','emergency') NULL DEFAULT NULL,
    `channel`               varchar(255) NOT NULL DEFAULT '',
    `message`               varchar(1024) NOT NULL DEFAULT '',
    `context`               text NULL DEFAULT NULL,
    `method`                varchar(255) NULL DEFAULT NULL,
    `cmd`                   varchar(255) NULL DEFAULT NULL,
    `uid`                   varchar(32) NULL DEFAULT NULL,
    `count`                 int NULL DEFAULT NULL,
    `time`                  float NULL DEFAULT NULL,
    `exception_class`       varchar(1024) NULL DEFAULT NULL,
    `exception_message`     varchar(1024) NULL DEFAULT NULL,
    `exception_line`        varchar(1024) NULL DEFAULT NULL,
    `exception_trace`       text NULL,
    PRIMARY KEY (`id`),
    INDEX `log_created_at_level` (`created_at`, `level`) USING BTREE
) DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_general_ci;

其他

参见 tests