简体   繁体   中英

“log4j:WARN Please initialize the log4j system properly” error

I have a web application, in which one of the JSPs contain:

PropertyConfigurator.configure(System.getenv("MY_HOME") + "/cfg/log4j.properties");

I double-checked that MY_HOME is setup

The Tomcat web server says:

log4j:WARN No appenders could be found for logger (com.mycompany.data.JobData).
log4j:WARN Please initialize the log4j system properly.

The same setup works fine in one of the other servers.

Any hints?

The problem is that no logs are created

Not related to your answer, but this helps too.

If it's a web application, the easiest way is to make sure that your log4j.properties is situated in WEB-INF/classes folder in your WAR file. When your application will be deployed, log4J will be configured.

The PropertyConfigurator must be called before anything in your system attempts to log to avoid this warning. You may find setting the log4j configuration on the command line more reliable.

Or you could ignore the warning. ;)

BTW: You don't want to call this method too often, ideally only once.

You can try this, its helps to me. http://www.log4j.ru/articles/HelloWorld.html

I was able to find the solution to this problem running a Eclipse Dynamic Web Project in Apache Tomcat 6. Bascially, you need to load the log4j properties file out of your context.

Two basic steps

(1) Get the log4j.properties file into the "class directory" of the war file.

(2) Read the log4j properties file out of the current context. I found the best way to do this is to access the current thread's context and work from there.

For the first step above, alter the Eclipse build process to add an additional directory that will eventually load into the WEB-INF/classes directory in the war file. Specifically....

(1) In Eclipse, right click your project in the project explorer, select 'New'->'Folder'. You can name the folder anything, but the standard in this case is 'resources'. The new folder should appear at the root level of your project.

(2) Move the log4j.properties file into this new folder.

(3) Right click the project again, and select 'Build-Path'->'Configure Build Path'. Select the 'Sources' tab. Click the 'Add Folder' button. Browse to find your new folder you created in step (1) above. Select 'OK'.

(4) Once back to the eclipse Project Explorer view, note that the folder has now moved to the 'Java Resources' area (ie it's no longer at the root due to eclipse presentation abstraction).

(5) Clean build your project.

(6) To validate that the .properties file now exists in WEB-INF/classes in your war file, export a war file to an easy location (right click Project -> Export -> War file) and checkout the contents. Note that the log4j.properties file now appears in the WEB-INF/classes.

Now for the second step above, accessing the context to read the file. Add the following code where attempting to read the file. Note that this reads this out of the war file context, so this 'should' work as the war file moves from server to server.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
PropertyConfigurator.configure(classLoader.getResourceAsStream("log4j.properties") );

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