This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Log4j in a web application

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. I have a problem with log4j in my webapp.
This is my config file:

log4j.rootCategory=ALL, A1
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File=F:/ibt/logs/ibt.log
log4j.appender.A1.MaxFileSize=1024KB
# Keep one backup file
log4j.appender.A1.MaxBackupIndex=1
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
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
Regards
Miguel
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you explain me how to do it?
 
Miguel Flecha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"/>
</layout>
</appender>

<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"/>
</layout>
</appender>


How do I modify web.xml now?
Thanks
Miguel
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miguel,

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,
Regards,
 
Miguel Flecha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Regards
Miguel
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() {
BasicConfigurator.configure();

DOMConfigurator.configure(log4jConfigFilename);

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!!

-Hitesh
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!