Connecting Tech Pros Worldwide Forums | Help | Site Map

Log4j in Servlets

Newbie
 
Join Date: Feb 2008
Posts: 3
#1: Feb 13 '08
How to use Log4j to create log files using file appender in a Servlet? Also, how to make that Servlet read the log4j.properties file?


I have tried following:

My web.xml( I use Eclipse europa)
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3.  
  4.         <display-name>ClientSkill_Log4j</display-name>
  5.  
  6.         <welcome-file-list>
  7.  
  8.           <welcome-file>index.html</welcome-file>
  9.  
  10.           <welcome-file>index.htm</welcome-file>
  11.  
  12.           <welcome-file>index.jsp</welcome-file>
  13.  
  14.           <welcome-file>default.html</welcome-file>
  15.  
  16.           <welcome-file>default.htm</welcome-file>
  17.  
  18.           <welcome-file>default.jsp</welcome-file>
  19.  
  20.         </welcome-file-list>
  21.  
  22.  
  23.  
  24.         <servlet>
  25.  
  26.           <description>Description</description>
  27.  
  28.           <display-name>Display name</display-name>
  29.  
  30.  
  31.           <servlet-name>ClientSkillServlet</servlet-name>
  32.  
  33.           <servlet-class>HTML_Generator</servlet-class>
  34.  
  35.  
  36.  
  37.           <init-param>
  38.  
  39.               <param-name>log4j.properties</param-name>
  40.  
  41.               <param-value>WEB-INF/log4j.properties</param-value>
  42.  
  43.           </init-param>
  44.  
  45.         </servlet>
  46.  
  47.  
  48.  
  49.         <servlet-mapping>
  50.  
  51.           <servlet-name>ClientSkillServlet</servlet-name>
  52.  
  53.           <url-pattern>/</url-pattern>
  54.  
  55.         </servlet-mapping>
  56. </web-app>
  57.  


My log4j.properties file:-
Expand|Select|Wrap|Line Numbers
  1.  
  2.       # Configures Log4j as the Tomcat system logger
  3.       #
  4.       #
  5.       # Configure the logger to output info level messages into a log file.
  6.       #
  7.       log4j.rootLogger=INFO, R
  8.       #
  9.       # To continue using the "catalina.out" file (which grows forever),
  10.       # comment out the above line and uncomment the next.
  11.       #
  12.       log4j.rootLogger=ERROR, A1
  13.       #
  14.       # Configuration for standard output ("catalina.out").
  15.       #
  16.       log4j.appender.A1=org.apache.log4j.ConsoleAppender 
  17.       log4j.appender.A1.layout=org.apache.log4j.PatternL  ayout
  18.       #
  19.       # Print the date in ISO 8601 format
  20.       #
  21.       log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
  22.       #
  23.       # Configuration for a rolling log file ("tomcat.log").
  24.       #
  25.       log4j.appender.R=org.apache.log4j.FileAppender
  26.       # Edit the next line to point to your logs directory.
  27.       # The last part of the name is the log file name.
  28.       #
  29.       log4j.appender.R.File=D:\DATA\tomcat.log
  30.       log4j.appender.R.layout=org.apache.log4j.PatternLa  yout
  31.       #
  32.       # Print the date in ISO 8601 format
  33.       #
  34.       log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
  35.       # Application logging options
  36.       #
  37.       #log4j.logger.org.apache=DEBUG
  38.       #log4j.logger.org.apache=INFO
  39.       #log4j.logger.org.apache.struts=DEBUG
  40.       #log4j.logger.org.apache.struts=INFO
  41.  

and My servlet code is:
Expand|Select|Wrap|Line Numbers
  1.       import java.sql.*;
  2.       import java.io.*;
  3.       import javax.servlet.*;
  4.       import javax.servlet.http.*;
  5.       import org.apache.log4j.*;
  6.  
  7.       public class HTML_Generator extends HttpServlet
  8.  
  9.       {
  10.           private static Logger logger =Logger.getLogger(HTML_Generator.class);
  11.           public void init() throws ServletException
  12.  
  13.           {
  14.  
  15.               // Put your code here
  16.  
  17.               String prefix = getServletContext().getRealPath("/");
  18.  
  19.               String file = getInitParameter("log4j.properties");
  20.  
  21.               if(file != null)
  22.  
  23.               {
  24.  
  25.                   PropertyConfigurator.configure(prefix+file);
  26.               }
  27.  
  28.           }
  29.  
  30.           public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException
  31.  
  32.           {
  33.  
  34.                   PrintWriter pw = resp.getWriter();
  35.  
  36.                   Connection conn = null;
  37.  
  38.                   ResultSet rs = null;
  39.  
  40.                   Statement stmt = null;
  41.  
  42.                   pw.println("Check your web server console...");
  43.  
  44.                   System.out.println("Insde doPost()");
  45.  
  46.                   pw.flush();
  47.  
  48.                   pw.close();
  49.  
  50.                   try
  51.  
  52.                   {
  53.  
  54.                       Class.forName("<driver name>");
  55.  
  56.                       conn = DriverManager.getConnection("<url>","<username>","<password>");
  57.  
  58.                       stmt = conn.createStatement();
  59.  
  60.                       rs = stmt.executeQuery("<my query>" );
  61.  
  62.                       int numColumns = rs.getMetaData().getColumnCount();
  63.  
  64.                       int i=0;
  65.  
  66.                       pw.println("<form name=\"Form2\" method=\"POST\" action=\"http://localhost/servlet/<target-servlet(running fine)>\">");
  67.  
  68.                       while (rs.next())
  69.  
  70.                       {
  71.  
  72.                           for (i = 1 ; i < numColumns ; i++ )
  73.  
  74.                               {
  75.  
  76.                                   <my action>
  77.  
  78.                               }
  79.  
  80.                       }
  81.  
  82.  
  83.  
  84.                   }
  85.  
  86.                   catch(SQLException sqle)
  87.  
  88.                   {
  89.  
  90.                       sqle.printStackTrace();
  91.  
  92.                   }
  93.  
  94.                   catch(Exception exc)
  95.  
  96.                   {
  97.  
  98.                       exc.printStackTrace();
  99.  
  100.                   }
  101.  
  102.                   try
  103.  
  104.                   {
  105.  
  106.                       rs.close();
  107.  
  108.                       stmt.close();
  109.  
  110.                       conn.close();
  111.  
  112.                   }catch(SQLException sqle){
  113.  
  114.                       sqle.printStackTrace();
  115.  
  116.                   }catch(Exception exc){
  117.  
  118.                       exc.printStackTrace();
  119.  
  120.                   }
  121.  
  122.           }
  123.  
  124.       }
  125.  

Thanks in advance,
Kedar

Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#2: Dec 8 '08

re: Log4j in Servlets


Greetings!

I fetched below and thought it could be of use:

Using the Log4J

http://www.roseindia.net/tutorials/l...er-log4j.shtml

Let us know if it worked:-)
Reply