美文网首页
Spring 配置日志

Spring 配置日志

作者: 阿西Enzo | 来源:发表于2017-10-08 10:51 被阅读0次

    Spring 配置日志


    参考:http://www.cnblogs.com/hapjin/p/5520547.html

    添加依赖

    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    

    创建log4j.properties

    log4j.rootLogger=DEBUG, Console,RollingFile, DailyRollingFile
    
    #Console
    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    log4j.appender.Console.Threshold=DEBUG
    log4j.appender.Console.ImmediateFlush=true
    log4j.appender.Console.Targer=System.out
    log4j.appender.Console.layout=org.apache.log4j.PatternLayout
    #log4j.appender.Console.layout.ConversionPattern=%d %-5p [%c{5}] - %m%n
    log4j.appender.Console.layout.ConversionPattern= [%-5p] [%-25d{yyyy-MM-dd HH:mm:sss,SSS}] [%l] %n %m %n
    
    #RollingFileAppender
    #log4j.appender.RollingFile=org.apache.log4j.RollingFileAppender
    #log4j.appender.RollingFile.Threshold=DEBUG
    #log4j.appender.RollingFile.File=../logs/bim/bim1.log
    #log4j.appender.RollingFile.MaxFileSize=10KB
    #log4j.appender.RollingFile.Appender=true
    #log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
    #log4j.appender.RollingFile.layout.ConversionPattern= [%-5p] [%-25d{yyyy-MM-dd HH:mm:sss,SSS}] [%l] %n %m %n
    
    #DailyRollingFile
    log4j.appender.DailyRollingFile=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.RollingFile.Threshold=INFO
    log4j.appender.DailyRollingFile.File=../logs/bim/bim_
    log4j.appender.DailyRollingFile.Appender=true
    log4j.appender.DailyRollingFile.DatePattern=yyyy-MM-dd-a'.txt'
    log4j.appender.DailyRollingFile.layout=org.apache.log4j.PatternLayout
    log4j.appender.DailyRollingFile.layout.ConversionPattern=[%-5p] [%-25d{yyyy-MM-dd HH:mm:sss,SSS}] [%l] %n %m %n
    

    配置Web.xml

    <!-- 加载log4j的配置文件log4j.properties -->
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>classpath:log4j.properties</param-value>
        </context-param>
    
        <!-- 设定刷新日志配置文件的时间间隔,这里设置为10s -->
        <context-param>
            <param-name>log4jRefreshInterval</param-name>
            <param-value>10000</param-value>
        </context-param>
    
        <!-- 加载Spring框架中的log4j监听器Log4jConfigListener -->
       <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
    
        <!-- 为避免项目间冲突,定义唯一的 webAppRootKey -->
        <context-param>
            <param-name>webAppRootKey</param-name>
            <param-value>scheduleProject</param-value>
        </context-param>
    

    ①log4jConfigLocation 指定Spring从哪个目录下加载 log4j.properties 配置文件

    ②log4jRefreshInterval 当修改了配置文件时,不需要重启就能加载变化了的log4j.properties 配置文件

    ③webAppRootKey 项目的标识,一个窗口中可能部署了多个项目,用它进行区分。当配置 日志文件的输出目录时,可能会用到它

    使用Logger

    import org.apache.log4j.Logger;
    
    class  Test{
    
    private static Logger logger = Logger.getLogger(Test.class);
    //....other code
    
    logger.info("info ");
    logger.error("error");
    logger.warn("warn");
    
    .......
    

    相关文章

      网友评论

          本文标题:Spring 配置日志

          本文链接:https://www.haomeiwen.com/subject/phreyxtx.html