dcsimg
Configuration file in a Web Application
5 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Miguel_Flecha
Posted On:   Wednesday, June 23, 2004 02:24 AM

I am not able to make log4j configuration file work in a web application I put log4j.properties in WEB-INF/classes and called it this way: PropertyConfigurator.configure("WEB-INF/classes/log4j.properties"); I also tried PropertyConfigurator.configure("log4j.properties"); My log4j.properties file contains: log4j.rootCategory=ALL, A1 log4j.appender.A1=org.apache.log4j.FileAppender log4j.appender.A1.File=ibt.log log4j.appender.A1.MaxFileSize=100KB # 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    More>>

I am not able to make log4j configuration file work in a web application



I put log4j.properties in WEB-INF/classes and called it this way:


PropertyConfigurator.configure("WEB-INF/classes/log4j.properties");


I also tried


PropertyConfigurator.configure("log4j.properties");


My log4j.properties file contains:


log4j.rootCategory=ALL, A1 log4j.appender.A1=org.apache.log4j.FileAppender log4j.appender.A1.File=ibt.log log4j.appender.A1.MaxFileSize=100KB # 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 don't get any file with the logs. Which is the problem?



Thanks and regards


Miguel

   <<Less

Re: Configuration file in a Web Application

Posted By:   Ravindranath_Y  
Posted On:   Tuesday, July 13, 2004 07:59 AM

Try the following as it works for me. Put your log4j.properties file under WEB-INF/log4j/log4j.xml


Also place the log4j.dtd under the same directory.


In your web deployment descriptor have an initialization paramter called log4j-init to point to
/WEB-INF/log4j/log4j.xml


And in your servlet init() have the following code..


String prefix = getServletContext().getRealPath("/");


String file = getInitParameter("log4j-init-file");


System.out.println("Reading file" + prefix + file);


if (file != null) {


DOMConfigurator.configureAndWatch(prefix + file, 60000);


}


But if you are using .properties then all you would need to do is in the servlet code


String prefix = getServletContext().getRealPath("/");


String file = getInitParameter("log4j-init-file");


System.out.println("Reading file" + prefix + file);


if (file != null) {


PropertyConfigurator.configure(prefix+file);


}





Good luck!



Thanks
Ravi

Re: Configuration file in a Web Application

Posted By:   Ravindranath_Y  
Posted On:   Tuesday, July 13, 2004 07:59 AM

Try the following as it works for me. Put your log4j.properties file under WEB-INF/log4j/log4j.xml


Also place the log4j.dtd under the same directory.


In your web deployment descriptor have an initialization paramter called log4j-init to point to
/WEB-INF/log4j/log4j.xml


And in your servlet init() have the following code..


String prefix = getServletContext().getRealPath("/");


String file = getInitParameter("log4j-init-file");


System.out.println("Reading file" + prefix + file);


if (file != null) {


DOMConfigurator.configureAndWatch(prefix + file, 60000);


}


But if you are using .properties then all you would need to do is in the servlet code


String prefix = getServletContext().getRealPath("/");


String file = getInitParameter("log4j-init-file");


System.out.println("Reading file" + prefix + file);


if (file != null) {


PropertyConfigurator.configure(prefix+file);


}





Good luck!



Thanks
Ravi

Re: Configuration file in a Web Application

Posted By:   Anonymous  
Posted On:   Wednesday, July 7, 2004 10:56 AM

use getServletContext().getRealPath("WEB-INF/classes")
from your JSP/Servlet code to get a full path to the lof4j.properties file



This translates the relative path (application path)
to a path like 'C: omcatwebapps heappweb-infclasses'

Re: Configuration file in a Web Application

Posted By:   Morten_Bagai  
Posted On:   Wednesday, June 30, 2004 02:53 PM

I think the problem you are facing is related to class loaders and is explained in some detail on the log4J FAQ.


The problem is that log4J doesn't know that it is being called within a web application context, so the context relative paths you're supplying do not get resolved properly.


If you're looking for an easy fix, you can set parameters such as the name of the properties file and the context relative path to your log directory as context parameters, and resolve them with ServletContext.getRealPath. It's but it's kind of a kludge, which may lead to some problems with separation of logging for multiple applications depending on your setup. There's more about that on the log4J FAQ as well.


A better solution is probably to try to follow the instructions here: http://wiki.apache.org/logging-log4j/AppContainerLogging. I used the InitContextListener, and it seems to work quite well for my purposes.


HTH,


Morten

Re: Configuration file in a Web Application

Posted By:   M_J  
Posted On:   Tuesday, June 29, 2004 06:46 AM

Any progress on this? I'm having the same problem.

Thanks.
About | Sitemap | Contact