简体   繁体   中英

Log4j is not finding configuration in properties file

My log4j.properties file is:

# Log levels
log4j.rootLogger=INFO,CONSOLE,file
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.file.File=C:/Logging/log4jFile.log
log4j.appender.file.MaxFileSize=200KB
# Number of backup files
log4j.appender.file.MaxBackupIndex=2
# Layout for Rolling File Appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d - %c - %p - %m%n

and the location of log4j.properties file is in WEB-INF of my project. So when I run my servlet I get this below message:

log4j:WARN No appenders could be found for logger (...).

log4j:WARN Please initialize the log4j system properly.

Where I am going wrong, I don't understand!

I'm pretty sure by default Log4J searches in the classes directory. So try putting your log4j.properties there.

This is due to log4j searching the classpath for the log4j.properties and the WEB-INF Folder is not part of that but the classes directory is. So either put the file in classes directory, add the file to the classpath or manually load in your Servlet with:

Below is my log4j properties file which works for me. It needs to be in the src/main/resources source folder. I notice the top line is different between mine and yours so maybe this is incorrect - try changing rootLogger to rootCategory.

log4j.rootCategory=INFO, CONSOLE, FILE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=ERROR
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p %c- %m%n

log4j.logger.com.myCompany.log4jexample= TRACE, FILE, CONSOLE

log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=Logs.log
log4j.appender.FILE.Threshold=WARN
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d %-5p %c- %m%n

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