简体   繁体   English

log4j2:日期/转换模式不起作用

[英]log4j2: date/conversion pattern does not work

Added log4j 2 to my project. 将log4j 2添加到我的项目中。 At first, I didn't add any configuration / properties file and got the message ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console 最初,我没有添加任何配置/属性文件,但收到消息ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console . ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console That was fine since I used the default settings anyway (and it worked). 没关系,因为我还是使用默认设置(并且有效)。

After setting it all up, I noticed that the log msgs timestamp does not contain date. 设置全部之后,我注意到日志msgs时间戳不包含日期。 For example: 10:46:24.597 [[STANDBY] ExecuteThread: '6' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO utils.ServerParamLoader - INFO LOG . 例如: 10:46:24.597 [[STANDBY] ExecuteThread: '6' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO utils.ServerParamLoader - INFO LOG So I started digging the web for answer. 因此,我开始在网上寻找答案。

With help from the log4jtester.com I made a small log4j2.properties file: log4jtester.com的帮助下,我制作了一个小的log4j2.properties文件:

log4j.rootLogger=INFO, STDOUT

#appenders
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.Target=System.out
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=[%d{dd/MM/yyyy HH:mm:ss,SSS}] [%-7p] %c{1}:%L (%t) - %m%n

And I put it under /src. 然后将其放在/ src下。

When I started the server again the error message was gone (that's why I'm sure that the file is being loaded!), but the messages in the log was still in the old format. 当我再次启动服务器时,错误消息消失了(这就是为什么我确定文件正在加载!),但是日志中的消息仍然是旧格式。

What am I doing wrong? 我究竟做错了什么?

(Another thing, maybe it's related maybe not - according to the API the default for timestamps is ISO8601, which includes the date!) (另一件事,也许是相关的,也许不是相关的-根据API ,时间戳的默认值为ISO8601,其中包括日期!)

The configuration file shown in the question is in the old Log4j 1 format. 问题中显示的配置文件为旧的Log4j 1格式。 It is named log4j2.properties, so Log4j2 will try to parse it, but I suspect an error occurred and you ended up with an invalid configuration... 它被命名为log4j2.properties,因此Log4j2将尝试解析它,但是我怀疑发生了错误,并且您最终获得了无效的配置...

Please see the Log4j2 manual page on configuration for an example of the properties format in Log4j2. 请参阅Log4j2手册页上的配置,以获取Log4j2中属性格式的示例。

Most of the examples in the manual use the xml format. 手册中的大多数示例都使用xml格式。 For that reason it may be easier to get started with an xml configuration file. 因此,开始使用xml配置文件可能会更容易。

The log4j2.xml configuration for a console appender is: 控制台附加程序log4j2.xml配置为:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
  <Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{dd/MM/yyyy HH:mm:ss,SSS}] [%-7p] %c{1}:%L (%t) - %m%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="STDOUT"/>
    </Root>
  </Loggers>
</Configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM