Log4j记录器仅使用根附加器

时间:2020-05-16 12:06:48

标签: java log4j2

我的记录器仅在根记录器ServerAppenderV2上记录消息,日志文件生成正确,当我使用更改级别在根目录中调试时,我可以在根目录中定义的两个附加程序中看到日志,但在文件附加器FileAppender中仍然没有日志和PerfAppender。

对于以下代码:

import logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestLog {
  private static final Logger logger= Logger.getLogger(TestLog.class);

    public static void main(String [] args){


       logger.perf("hello from debug",null,null,null,null);
       logger.log(Traces.43E, "info", 12);
       logger.debug("hello from debug",null);
    }
}

这是我的Logger类和标记:

public class Logger {

    private static final String ID_TRACE = "IdTrace";
    private static final Marker MARKER_PERF = MarkerFactory.getMarker("perf");
    private static final Marker V2 = MarkerFactory.getMarker("V2");
    private Logger delegate;


    public void debug(String message, Object... objects) {
        delegate.debug(message, objects);
    }

    public void log(Traces trace, Object... params) {

            Marker marker=V2;
            MDC.clear();
            MDC.put(ID_TRACE, trace.getId());

            switch(trace.getLevel()) {
                case DEBUG:
                    this.delegate.debug(marker, trace.getMessage(), params);
                    break;
                case INFO:
                    this.delegate.info(marker, trace.getMessage(), params);
                    break;
                case WARNING:
                    this.delegate.warn(marker, trace.getMessage(), params);
                    break;
                default:
                    this.delegate.error(marker, trace.getMessage(), params);
            }
        }

    public void perf(String msg, Object... params) {
        this.delegate.info(MARKER_PERF, msg, params);
    }

    }

这是我的配置Log4j2.xml:

<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_PATTERN">
            %d{yyyy-MM-dd HH:mm:ss,SSS} %5p --- [%15.15t] %-40.40c{1.} : %m%n%ex
        </Property>
        <Property name="PERF_LOG_PATTERN">
            %d{yyyy-MM-dd HH:mm:ss,SSS};%5p;%t;%m;%n
        </Property>
    </Properties>

    <Appenders>
        <RollingFile name="ServerAppender" fileName="logs\\server.log"
                     filePattern="logs\\server-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <Pattern>${LOG_PATTERN}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
            </Policies>
        </RollingFile>
        <RollingFile name="ServerAppenderV2" fileName="logs\\serverV2.log"
                     filePattern="logs/serverV2-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <Pattern>${LOG_PATTERN}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
            </Policies>
        </RollingFile>
        <RollingFile name="FileAppender" fileName="logs\\appli.log"
                     filePattern="logs\\appli-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <Pattern>${LOG_PATTERN}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
            </Policies>
        </RollingFile>

        <RollingFile name="PerfAppender" fileName="logs\\perf.log"
                     filePattern="logs\\perf-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <Pattern>${PERF_LOG_PATTERN}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>
        <AsyncLogger name="logging" level="info" additivity="false">
            <AppenderRef ref="FileAppender">
                <MarkerFilter marker="perf" onMatch="DENY" onMismatch="ACCEPT"/>
            </AppenderRef>
                <AppenderRef ref="PerfAppender">
                <MarkerFilter marker="perf" onMatch="ACCEPT" onMismatch="DENY"/>
            </AppenderRef>
        </AsyncLogger>

        <Root level="info">
            <AppenderRef ref="ServerAppender" >
                <MarkerFilter marker="V2" onMatch="DENY" onMismatch="ACCEPT"/>
            </AppenderRef>
                    <AppenderRef ref="ServerAppenderV2">
                <MarkerFilter marker="V2" onMatch="ACCEPT" onMismatch="DENY"/>
            </AppenderRef>
        </Root>
    </Loggers>

</Configuration>

2 个答案:

答案 0 :(得分:1)

这是因为您将记录器name称为“记录”。

<AsyncLogger name="logging" level="info" additivity="false">
   // configurations
</AsyncLogger>

但是在访问时您正在使用它。

private static final Logger logger= Logger.getLogger(TestLog.class);

这将使用packageclassName解析记录器名称。

使用当前配置,您应该像这样访问logger

private static final Logger logger= Logger.getLogger("logging");

或者,您可以这样配置。 假设您的TestLog.class位于软件包org.random.service中,则您的配置应如下所示。

<AsyncLogger name="org.random.service.TestLog" level="info" additivity="false">
   // configurations
</AsyncLogger>

要启用完整的程序包,可以从记录器名称中删除className

<AsyncLogger name="org.random.service" level="info" additivity="false">
   // configurations
</AsyncLogger>

答案 1 :(得分:0)

感谢您的响应,我是Log4J的新手,是的记录器名称是log4j的过滤器,我应该已经使用LOG4J引用了这些类。