log4j2:日期/转换模式不起作用 [英] log4j2: date/conversion pattern does not work

查看:75
本文介绍了log4j2:日期/转换模式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将log4j 2添加到我的项目中.最初,我没有添加任何配置/属性文件,但收到消息 ERROR StatusLogger找不到log4j2配置文件.使用默认配置:仅将错误记录到控制台.没关系,因为我还是使用默认设置(并且有效).

Added log4j 2 to my project. 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. That was fine since I used the default settings anyway (and it worked).

全部设置完后,我注意到日志msgs时间戳不包含日期.例如: 10:46:24.597 [[STANDBY] ExecuteThread:'6'for queue:'weblogic.kernel.Default(self-tuning)'] INFO utils.ServerParamLoader-INFO LOG .所以我开始在网上挖掘答案.

After setting it all up, I noticed that the log msgs timestamp does not contain date. For example: 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.

log4jtester.com 的帮助下,我制作了一个小的log4j2.properties文件:

With help from the log4jtester.com I made a small log4j2.properties file:

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

我把它放在/src下.

And I put it under /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.

我在做什么错了?

(另一件事,也许是相关的,也许不相关-根据

(Another thing, maybe it's related maybe not - according to the API the default for timestamps is ISO8601, which includes the date!)

推荐答案

问题中显示的配置文件采用旧的Log4j 1格式.它名为log4j2.properties,因此Log4j2将尝试解析它,但是我怀疑发生了错误,并且您最终获得了无效的配置...

The configuration file shown in the question is in the old Log4j 1 format. 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手册页上的配置Log4j2中的属性格式.

手册中的大多数示例都使用xml格式.因此,开始使用xml配置文件可能会更容易.

Most of the examples in the manual use the xml format. For that reason it may be easier to get started with an xml configuration file.

The log4j2.xml configuration for a console appender is:

<?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>

这篇关于log4j2:日期/转换模式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆