2 Minutes To Code (β Version)

Log4j - 2 minute tutorial

How to make log files rotate based on size and zip old rotated logs programmatically

		package com.techfundaes.log4j;

		import org.apache.log4j.Layout;
		import org.apache.log4j.Logger;
		import org.apache.log4j.PatternLayout;
		import org.apache.log4j.RollingFileAppender;

		public class ProgrammaticConfigRollingFileOnSize
		{
			public static void main(String[] args)
			{
				Logger logger = Logger.getLogger(ProgrammaticConfigRollingFileOnSize.class);
				Layout layout = new PatternLayout("%d [%t] %-5p (%F:%L) - %m%n");
				RollingFileAppender rollingFileAppender = null;
				try
				{
					rollingFileAppender = new RollingFileAppender(layout, "app.log", false);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				rollingFileAppender.setMaxBackupIndex(5);
				rollingFileAppender.setMaximumFileSize(1000000);
				
				logger.addAppender(rollingFileAppender);
				while(true)
					logger.info("Logged this message!!!");
			}
		}