Description : Log4j Example with BasicConfigurator and PropertyConfigurator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/*>>>>>>>>>>>>>>>>Program1 with BasicConfigurator<<<<<<*/ /*>>>>>>>>>>>>>>>>SampleLog4j.java<<<<<<<<<<<<<<<<<<*/ package com.example; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Level; import org.apache.log4j.Logger; public class SampleLog4j { static final Logger logger = Logger.getLogger(SampleLog4j.class); public static void main(String... args) { BasicConfigurator.configure(); Logger.getRootLogger().setLevel(Level.DEBUG); System.out.println(logger.isDebugEnabled()); logger.debug("Sample debug message"); logger.info("Sample info message"); logger.warn("Sample warn message"); logger.error("Sample error message"); logger.fatal("Sample fatal message"); } } /* >>>>>>>>>>>>>>>Program1 ends<<<<<<<<<<<<<<<<<<<< */ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/*>>>>>>>>>>>>>>>Program 2 with PropertyConfigurator<<<*/ /*>>>>>>>>>>>>>>>>SampleLog4j.java<<<<<<<<<<<<<<<<<*/ package com.example; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.Level; import org.apache.log4j.Logger; public class SampleLog4j { static final Logger logger = Logger.getLogger(SampleLog4j.class); public static void main(String... args) { PropertyConfigurator.configure("log4j.properties"); System.out.println(logger.isDebugEnabled()); logger.debug("Sample debug message"); logger.info("Sample info message"); logger.warn("Sample warn message"); logger.error("Sample error message"); logger.fatal("Sample fatal message"); } } |
/*>>>>>>>>>>>>>>>log4j.properties<<<<<<<<<<<<<<<<<<<<*/ log4j.rootLogger=WARN, CA log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n /*>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/