logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ashiers <alan.shi...@bellaliant.net>
Subject cant log to file
Date Fri, 09 Aug 2013 17:50:22 GMT
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.RollingFileAppender 
 # 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.PatternLayout 
 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 




--
View this message in context: http://apache-logging.6191.n7.nabble.com/cant-log-to-file-tp39514.html
Sent from the Log4j - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message