简体   繁体   中英

log4j doesn't see log4j.properties file

I'm trying to configure logging in my app where log4j.properties is put to /resources folder an has the following content:

#log level
log4j.rootLogger=ERROR, file
log4j 
# 
log4j.appender.file=org.apache.log4j.RollingFileAppender
#
log4j.appender.file.File=D:\agent\temp\panel log.log
#
log4j.appender.file.MaxFileSize=1MB
#
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n    

and on my app start I see the following in the console:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

what does this mean and how to fix it?

Try putting the log4j.properties in the root of your source folder. Or alternatively add resources to your classpath.

The root problem is log4j not seeing your config file, so you need to make it accessible.

Extract from log4j manual:

Default Initialization under Tomcat

The default log4j initialization is particularly useful in web-server environments. Under Tomcat 3.x and 4.x, you should place the log4j.properties under the WEB-INF/classes directory of your web-applications. Log4j will find the properties file and initialize itself. This is easy to do and it works.

You can also choose to set the system property log4j.configuration before starting Tomcat. For Tomcat 3.x The TOMCAT_OPTS environment variable is used to set command line options. For Tomcat 4.0, set the CATALINA_OPTS environment variable instead of TOMCAT_OPTS.

I had the same problem, I was using tomcat 7. I solved the problem by adding conf path to the /bin/setenv.sh (if you are on linux, create the file if it does not exist) like this:

export JAVA_OPTS="$JAVA_OPTS -Dconfig.location=file -Dlog4j.configuration=file:///opt/apache-tomcat-7.0.61/lib/log4j.properties"

if you are on windows edit or create setenv.bat and write something like this (not sure about the path format):

set JAVA_OPTS=%JAVA_OPTS%-Dconfig.location=file -Dlog4j.configuration=file://c:/opt/apache-tomcat-7.0.61/lib/log4j.properties

Forgot to say: this will affect all the apps on the server

i've changed log4j.properties to log4j.xml and put it to resources folder and now everything is correct. maybe my log4j version doesn't supprot .properties format

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