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

Logback:如何为FileAppender配置AUDIT Marker实现日志分流

Answer

Great question! You’re right that EventEvaluator implementations (like OnMarkerEvaluator) are primarily intended for appenders such as SMTPAppender that need conditional triggering logic. For FileAppender, the standard approach to filter logs by marker is to use Logback’s Filter mechanism instead.

Here’s the corrected configuration that will route only logs tagged with the AUDIT marker to your dedicated audit file:

<appender name="AUDIT_FILE" class="ch.qos.logback.core.FileAppender">
  <!-- Filter to only accept logs with the AUDIT marker -->
  <filter class="ch.qos.logback.classic.filter.MarkerFilter">
    <marker>AUDIT</marker>
    <onMatch>ACCEPT</onMatch> <!-- Keep events with the AUDIT marker -->
    <onMismatch>DENY</onMatch> <!-- Reject all other events -->
  </filter>
  
  <file>${USER_HOME}/audit.log</file>
  <encoder>
    <pattern>%msg%n</pattern>
  </encoder>
</appender>

How this works:

  • The MarkerFilter is a built-in Logback filter that checks if a log event contains the specified marker.
  • Setting onMatch="ACCEPT" ensures any log with the AUDIT marker is processed by this appender.
  • onMismatch="DENY" tells the appender to ignore all other log events, so only audit logs end up in audit.log.

Quick note on using the marker in code:

Make sure you’re correctly attaching the AUDIT marker to your log statements in your application code:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class AuditExample {
    private static final Logger logger = LoggerFactory.getLogger(AuditExample.class);
    private static final Marker AUDIT_MARKER = MarkerFactory.getMarker("AUDIT");

    public void performAuditableAction() {
        logger.info(AUDIT_MARKER, "User {} performed action X", "john_doe");
    }
}

内容的提问来源于stack exchange,提问作者morsor

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:43:20