The moose likes Other Open Source Projects and the fly likes Log4j in a web application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Log4j in a web application" Watch "Log4j in a web application" New topic

Log4j in a web application

Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
Hi all. I have a problem with log4j in my webapp.
This is my config file:

log4j.rootCategory=ALL, A1
# Keep one backup file
log4j.appender.A1.layout.ConversionPattern=%p %t %c - %m%n

I just want to know how to config the web.xml to use this file, as I get this error:

log4j:WARN No appenders could be found for logger (org.apache.struts.util.PropertyMessageResources).
log4j:WARN Please initialize the log4j system properly.

Sorry, but I'm new with Log4j
din dee

Joined: Aug 11, 2004
Posts: 9
Log4j is used for Logging .,It is do Appender,Layout(design the o/p ) for logging information. using log4j.xml ., it has file tag u assign where u can store it.
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
Could you explain me how to do it?
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
something like this?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<!-- LOGS => Consola -->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %C{2} - %m%n"/>

<appender name="org.apache.struts.util.PropertyMessageResources" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="F:/ibt/ibt2.log"/>
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %C{2} - %m%n"/>

How do I modify web.xml now?
Pavan Panduga
Ranch Hand

Joined: Jun 12, 2002
Posts: 68

if you're using Tomcat, place your log4jconfig.xml file in WEB-INF/classes directory and log4j.jar in the lib directory. Taht will initialize Log4J.

If you want to use a special Servlet,
this might help.

you web.xml should have

if you have an xml config file, use DOMConfigurator instead of PropertyConfigurator.

Hope this helps,
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
Hi Paval!!
I haven't answered before because I just came from holidays. Thanks for your help!
The problem I have is that prefix = getServletContext().getRealPath("/"); returns null. If I set the prefix this way : prefix= "F:\ibt\logs\" it works perfectly
What am I doing wrong?
H Shah

Joined: Sep 22, 2004
Posts: 1
I'm trying the same thing as Miguel, but I'm running into a different problem. I want to use Log4J in my web app, but whenever I try to get a logger, JCL returns the Jdk14Logger instead.

Here's my setup:

<tomcat_dir>\common\lib does not contain commons-logging.jar or log4j.jar
<mywebapp>\WEB-INF\lib contains commons-logging.jar and log4j.jar

I have an initialization servlet that reads the log4j xml config file:

private void initializeLogging() {


Log jclLog = LogFactory.getLog(EnvironmentInitializer.class);
jclLog.debug("Debug statement");
jclLog.info("Info statement");
jclLog.error("Error statement");
jclLog.fatal("Fatal statement");

and here's my output:

Sep 22, 2004 1:11:54 PM log4jconfig.EnvironmentInitializer initializeLogging
INFO: Info statement
Sep 22, 2004 1:11:54 PM log4jconfig.EnvironmentInitializer initializeLogging
SEVERE: Error statement
Sep 22, 2004 1:11:54 PM log4jconfig.EnvironmentInitializer initializeLogging
SEVERE: Fatal statement

Any help would be greatly appreciated. Thanks!!

I agree. Here's the link: http://aspose.com/file-tools
subject: Log4j in a web application
Similar Threads
servlet loggin not working (log4j)
log4j / eclipse / linux
how to consume a webservice using axis2
Log4j - Compress Log Files