spring配置log4j
1.引入log4j-xxx.jar包,buildpath.
2.在项目的根目录下新建resources名的文件夹,注意是source folder,并新建log4j.properties文件
3.在log4j.properties中配置:
#LOG_DIR:/usr/local/.../logs LOG_DIR = E:\\logs #Level:ERROR,WARN,INFO,DEBUG log4j.rootLogger = DEBUG,Console,FileInfo,FileError log4j.appender.Console = org.apache.log4j.ConsoleAppender log4j.appender.Console.Threshold = DEBUG log4j.appender.Console.ImmediateFlush = true log4j.appender.Console.Target = SYSTEM_OUT log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n log4j.appender.FileInfo = org.apache.log4j.DailyRollingFileAppender log4j.appender.FileInfo.Threshold = INFO log4j.appender.FileInfo.ImmediateFlush = true log4j.appender.FileInfo.Append = true log4j.appender.FileInfo.DatePattern = '_'yyyy-MM-dd'.log' log4j.appender.FileInfo.encoding=UTF-8 log4j.appender.FileInfo.File = ${LOG_DIR}/info log4j.appender.FileInfo.layout = org.apache.log4j.PatternLayout log4j.appender.FileInfo.layout.ConversionPattern = [%-5p][%d{ISO8601}]%m%n log4j.appender.FileError = org.apache.log4j.DailyRollingFileAppender log4j.appender.FileError.Threshold = ERROR log4j.appender.FileError.ImmediateFlush = true log4j.appender.FileError.Append = true log4j.appender.FileError.DatePattern = '_'yyyy-MM-dd'.log' log4j.appender.FileError.encoding=UTF-8 log4j.appender.FileError.File = ${LOG_DIR}/error log4j.appender.FileError.layout = org.apache.log4j.PatternLayout log4j.appender.FileError.layout.ConversionPattern = [%-5p][%d{ISO8601}]%m%n log4j.appender.cn.yivi.service.pay = info,pay log4j.additivity.cn.yivi.service.pay = false log4j.appender.pay = org.apache.log4j.DailyRollingFileAppender log4j.appender.pay.Threshold = WARN log4j.appender.pay.ImmediateFlush = true log4j.appender.pay.Append = true log4j.appender.pay.DatePattern = '_'yyyy-MM-dd'.log' log4j.appender.pay.encoding=UTF-8 log4j.appender.pay.File = ${LOG_DIR}/pay log4j.appender.pay.layout = org.apache.log4j.PatternLayout log4j.appender.pay.layout.ConversionPattern = [%-5p][%d{ISO8601}]%m%n
tips:日志文件的位置
1)绝对路径
log4j.appender.R.File=D:/log/xxx.log
2)相对路径:
log4j.appender.R.File=../logs/app.log,将日志记录到tomcat下的logs文件夹;
log4j.appender.R.File=logs/app.log,将日志记录到tomcat的bin目录下的logs文件夹;
(3)使用环境变量相对路径法:程序会优先找jvm环境变量,然后再找系统环境变量,来查找配置文件中的变量。
log4j.appender.logfile.File=${user.dir}/logs/app.log,使用tomcat容器时${user.dir}对应tomcat的bin目录;
log4j.appender.logfile.File=${user.home}/logs/app.log,${user.home}对应操作系统当前用户目录;
log4j.appender.logfile.File=${webApp.root}/logs/app.log,${webApp.root}对应当前应用根目录;
4.在web.xml中配置log4j:
<!-- 配置日志 start--> <!-- 加载Log4J 配置文件 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value> classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>3000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 配置日志 end-->