Log4j2 – 日期在新格式中不起作用

huangapple 未分类评论51阅读模式
英文:

Log4j2 - Date is not working in new format

问题

最近我升级了log4j,这使得我在XML级别上进行了更改,我按照以下方式进行了更改。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Appenders>
        <!-- 用于控制台的 appender -->
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p [%t] %m%n%ex"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="fatal">
            <AppenderRef ref="console"/>
        </Root>
    </Loggers>
</Configuration>

但在更改为log4j2格式后,数据未按预期打印,例如 "2019-07-26 17:53:28,261",而显示为 "%d"。

%d DEBUG [main]  new  Test 
%d DEBUG [main]  new  Test1
%d DEBUG [main]  new  Test2

我是否遗漏了某些配置?

英文:

Recently I have upgraded the log4j which made me change in XML level
And I did as below.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;Configuration status=&quot;warn&quot;&gt;
    &lt;Appenders&gt;
        &lt;!-- An appender for the console --&gt;
        &lt;Console name=&quot;console&quot; target=&quot;SYSTEM_OUT&quot;&gt;
            &lt;PatternLayout pattern=&quot;%d %-5p [%t] %m%n%ex&quot;/&gt;
        &lt;/Console&gt;
    &lt;/Appenders&gt;
    &lt;Loggers&gt;
        &lt;Root level=&quot;fatal&quot;&gt;
            &lt;AppenderRef ref=&quot;console&quot;/&gt;
        &lt;/Root&gt;
    &lt;/Loggers&gt;
&lt;/Configuration&gt;

But after changing format as log4j2, the Data is not printing as expected like "2019-07-26 17:53:28,261" instead it displays as "%d".

%d DEBUG [main]  new  Test 
%d DEBUG [main]  new  Test1
%d DEBUG [main]  new  Test2

Am I missing any configuration?

答案1

得分: 0

发现了问题。实际问题是 Log4j2.xml(src/main/resources/log4j2.xml)中的更改未在生成的文件(target/classes/log4j2.xml)中得到体现,其中日期的模式是错误的 "%%d"。

英文:

Found the Issue. The actual problem is the changes in Log4j2.xml (src/main/resources/log4j2.xml) is not get reflected in the Generated file (target/classes/log4j2.xml) in which the Pattern of date is wrong "%%d".

huangapple
  • 本文由 发表于 2020年4月8日 22:42:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61103426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定