Log4J的配置文件(Configuration File)就是用来设置记录器的级别、存放器和布局的。
import org.apache.log4j.Logger;//头文件导入 import org.apache.log4j.PropertyConfigurator; public class TestLog4J { public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties");//利用log4j读取配置文件 Logger logger = Logger.getLogger(TestLog4J.class); for(int i = 0; i < 10000; i++){ logger.info("zhujiadun"); logger.debug("debug " + i); } } }
log4j.rootLogger=DEBUG, stdout, log, errorlog ###Console ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} [ %t ] [ %p ]:%L - %m%n ### Log ### log4j.appender.log = org.apache.log4j.DailyRollingFileAppender log4j.appender.log.File = log/log.log log4j.appender.log.Append = true log4j.appender.log.Threshold = INFO log4j.appender.log.DatePattern='.'yyyy-MM-dd log4j.appender.log.layout = org.apache.log4j.PatternLayout log4j.appender.log.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t ] %m%n ### Error ### log4j.appender.errorlog = org.apache.log4j.DailyRollingFileAppender log4j.appender.errorlog.File = log/errorlog.log log4j.appender.errorlog.Append = true log4j.appender.errorlog.Threshold = DEBUG log4j.appender.errorlog.DatePattern='.'yyyy-MM-dd log4j.appender.errorlog.layout = org.apache.log4j.PatternLayout log4j.appender.errorlog.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss} [ %t ] %m%n
#根配置 log4j.rootLogger = [ level ] , appenderName1 , appenderName2 , … #配置日志信息输出目的地Appender log4j.appender.appenderName = fully.qualified.name.of.appender.class log4j.appender.appenderName.option1 = value1 … log4j.appender.appenderName.optionN = valueN #配置日志信息的格式(布局) log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class log4j.appender.appenderName.layout.option1 = value1 … log4j.appender.appenderName.layout.optionN = valueN
原文地址:http://blog.csdn.net/some_times/article/details/41517453