logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tan Yoke Yew <yy...@infopro.com.my>
Subject rolling file appender
Date Mon, 21 Apr 2003 03:47:29 GMT
hi there;

i have attached with [LogToTextFile2.java, ChkVal.java, config.properties]

i want to do RollingFileAppender, meaning if the log file exceed 2kb, it
will 
change it to ***.log.1

but, it seems everytime, the log size will increase to 3kb, after that
rollback 
to 1kb, it didnt generate the backup file.

may i know where is my error in the code below? thanks in advance.  


------LogToTextFile2 .java ----------

/**
 * @author yytan
 */

//import java.io.*;
import org.apache.log4j.*;

public class LogToTextFile2 extends PatternLayout{
	
	//static String thisLog = "D:/installer/eclipse
IDE/eclipse-SDK-2.0.2-win32/eclipse/workspace/testProject/thisLog.txt";
	//private static String thisLog = "../try1.log";
	//static String thisLog = "thisLog.txt";
	//static Category cat =
Category.getInstance(LogToTextFile1.class.getName());
	static Logger log =
Logger.getLogger(LogToTextFile2.class.getName());
	
	public LogToTextFile2(String pattern) {
    super(pattern);
    }
	
	public static void main(String[] args) throws Exception {
		String thisLog = "thisLog.log";
		String config  = "config.properties";
		int val1 = 10;
		int finalval = 0;
		
		Layout layout = new LogToTextFile2("%d{dd-MMM-yyyy,
hh:mm:ss,sss} %M [%t] (%F:%L) - %m%n");
	    //Category cat =
Category.getInstance(LogToTextFile1.class.getName());
	    log.addAppender(new FileAppender(layout, thisLog));
	    
	    //Allows the configuration of log4j from an external file. 
	    PropertyConfigurator.configure(config);
	    
	    log.debug("Enter log ...");
	    finalval = calc(val1);
	    System.out.println("finalval = " + finalval);
	    log.info("finalval = " + finalval);
	    
	    ChkVal cv = new ChkVal();
	    	cv.prVal(finalval);
	    log.info("Exit log.");
	}
	
	private static int calc (int a) {
		int geta = 0;
		int ans  = 0;
		
		log.info("get a = " + a);
		geta = a;
		ans = 2 * geta;
		
		return (ans);
	}
}


------- ChkVal.java------- 

/**
 * @author yytan
 */
import org.apache.log4j.*;

public class ChkVal extends PatternLayout{
	/**
	 * Constructor ChkVal.
	 * @param string
	 */
	public ChkVal(String string) {
	}

	/**
	 * Constructor ChkVal.
	 */
	public ChkVal() {
	}


	
	//static Category cat =
Category.getInstance(ChkVal.class.getName());	
	static Logger log = Logger.getLogger(ChkVal.class.getName());
	
	public void prVal(int a) throws Exception {
		String thisLog = "thisLog.log";
		
		Layout layout = new ChkVal("%d{dd-MMM-yyyy, hh:mm:ss,sss} %M
[%t] (%F:%L) - %m%n");	    
	    log.addAppender(new FileAppender(layout, thisLog));
	    
		if (a > 50)
			log.info("ur value is larger than 50");
		else
			log.info("try enter larger value");
	}
}


--------config.properties -------

log4j.rootLogger=DEBUG, R


log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=D:/installer/eclipse
IDE/eclipse-SDK-2.0.2-win32/eclipse/workspace/testProject/thisLog.log
log4j.appender.R.Append=true

log4j.appender.R.maxFileSize=2KB
# Keep one backup file
log4j.appender.R.maxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n




Regards;
yoke yew


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


Mime
View raw message