如何在log4j.properties中使用Marker来过滤一些日志?

时间:2017-09-05 06:48:37

标签: java logging

我在我的程序中使用它,但是如何在log4j.properties中过滤此日志。

Marker marker = MarkerFactory.getMarker("tp-count");
log.info(marker, "The num of tp is {}", 10);

log4j.properties是这样的:

log4j.rootLogger=info,F
log4j.appender.F=org.apache.log4j.RollingFileAppender
log4j.appender.F.File=client.log
log4j.appender.F.MaxBackupIndex=10
log4j.appender.F.MaxFileSize=100MB
log4j.appender.F.layout=org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern=[%d] %p %m (%c)%n

2 个答案:

答案 0 :(得分:1)

假设您使用的是log4j 2.0,您可以按manual

中所述使用MarkerFilter过滤日志

另请注意,您需要从properties配置切换到xml配置,以便启用对过滤器的支持<澄清here。您无法使用properties方法实现过滤!

您可以使用此online tool将属性快速转换为xml。请参阅手册的this section以确保您的log4j2.xml已正确放置并命名,以便您的应用程序自动选择它

您的最终log4j2.xml可能如下例所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="F" class="org.apache.log4j.RollingFileAppender">

        <!-- below forces inclusion of only log events with marker 'tp-count' to this client.log-->
        <MarkerFilter marker="tp-count" onMatch="ACCEPT" onMismatch="DENY"/>

        <param name="File" value="client.log"/>
        <param name="MaxBackupIndex" value="10"/>
        <param name="MaxFileSize" value="100MB"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d] %p %m (%c)%n"/>
        </layout>
    </appender>
    <root>
        <level value="INFO"/>
        <appender-ref ref="F"/>
    </root>
</log4j:configuration>

答案 1 :(得分:1)

问题是关于将Marker与通过propertie文件配置结合使用。我将回答log4j2版本2.11。举个例子。

示例项目文件:

<强> log4j2.properties

status = error


appender.ana_whitespace.type = RollingFile
appender.ana_whitespace.name = ana_whitespace
appender.ana_whitespace.fileName = ${sys:es.logs.base_path:-target}${sys:file.separator}ana_whitespace.log
appender.ana_whitespace.filter.1.type = MarkerFilter
appender.ana_whitespace.filter.1.onMismatch=DENY
appender.ana_whitespace.filter.1.onMatch=ACCEPT
appender.ana_whitespace.filter.1.marker=ANA_WHITESPACE
appender.ana_whitespace.policies.type = Policies
appender.ana_whitespace.policies.time.type = TimeBasedTriggeringPolicy
appender.ana_whitespace.policies.time.interval = 1
appender.ana_whitespace.policies.time.modulate = true

rootLogger.level = info
rootLogger.appenderRef.ana_whitespace.ref = ana_whitespace

示例Java代码

package de.es.stemmer;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.client.ClientProtocolException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.message.ObjectMessage;

public class JsonLoggerTest {
    final static Logger log = LogManager.getLogger(JsonLoggerTest.class);
    final static Marker MARKER_WHITESPACE = MarkerManager.getMarker("ANA_WHITESPACE");

    public static void main(String[] args) throws ClientProtocolException, IOException {
        System.setProperty("es.logs.base_path", "target");
        LoggerContext.getContext().reconfigure();
        log.info(MARKER_WHITESPACE, "msg_sourceSnippet whitespace");
    }

}