jQuery in Action, 2nd edition*
The moose likes Other Open Source Projects and the fly likes Log4J configuration problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Log4J configuration problem" Watch "Log4J configuration problem" New topic
Author

Log4J configuration problem

Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
Hello,

I am trying to use Log4J logging in a web application, deployed on BEA.
I am writing this down from memory since I can not access my code right now. My apologies.

First, in a Java class (e.g. mypackage.MyClass), I put code like:

Second, I have created a log4j.properties file and I have put it in the web project's WEB-INF/classes folder (later on, I tried putting it in other classpath locations as well).
The properties file basically just defines the root logger, and configures it to a console output appender, at INFO level. I copied the property file content directly from an example, so I assume nothing is wrong with it (later on I tried to create a specific logger for "mypackage" as well).

When starting up the server and application, and passing through the code of MyClass, the following appears in the console output:

log4j:WARN No appenders could be found for logger (mypackage.MyClass)
log4j:WARN Please initialize the log4j system properly.

What do I need to do to get it working? It is a web application, so I assume I do not need to call some initialization method in advance, like I noticed in stand-alone Java application examples.

Thanks!
Kjeld

Kjeld Sigtermans - SCJP 1.4 - SCWCD 1.4
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And does this logging configuration work (i.e. produce log output) when you use it in a simpler environment? E.g. not in a Java EE container, but in a plain old Java application?
Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
I have not tried that, thanks. I will in the morning and post the result here.
Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
Well the log4j.properties file works fine in a standalone Java application.
This is the file:
Note that my standalone Java app is in a package called log4jtest.

I put the log4j.properties file in the classpath and before doing any logging I initialize log4j using:

I understand that PropertyConfigurator.configure(...) does the trick, but I doubt this is the way to initialize Log4J in a web application?
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,
I think the problem may be that the web application does not have a console as such.
Have you tried using the file appender in the web application?

Sean


I love this place!
Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
I tried, but nog log file.
Right now I am creating a SetupServlet , loaded on startup, containing the initialization code.
Can't image this is the way to go, though.

Thanks!
Tanzy Akhtar
Ranch Hand

Joined: Jul 19, 2009
Posts: 110
Hi Kjeld, i am curious whether have you resolved this problem.
Please share with us if you find the solution so that it ll be helpful to others.


Thanks,
Tanzy.


Roll with punchers, there is always tomorrow.
Techie Blog -- http://jtanzy.blogspot.com/
Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
Hello Tanzy,

No change since my last post. The development team agreed in creating a Servlet called InitServlet that is loaded on startup, initializing the code in its 'init(ServletConfig)' method. Here's the method:



And the web.xml snipplet:



Grtz,
Kjeld
Tanzy Akhtar
Ranch Hand

Joined: Jul 19, 2009
Posts: 110
Thanks Kjeld for sharing the solution.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Log4J configuration problem
 
Similar Threads
Error using log4j in webapp
Help! I am getting strange and unusual Eclipse Warnings
Subtle, subtle, little Log4j difficulty
log4j configuration
Log4j not complaining but not printing anything too