2 Minutes To Code (β Version)

Log4j - 2 minute tutorial

Custom Pattern : Setting tokens/terms to be logged for every message programmatically

		package com.techfundaes.log4j;

		import org.apache.log4j.Layout;
		import org.apache.log4j.Logger;
		import org.apache.log4j.PatternLayout;
		import org.apache.log4j.rolling.FixedWindowRollingPolicy;
		import org.apache.log4j.rolling.RollingFileAppender;
		import org.apache.log4j.rolling.SizeBasedTriggeringPolicy;

		public class ProgrammaticConfigArchiveRollingFileOnSize
		{
			public static void main(String[] args)
			{
				Logger logger = Logger.getLogger(ProgrammaticConfigArchiveRollingFileOnSize.class);
				Layout layout = new PatternLayout("%d [%t] %-5p (%F:%L) - %m%n");
				RollingFileAppender rollingFileAppender = new RollingFileAppender();
				rollingFileAppender.setLayout(layout);
				rollingFileAppender.setFile("worker.log");
				FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy();
				fixedWindowRollingPolicy.setFileNamePattern("worker-%i.log.gz");
				fixedWindowRollingPolicy.setMaxIndex(5);
				SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy(5000000);
				rollingFileAppender.setTriggeringPolicy(sizeBasedTriggeringPolicy);
				rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy);
				rollingFileAppender.activateOptions();
				logger.addAppender(rollingFileAppender);
				
				while(true)
					logger.info("Logged this message!!!");
			}
		}