1:首先导入需要的jar包
2:创建log4j.properties文件
log4j.rootLogger=info,stdout
#log4j.rootLogger=error,daily_file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.daily_file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.daily_file.DatePattern = -yyyy-MM-dd
log4j.appender.daily_file.layout=org.apache.log4j.PatternLayout
log4j.appender.daily_file.layout.ConversionPattern = %d{MM-dd HH:mm:ss},%-5p [%r] [%t] %c{2} - %m%n
log4j.appender.daily_file.File =log.log/log4j.log
3:在web.xml中配置
log4jConfigLocation
classpath:log4j.properties//log4j.properties文件下的绝对路径
4:然后即可使用啦,例子:
private static Logger log = Logger.getLogger(TextController.class);
http://log.info("这是Info级别");
网友评论