简体   繁体   中英

log4j2: date/conversion pattern does not work

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

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

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.

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!)

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

Please see the Log4j2 manual page on configuration for an example of the properties format in Log4j2.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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