RollingFile Appender Log4j2不打印行号

时间:2015-08-20 14:57:35

标签: logging log4j2 line-numbers rollingfileappender pattern-layout

我正在使用log4j2,其中包含以下依赖项::

   <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0-rc1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0-rc1</version>
    </dependency>

我正在使用以下配置::

    <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">

    <Properties>
        <Property name="LOGGER_HOME">/logs</Property>
    </Properties>

    <Appenders>

        <RollingFile name="application" fileName="${LOGGER_HOME}/application.log"
            filePattern="${LOGGER_HOME}/application.%d{yyyy-MM-dd}_%i.log">

            <PatternLayout pattern="%d{ISO8601}{GMT} %-5p %C{2} (%F:%L) - %m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="1 GB" />
            </Policies>

        </RollingFile>

        <RollingFile name="framework" fileName="${LOGGER_HOME}/em-logs/framework.log"
            filePattern="${LOGGER_HOME}/framework.%d{yyyy-MM-dd}_%i.log">

            <PatternLayout pattern="%d{ISO8601}{GMT} %-5p %C{2} (%F:%L) - %m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="1 GB" />
            </Policies>
        </RollingFile>

        <Console name="out" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{ISO8601}{GMT} %-5p %C{2} (%F:%L) - %m%n" />
        </Console>

        <Async name="asyncApplication">
            <AppenderRef ref="application" />
        </Async>

        <Async name="asyncFramework">
            <AppenderRef ref="framework" />
        </Async>


    </Appenders>


    <Loggers>

        <Logger name="com.memorynotfound.logging" level="debug"
            includeLocation="true">
            <AppenderRef ref="asyncApplication" />
        </Logger>

    <Root level="debug" includeLocation="true">
            <AppenderRef ref="asyncApplication"></AppenderRef>
        </Root>

        <Logger name="org.axonframework" level="info" additivity="false"
            includeLocation="true">
            <AppenderRef ref="asyncFramework" />
        </Logger>

        <Root level="error" includeLocation="true">
            <AppenderRef ref="out" />
        </Root>

    </Loggers>


</Configuration>

但是我按照以下格式在控制台上获取日志

2015-08-20 14:29:41,613 DEBUG logging.LoggerExample (LoggerExample.java:11) - This will be printed on debug

在Rolling文件中,我得到以下缺少行号的模式::

2015-08-20 14:29:41,613 DEBUG ? () - This will be printed on debug

我已经疯了,因为似乎没有什么工作可以打印行号,我也遵循官方的log4j2链接 Log4j2 Migration 但结果仍与上述相同。如果有任何解决方案,请告诉我。

3 个答案:

答案 0 :(得分:1)

在这里我找到了解决方案:: Log4j2 AsyncLogger with rolling file appender not showing file line number

然后我更改了我的appender引用,直接指向RollingFile名称而不是<Async name>,它现在正确显示行号。不知道为什么会发生这种情况,我会发现并很快发布原因。

所以更改了以下::

<Logger name="com.memorynotfound.logging" level="debug"
            includeLocation="true">
            <AppenderRef ref="asyncApplication" />
        </Logger>

<Logger name="com.memorynotfound.logging" level="debug"
        includeLocation="true">
        <AppenderRef ref="application" />
    </Logger>

答案 1 :(得分:1)

出于性能方面的考虑,异步记录器默认情况下关闭了includeLocation。如果要打印行号和类名,则需要添加<Async name="asyncFramework" includeLocation="true">

仅供参考 https://logging.apache.org/log4j/2.x/manual/layouts.html#LocationInformation

答案 2 :(得分:0)

文件描述了不打印行号的原因。 它表示您可以通过指定includeLocation="true"覆盖记录器或异步附加程序配置中的默认行为。 但是我没有解决我完成后不打印行号的问题。 也许是因为我不明白这意味着什么,所以我选错了地方。

我的解决方案是在Log4j2 PatternLayout模式中添加%L。 我的Log4j2.xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN" monitorInterval="30">
        <Properties>
            <Property name="LOG_HOME">./logs</Property>
        </Properties>

        <Console name="Console" target="SYSTEM_OUT">
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n" />
        </Console>

        <RollingFile name="RollingFileInfo" fileName="${LOG_HOME}/pinche_quartz.log"
                     filePattern="${LOG_HOME}/test.log-%d{yyyy-MM-dd}.log">
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>

        <RollingFile name="RollingFileError" fileName="${LOG_HOME}/error/pinche_quartz-error.log"
                     filePattern="${LOG_HOME}/error/pinche_quartz-error.log-%d{yyyy-MM-dd}.log">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>
        <root level="all">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileError"/>
        </root>
    </Loggers>

</Configuration>

希望对您有所帮助。

Log4j2文档https://logging.apache.org/log4j/2.x/manual/layouts.html#LocationInformation