package com.v512.log4j;
import org.apache.log4j.Logger;
public class HelloLog4J {
// 构造记录器,形参是记录器所在的类,表示要在该类做日志
private static Logger logger = Logger.getLogger(HelloLog4J.class);
/**
* @param args
*/
public static void main(String[] args) {
getMessage();
}
private static void getMessage() {
// 记录下各种级别的信息,这些信息放在哪儿,以哪种方式存放,在log4j.properties文件中配置.
logger.debug("This is debug message.");
logger.info("This is a info message.");
logger.warn("This is a warn message.");
logger.error("This is a error message.............");
}
}
log4j.rootLogger=info,file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=Message.log
log4j.appender.file.Append=false
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyyMMdd HH:mm:ss} %p %l [%t] %m%n
Message.log(工程目录下)
20120730 11:21:13 INFO com.v512.log4j.HelloLog4J.getMessage(HelloLog4J.java:18) [main] This is a info message.
20120730 11:21:13 WARN com.v512.log4j.HelloLog4J.getMessage(HelloLog4J.java:19) [main] This is a warn message.
20120730 11:21:13 ERROR com.v512.log4j.HelloLog4J.getMessage(HelloLog4J.java:20) [main] This is a error message.............
总结:,log4j.appender.file.Append=false,不追加写到文件,最后输出的信息总是覆盖掉前面输出的信息