本文不赘述log4j的各种配置属性的意义只是简单的教你如何使用log4j的使用
1、首先新建一个java project
2、引入commons-logging-1.0.4.jar 和 log4j-1.2.15.jar两个包 版本号不限
3、在项目根目录下新建log4j.properties 文件内容如下
log4j.rootLogger=info, ServerDailyRollingFile, stdout
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern=‘.‘yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=logs/notify-subscription.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n
log4j.appender.ServerDailyRollingFile.Append=true
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n
4、新建测试类
public class TestLog4j {
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
Logger logger = Logger.getLogger(TestLog4j.class);
logger.warn("yes!");
logger.debug("yes!");
logger.error("yes!");
logger.info("yes!");
}
}
运行结果如下
2014-12-09 11:07:50 WARN [com.iss.log4j.TestLog4j] yes!
2014-12-09 11:07:50 ERROR [com.iss.log4j.TestLog4j] yes!
2014-12-09 11:07:50 INFO [com.iss.log4j.TestLog4j] yes!
好,基本的log4j
d