zoukankan      html  css  js  c++  java
  • sprigboot项目中配置xml格式的logback

    slf4j依赖和logback的依赖

    idea中springboot项目的resources目录下新建logback-spring.xml文件,内容大致如下:

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <configuration>
    
        <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>
                    %d - %msg%n
                </pattern>
            </layout>
        </appender>
    
        <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <!--除了WARN级别的日志不输出外,其他级别日志都输出-->
                <level>WARN</level>
                <onMatch>DENY</onMatch>
                <onMismatch>ACCEPT</onMismatch>
            </filter>
            <encoder>
                <pattern>
                    %msg%n
                </pattern>
            </encoder>
            <!--滚动策略-->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--路径-->
                <fileNamePattern>/Users/yanguobin/Desktop/shell/info.%d.log</fileNamePattern>
            </rollingPolicy>
        </appender>
    
        <appender name="fileWarnLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <!--只输出WARN及以上级别的日志-->
                <level>WARN</level>
            </filter>
            <encoder>
                <pattern>
                    %msg%n
                </pattern>
            </encoder>
            <!--滚动策略-->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--路径-->
                <fileNamePattern>/Users/yanguobin/Desktop/shell/warn.%d.log</fileNamePattern>
            </rollingPolicy>
        </appender>
    
        <root level="info">
            <appender-ref ref="consoleLog" />
            <appender-ref ref="fileInfoLog" />
            <appender-ref ref="fileWarnLog" />
        </root>
    
    </configuration>

    测试类如下:

    package com.example.shell;
    
    import lombok.extern.slf4j.Slf4j;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @Slf4j
    public class LoggerTest {
    
        @Test
        public void test1(){
            log.debug("debug...");
            log.info("info...");
            log.warn("warn...");
            log.error("error...");
        }
    }

     桌面生成了日志文件

  • 相关阅读:
    用的fileupload组件实现的大文件上传
    手机刷机
    Bonferroni correction
    FTP 常用命令
    Oracle 10 参数配置说明
    Solaris 10 ftp,telnet,ssh,sendmail
    solaris10下安装oracle10g
    solaris10中安装oracle内核参数的调整
    matlab从txt/csv文件中读取一行
    SUN服务器Solaris10安装步骤
  • 原文地址:https://www.cnblogs.com/yanguobin/p/11530706.html
Copyright © 2011-2022 走看看