| Author |
using log4j in servlets
|
deepthi Ragunathan
Greenhorn
Joined: Nov 12, 2005
Posts: 6
|
|
hello I am new to Log4j and i am having a problem with using log4j in servlets. Below i am giving the code: _____________________________________________________ SetupServlet.java import java.io.File; import javax.servlet.*; import javax.servlet.http.*; import org.apache.log4j.*; public class SetupServlet extends HttpServlet { public void init(ServletConfig config)throws ServletException { super.init(config); String props=config.getInitParameter("props"); if(props==null||props.length()==0||!(new File(props).isFile())) { System.err.println("ERROR: cannot read the configuration file"+ "Please check the path of the config init parameterin web.xml"); throw new ServletException(); } // look up another init parameter that tells whether to watch this // configuration file for changes. String watch=config.getInitParameter("watch"); // use the props file to load up configuration parameters for log4j if(watch!=null && watch.equalsIgnoreCase("true")) { PropertyConfigurator.configureAndWatch(props); } else { PropertyConfigurator.configure(props); } } public void destroy() { super.destroy(); } } ______________________________________________________ log4j.properties log4j.logger.foo=ERROR, A0 log4j.logger.deleted_log=INFO, A1 log4j.appender.A0=org.apache.log4j.FileAppender log4j.appender.A0.File=D:\Program Files\Apache Software Foundation\Tomcat 5.0\logs\foo.log log4j.appender.A1=org.apache.log4j.FileAppender log4j.appender.A1.File=D:\Program Files\Apache Software Foundation\Tomcat 5.0\logs\deleted_log.log # Define appenders for your logging. These will show which messages to log. log4j.appender.A0.layout=org.apache.log4j.SimpleLayout log4j.appender.A1.layout=org.apache.log4j.SimpleLayout __________________________________________________________________ web.xml <servlet> <servlet-name>setup</servlet-name> <servlet-class>SetupServlet</servlet-class> <init-param> <param-name>props</param-name> <param-value>D:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\logforj\WEB-INF\log4j.properties</param-value> </init-param> <init-param> <param-name>watch</param-name> <param-value>true</param-value> </init-param> <load-on-startup/> </servlet> <servlet> <servlet-name>configure</servlet-name> <servlet-class>LogServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>configure</servlet-name> <url-pattern>/ls</url-pattern> </servlet-mapping> _______________________________________________________________ i have copied my log4j-1.2.8.jar file in F:\logforj\WEB-INF\lib\log4j-1.2.8.jar I am using tomcat container. I am not understanding how to proceed further with the example.....ie i would like to write another servlet which writes the messages to the log files specified in the log4j.properties file. i have tried writing the servlet as shown below ___________________________________________ LogServlet.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.log4j.*; public class LogServlet extends HttpServlet { public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException { Logger foo_log = Logger.getLogger("foo"); foo_log.info("Inside whatever method"); } } _____________________________ I am lost........I think the code of LogServlet.java is wrong.please can anyone guide me thru the coding that has to be written in the second servlet ie.LogServlet.java............
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16480
|
|
I put the log4j.properties file in my application's WEB-INF/lib/classes directory instead of in its WEB-INF/lib directory. And you might want to reconsider the idea of using filenames which contain spaces, and you might want to use forward slashes instead of backward slashes in the filenames. That's all hypothetical, though, you didn't say what your problem was.
|
 |
deepthi Ragunathan
Greenhorn
Joined: Nov 12, 2005
Posts: 6
|
|
Originally posted by Paul Clapham: I put the log4j.properties file in my application's WEB-INF/lib/classes directory instead of in its WEB-INF/lib directory. And you might want to reconsider the idea of using filenames which contain spaces, and you might want to use forward slashes instead of backward slashes in the filenames. That's all hypothetical, though, you didn't say what your problem was.
hi Thanks paul.....i have made the required changes specified by you...... but my requirement is........i have written a servlet(SetupServlet.java)which initializes log4j at the startup of the web container......Now i would like to write a second servlet from which i can start writting the log messages into the log files....but i have no clue as how to start writing the code for this second servlet........don't know how to proceed further..... any help will be appreciated and thanks in advance
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Assuming that log4j is configured, logging with it is very easy.
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
deepthi Ragunathan
Greenhorn
Joined: Nov 12, 2005
Posts: 6
|
|
thanks for that piece of code ben......it has worked fine......but with a slight modification protected static Logger log = Logger.getLogger("foo"); instead of protected static Logger log = Logger.getLogger(Hello.class.getName()); thanks once again
|
 |
Sandip Chaudhuri
Greenhorn
Joined: Dec 27, 2004
Posts: 26
|
|
yeah i think it will requre foo as you put log4j.logger.foo it all depends on the properties file. i think you mind find this age useful sure helped me. http://www.vipan.com/htdocs/log4jhelp.html
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Originally posted by deepthi Ragunathan: thanks for that piece of code ben......it has worked fine......but with a slight modification protected static Logger log = Logger.getLogger("foo"); instead of protected static Logger log = Logger.getLogger(Hello.class.getName()); thanks once again
The recommended practice is to use the same hierarchy in your logger names that you're using for your class/package structure. If you follow it, you'll be very glad you did later, Using {this class's name}.class.getName() will get you the fully qualified name, package and all. If you refactor by changing the package structure later, you won't need to update the logging name. If you change the name of the class later, the compiler will remind you if you forget to update the logger's name. Maybe it didn't work because you put "Hello" instead of you class's name?
|
 |
Priyanka reddy
Greenhorn
Joined: Nov 13, 2005
Posts: 5
|
|
hello Ben, If you don't mind could you please elaborate a bit more on the explanation you have given.I couldn't fully undersatand it.In the code line shown below can you please explain the "Hello.class.getName()" part of it.What exactly does it do. protected static Logger log = Logger.getLogger(Hello.class.getName()); thanks in advance
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
The javadocs for the Class object explain it better than I could (with exmples). http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getName() You may also want to go through some of the articles listed in the documentation page at log4j for an explanation of how their naming hierarchy for loggers works. http://logging.apache.org/log4j/docs/documentation.html
|
 |
 |
|
|
subject: using log4j in servlets
|
|
|