4

我正在使用具有以下依赖关系的 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

在滚动文件中,我得到以下模式,其中缺少行号::

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

我疯了,因为打印行号似乎没有任何效果,我也遵循了官方的 log4j2 链接 Log4j2 迁移 ,但结果仍然与上面相同。如果有人有任何解决方案,请告诉我。

4

3 回答 3

3

出于性能考虑,async logger 默认关闭了 includeLocation。如果要打印行号和类名,则需要添加<Async name="asyncFramework" includeLocation="true">

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

于 2018-08-14T00:34:38.900 回答
3

在这里我找到了解决方案 :: Log4j2 AsyncLogger with rolling file 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>
于 2015-08-21T03:25:26.950 回答
3

该文件描述了不打印行号的原因。它说您可以通过指定includeLocation="true". 但是我没有解决我这样做后不打印行号的问题。也许是因为我不明白它的意思,我发错地方了。

我的解决方案是添加%LLog4j2 PatternLayout 模式。我的 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

于 2019-05-29T03:37:06.063 回答