Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By lovelesh

Thread: Problem configuring Lg4j with servlet....

  1. #1
    shl_libra is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default Problem configuring Lg4j with servlet....

    hi frnz i'm fairly new to java servlets and log4j . I was making a servlet with log4j who will log servlet's log message to "File" .

    My servlet code :

    package com.example.web;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import org.apache.log4j.PropertyConfigurator;
    import org.apache.log4j.Logger;


    public class build extends HttpServlet
    {

    public void init() {

    String prefix = getServletContext().getRealPath("/");
    String file = getInitParameter("log4j-init-file");

    if(file != null) {
    PropertyConfigurator.configure(prefix+file);
    }
    }


    final static Logger log = Logger.getLogger(build.class);
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
    {

    PrintWriter out = res.getWriter();
    String a=req.getParameter("a");
    log.info("Started !!!!");
    out.println("Hi in servlet....);


    }
    }

    and my xml file code :

    <web-app>
    <servlet>
    <servlet-name>build</servlet-name>
    <servlet-class>com.example.web.build</servlet-class>

    <init-param>
    <param-name>log4j-init-file</param-name>
    <param-value>WEB-INF/classes/chapter/com/example/web/log4j.properties</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>


    </servlet>
    <servlet-mapping>
    <servlet-name>build</servlet-name>
    <url-pattern>/build</url-pattern>
    </servlet-mapping>


    </web-app>


    And log4j.properties file code :

    log4j.rootLogger=debug, R
    log4j.appender.R=org.apache.log4j.RollingFileAppen der
    log4j.appender.R.File=C:/example.log
    log4j.appender.R.MaxFileSize=100KB
    log4j.appender.R.layout=org.apache.log4j.PatternLa yout
    log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

    When i run my servlet , browser message is printed but I'm not able to get Log file. Plz help i'm nt able to find the error .....................................

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Quote Originally Posted by shl_libra View Post
    And log4j.properties file code :

    log4j.rootLogger=debug, R
    Your log level is set to be debug, where as you are using info level statements in your statement.
    Reset your log level to info and try again.
    i.e. --> log4j.rootLogger=info, R
    shl_libra likes this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    shl_libra, don't double post the same question. Your other thread has been removed.

    db

  4. #4
    shl_libra is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    Changed Debug to Info , but still no log file created . I have one more doubt where would be log4j.properties file placed. I have placed in inside WEB-INF/classes/com/example/web directory . Is it correct ?

  5. #5
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Please put log4j.properties file in WEB-INF/classes folder.
    It should work without any problem :-)

  6. #6
    shl_libra is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    No my frnd its still nt wrkin....i hv doubt regarding line in log file "log4j.appender.R.File=C:/example.log" ....i thnk there some fault in this .........cause what i'm understanding servlet is not able to use log4j file.....as if it is not reaching it ......am i correct ?
    Plz reply if i'm wrong.....

  7. #7
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Default

    Everything looks fine in your code, so can't say much without running it.
    Just try 1 more thing: remove init param from web.xml and remove PropertyConfigurator related code from your Servlet. As you have already kept log4j.properties in WEB-INF/classes folder, web server should pick it up for you.

  8. #8
    shl_libra is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    But if i'll remove init param from web xml then how'll container know where to look for properties file......and if remove property configurator from servlet then how'll it parse the log and configure according to propertirs file.

Similar Threads

  1. Problem Configuring the JDK (slightly confused)
    By lumzi23 in forum New To Java
    Replies: 2
    Last Post: 12-23-2010, 07:12 PM
  2. servlet problem plz help
    By superbg in forum Advanced Java
    Replies: 1
    Last Post: 10-18-2010, 11:43 AM
  3. problem with configuring hibernate in struts
    By Trendy in forum Java Servlet
    Replies: 1
    Last Post: 11-23-2009, 10:05 AM
  4. servlet problem
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-29-2009, 10:34 AM
  5. Servlet problem
    By maheshkanda in forum New To Java
    Replies: 5
    Last Post: 02-10-2009, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •