Results 1 to 4 of 4
  1. #1
    ashiers is offline Member
    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Default can't log to file

    Hi Guys,

    I'm trying to use log4j to write to a file for my Java web application.
    Based on what I've read, I have the following setup:

    My web.xml file contains the following:

    <servlet>
    <servlet-name>log4j-init</servlet-name>
    <servlet-class>com.tacticalenterprisesltd.Log4jInit</servlet-class>
    <init-param>
    <param-name>log4j-init-file</param-name>
    <param-value>WEB-INF\properties\log4j_FILE.properties</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>log4j-init</servlet-name>
    <url-pattern>/log4j-init/*</url-pattern>
    </servlet-mapping>

    My servlet Log4jInit looks like this:

    01.package com.tacticalenterprisesltd;
    02.
    03.import org.apache.log4j.PropertyConfigurator;
    04.import javax.servlet.http.*;
    05.
    06.
    07.public class Log4jInit extends HttpServlet
    08.{
    09. public void init()
    10. {
    11. try{
    12. String prefix = getServletContext().getRealPath("/");
    13. String file = getInitParameter("log4j-init-file");
    14. if(file != null)
    15. {
    16. System.out.println("Log4jInit.init - prefix + file: " + prefix + file);
    17. PropertyConfigurator.configure(prefix + file);
    18. }
    19. }
    20. catch(Exception e){e.printStackTrace();}
    21. }
    22.
    23. public void doGet(HttpServletRequest req, HttpServletResponse res){}
    24.}


    My properties file looks like this:

    # Define the root logger with appender file
    log4j.rootLogger = DEBUG, FILE

    # Define the file appender
    log4j.appender.FILE=org.apache.log4j.RollingFileAp pender
    # Set the name of the file
    log4j.appender.FILE.File=C:\\logs\\myLog.txt

    # Set the immediate flush to true (default)
    log4j.appender.FILE.ImmediateFlush=true

    # Set the threshold to debug mode
    log4j.appender.FILE.Threshold=debug

    # Set the append to false, should not overwrite
    log4j.appender.FILE.Append=true

    # Set the maximum file size before rollover
    log4j.appender.FILE.MaxFileSize=5KB

    # Set the the backup index
    log4j.appender.FILE.MaxBackupIndex=2

    # Define the layout for file appender
    log4j.appender.FILE.layout=org.apache.log4j.Patter nLayout
    log4j.appender.FILE.layout.conversionPattern=%m%n

    As you can see, I'm telling log4j to write to a file at C:\logs\myLog.txt Pretty straight forward right. Well, it doesn't seem to think so since it isn't writing to the file at all. I have another properties file which tells log4j to print to the console. It doesn't seem to have a problem with that one. Could someone please advise as to what might be going wrong. Any help will be appreciated.

    Alan

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: can't log to file

    When posting code here please wrap it in [code] tags [/code].

    I don't see any attempt to actually log anything in that code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: can't log to file

    Ive used log4j for a long time now. Never have I ever had to create a servlet to initialize it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: can't log to file

    Oh.
    Is that what that thinks it's doing?
    I just thought it was some random test code...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  2. Replies: 1
    Last Post: 08-04-2010, 03:24 PM
  3. Replies: 8
    Last Post: 02-18-2010, 12:54 PM
  4. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  5. Replies: 7
    Last Post: 05-23-2008, 03:46 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
  •