You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony 6.2配置Monolog自定义brp通道实现指定等级日志入库

Symfony 6.2 自定义brp Monolog通道配置方案

你最初的配置错误原因:monolog.channels节点仅用于声明通道名称,所有处理器(handler)的配置(包括存储方式、日志等级、过滤规则)都需要写在monolog.handlers节点下,不能直接嵌套在channels配置内。

以下是可直接落地的配置步骤,完全匹配两个核心需求:

  • brp通道日志写入指定数据库表
  • 仅捕获brp通道下info、warning、error三个等级的日志

1. 保留正确的基础通道声明

你当前简化版的通道声明配置是正确的,无需改动,仅用来注册brp通道名即可:

# config/packages/monolog.yaml
monolog:
    channels:
        - deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists
        - brp

2. 配置处理器规则

在同配置文件的handlers节点下,先给默认文件处理器加排除规则避免brp日志重复写入文件,再新增brp专属的数据库写入处理器,同时配置等级过滤规则:

monolog:
    channels:
        - deprecation
        - brp
    handlers:
        # 默认文件处理器,排除brp通道
        main:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
            channels: ["!event","!doctrine","!console","!brp"]
        
        # brp通道专属数据库处理器
        brp_database:
            type: doctrine
            connection: default # 替换为你项目使用的数据库连接名,默认填default即可
            table: # 这里替换为你指定的数据库表名,例如:`biz_brp_log`
            level: info # 最低记录等级设为info,自动过滤debug、notice等级日志
            max_level: error # 最高记录等级设为error,自动过滤critical、alert、emergency等级日志
            channels: ["brp"] # 仅处理brp通道的日志

Monolog日志等级从低到高排序为:debug < info < notice < warning < error < critical < alert < emergency,配置level: info+max_level: error后,会自动保留info、warning、error三个等级的日志,其余等级全部过滤。

3. 提前创建数据库表

在执行配置前,需要先在数据库中创建你指定的日志表,基础表结构参考如下(可根据自身业务扩展字段):

CREATE TABLE `biz_brp_log` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `channel` varchar(64) NOT NULL DEFAULT '' COMMENT '日志通道',
  `level` smallint NOT NULL COMMENT '日志等级值',
  `level_name` varchar(16) NOT NULL DEFAULT '' COMMENT '日志等级名',
  `message` text NOT NULL COMMENT '日志内容',
  `context` json DEFAULT NULL COMMENT '日志上下文',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='brp通道日志表';

4. 业务端调用方式

配置完成后,在业务代码中通过变量名匹配的方式注入brp通道的日志实例即可直接使用:

<?php

namespace App\Controller;

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class DemoController extends AbstractController
{
    // 参数名写为 $brpLogger 时,Symfony会自动注入brp通道对应的日志实例
    public function test(LoggerInterface $brpLogger)
    {
        // 以下三个等级的日志会被写入指定数据库表
        $brpLogger->info('业务操作提示', ['user_id' => 123, 'action' => 'create']);
        $brpLogger->warning('业务异常预警', ['order_id' => 456, 'msg' => '库存不足']);
        $brpLogger->error('业务执行错误', ['trace' => 'xxx', 'params' => []]);

        // debug、notice、critical等等级的日志会被自动过滤,不会写入
        // $brpLogger->debug('调试日志');
        // $brpLogger->critical('系统致命错误');
    }
}

可选:自定义字段映射

如果你的日志表字段和默认结构不一致,可以自定义日志格式化服务匹配字段,在config/services.yaml中添加如下配置即可:

services:
    # 自定义brp日志格式化器
    monolog.formatter.brp:
        class: Monolog\Formatter\LineFormatter
        arguments:
            - null
            - null
            - false
            - true

    # 覆写brp数据库处理器,绑定自定义格式化器
    monolog.handler.brp_database:
        class: Monolog\Handler\DoctrineHandler
        arguments:
            - '@doctrine.dbal.default_connection'
            - 'biz_brp_log' # 替换为你的表名
            - 200 # 对应INFO等级
            - false
        calls:
            - [setFormatter, ['@monolog.formatter.brp']]

内容的提问来源于stack exchange,提问作者Alex.Barylski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 11:48:28