473,545 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Log4j in Servlets

3 New Member
How to use Log4j to create log files using file appender in a Servlet? Also, how to make that Servlet read the log4j.propertie s 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.propertie s 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
Feb 13 '08 #1
1 5509
Dököll
2,364 Recognized Expert Top Contributor
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:-)
Dec 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
180315
by: Aaron Boxer | last post by:
Hello, My application is using a jar file MyJar.jar that uses log4j (I have the latest release of log4j). I have put both MyJar.jar and log4j.jar in a directory in my classpath. I have also put the file "log4j.properties" into this same directory. My log4j.properties file reads:
1
5965
by: Adi | last post by:
Hi, I have a web application (using J2EE) which runs on 3 tomcats (4.0) for Load Balancing. I have recently started to use the Log4j in this application. Currently the logs for this application is of the tomcat's(System.out.println...) - and because there are 3 tomcats there are 3 logs. The log4j writes to one file only. My question is:...
1
5593
by: Ragavendra BC | last post by:
Log4j Issue ----------- Hi, I am facing a problem related to Log4j... The problem is such that I have two different Log4j files one in ..properties format and the other in the .xml format.. and their source code is like this
4
7415
rsrinivasan
by: rsrinivasan | last post by:
Hi, I am using log4j to create log message. I want to write the log message to database. So i configured the log4j.properties files like this... # Database Configuration log4j.appender.dbase =org.apache.log4j.jdbc.JDBCAppender log4j.appender.dbase.layout =org.apache.log4j.PatternLayout log4j.appender.dbase.driver ...
2
1987
by: Hari2349 | last post by:
Regarding to log4j in java program..Plz help me... -------------------------------------------------------------------------------- Hai, friends i used the log4j.properties file like as below...... log4j.rootLogger=VERBOSE, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppe nder
2
1272
by: Kedar Kachare | last post by:
How to use Log4j in a Servlet using FileAppender? How to make the servlet read the 'log4j.properties' file? Thanks in advance, Kedar
0
5706
by: Mikhail Teterin | last post by:
Hello! I'm trying to use log4j's SMTPAppender to get warnings and errors reported by our application via e-mail (in addition to having ALL messages saved into a file via RollingFileAppender). It all works, except for one thing -- every warning/error arrives in its own e-mail message... The BufferSize parameters appears intended to...
0
1385
by: msg2ajay | last post by:
hello, I have configured %CATALINA_HOME% in Win Environ.But as I configured to "${catalina.home}/MQLog/info/info.log" for "file" in log4j.xml I can't see the log file after running my application. i have place INFO in my code, I can see INFO log in Tomcat console, my log4j.xml file is as below. --------------------------- ...
5
4101
by: Abhinay | last post by:
Hi there, I am working on server which used thread pool to process each client request by separate dedicated thread. My intention was each thread ( of thread pool ) have its own logger and hence all client processed by that thread must be logged in corresponding thread specific logger. My problem is that logger merging log content with...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7771
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.