log4j2编写方法和类

时间:2017-10-26 10:09:41

标签: java logging log4j2 pattern-layout

我在java程序中使用log4j2 .. 这是初始化的代码行

private static final Logger logger = LogManager.getLogger("application-
log");

这是决定格式的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="30" status="info" strict="true">
<Properties>
    <Property name="logApplicativo">../logs/logApplicativo.log</Property>
    <Property name="logCdr">../logs/logCdr.log</Property>
</Properties>
<Appenders>
    <Console name="STDOUT">
            <PatternLayout pattern="%m MDC%X%n"/>
    </Console>
    <RollingRandomAccessFile   name="fileLogApplicativo" 
    fileName="${logApplicativo}" filePattern="${log-Applicativo}-%d{yyyy-MM-
    dd}-%i.log" immediateFlush="false" append="true" 
    ignoreExceptions="false" >
        <PatternLayout>
            <pattern>%d [%-6p] %C.%M(%F:%L) - %m%n</pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="5 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="30"/>
    </RollingRandomAccessFile >
    <RollingRandomAccessFile  name="fileLogCdr" fileName="${logCdr}" 
    filePattern="${log-Cdr}-%d{yyyy-MM-dd}-%i.log" >
        <PatternLayout>
            <pattern>%d %-5p %c{2} - %m%n</pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="5 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="30"/>
    </RollingRandomAccessFile>
</Appenders>
<Loggers>
    <AsyncRoot level="error">
            <AppenderRef ref="STDOUT"/>
    </AsyncRoot>
    <AsyncLogger name="application-log" level="debug" additivity="false">
        <AppenderRef ref="fileLogApplicativo" level="debug"/>>
    </AsyncLogger>
    <AsyncLogger name="cdr-log" level="debug" additivity="false">
        <appender-ref ref="fileLogCdr" level="debug"/>
    </AsyncLogger>
</Loggers>

我不明白为什么,但是类名,代码名,行代码都没有打印出来。可能问题是微不足道的,但我有一天会发疯。

谢谢大家

1 个答案:

答案 0 :(得分:0)

如果您正在使用异步记录器或异步appender,请尝试将includeLocation="true"添加到记录器或appender配置中。

您可以在log4j2 manual

中详细了解相关信息