前提:
0:ssm框架已经搭建并且成功运行
1、maven环境配置成功
2、tomcat配置成功,并且配置本机的tomcat环境变量
内容:
0、导入所需要的jar包
<!-- 配置log4j日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
1、在Resource下面创建log4j.properties
内容如下:
#配置跟Logger,[$1]定义日志的全局级别,低于debug级别的不会被输出[$2]是输出的位置
log4j.rootLogger=info,systemOut,logFile,ExceptionFile
#####################配置日志输出到Console#####################
log4j.appender.systemOut=org.apache.log4j.ConsoleAppender
#灵活的制定布局格式
log4j.appender.systemOut.layout=org.apache.log4j.PatternLayout
#设置日志显示的格式
log4j.appender.systemOut.layout.ConversionPattern=%5p %d %C: %m%n
#指定Console日志输出的最低级别
log4j.appender.systemOut.Threshold=debug
#设置所有日志都会被立即输出
log4j.appender.systemOut.ImmediateFlush=true
#设置日志输出到控制台的颜色(out黑色,err红色)
log4j.appender.systemOUt.Target=System.out
#####################配置日志输出到logs_for_ate.log#####################
log4j.appender.logFile=org.apache.log4j.FileAppender
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=%5p %d %C: %m%n
log4j.appender.logFile.Threshold=debug
log4j.appender.logFile.ImmediateFlush=true
log4j.appender.logFile.Append=true
log4j.appender.logFile.File=${catalina.home}/logs/logs_for_ate.log
log4j.appender.logFile.Encoding=UTF-8
###############配置error日志输出到exception_logs_for_ate################
log4j.appender.ExceptionFile=org.apache.log4j.FileAppender
log4j.appender.ExceptionFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ExceptionFile.layout.ConversionPattern=%5p %d %C: %m%n
log4j.appender.ExceptionFile.Threshold=error
log4j.appender.ExceptionFile.ImmediateFlush=true
log4j.appender.ExceptionFile.Append=true
log4j.appender.ExceptionFile.File=${catalina.home}/logs/exception_logs_for_ate.log
log4j.appender.ExceptionFile.Encoding=UTF-8
2、在web.xml中配置加载log4j.properties
<!-- 加载log4j.properties --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
3、这样在tomcat的logs目录下就会出现日志文件,在console也会出现日志