2 Minutes To Code (β Version)

Log4j - 2 minute tutorial

How to make log files rotate per day and zip old rotated logs programmatically

		package com.techfundaes.log4j;

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

		public class ProgrammaticConfigRollingFileOnTimePeriod
		{
			public static void main(String[] args)
			{
				Logger logger = Logger.getLogger(ProgrammaticConfigRollingFileOnTimePeriod.class);
				Layout layout = new PatternLayout("%d [%t] %-5p (%F:%L) - %m%n");
				DailyRollingFileAppender dailyRollingFileAppender = null;
				try
				{
					dailyRollingFileAppender = new DailyRollingFileAppender(layout, "max.log", "yyyyMMdd-HHmm");
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				logger.addAppender(dailyRollingFileAppender);
				while(true)
					logger.info("Logged this message!!!");
			}
		}