spring配置log4j

1.引入log4j-xxx.jar包,buildpath.web

 

2.在項目的根目錄下新建resources名的文件夾,注意是source folder,並新建log4j.properties文件spring

 

3.在log4j.properties中配置:apache

#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:日誌文件的位置tomcat

1)絕對路徑app

log4j.appender.R.File=D:/log/xxx.logjvm

2)相對路徑:ui

log4j.appender.R.File=../logs/app.log,將日誌記錄到tomcat下的logs文件夾;spa

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}對應當前應用根目錄;3d

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-->

 

4.

相關文章
相關標籤/搜索