简体   繁体   中英

Log4j.properties does not producing logs with timestamp for LOGGER.info(), LOGGER.error() statements

I have recently added Log4j in my project and all the logs are printing as expected.

Now I have added the timestamp in the log4j.properties file and server-generated logs are printing with the timestamp but which statements I am printing from Logger.info() and Logger.error() it is not printing with the timestamp. This is how my log4j.properties look like:

logFileLoc = ${catalina.base}/logs/

log4j.rootLogger=INFO, stdout, fileAppender

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{yyyy-MM-dd HH:mm:ss}: [%-5p] %c{1}:%L - %m%n

# Redirect Log Messages To A Debug Log File, Support File Rolling !!
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.File=${logFileLoc}/catalina.out
log4j.appender.fileAppender.MaxFileSize=50MB
log4j.appender.fileAppender.MaxBackupIndex=10
log4j.appender.fileAppender.Append=true
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}: [%-5p] %c{1}:%L - %m%n

Whenever I try something like this in my code:

LOGGER.debug("creating HomePage");

This is what I see in my log file:

creating HomePage

Can anyone please suggest how can I print the timestamp in LOGGER statements? It will be highly appreciable.

I checked your configuration with log4j 1.2.17 and everything works correctly.

I think you have other log4j config file in classpath or you are using system variables(like log4j.defaultInitOverride or some others) which have impact on log4j configuration. I recommend debugging log4j initialization in static block of the LogManager class and then you will see from which place log4j is loading configuration.

if I'm wrong, please provide code for reproducing your issue.

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