SpringBoot日志输出(Logback)

SpringBoot之Logback 日志

- 创建一个基本的SpringBoot项目

SpringBoot日志输出(Logback)_第1张图片
编写一个测试方法:LoggerTest

@RunWith(SpringRunner.class)
@SpringBootTest
public class LoggerTest {

    private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);

    @Test
    public  void  test() {

        logger.debug("debug...");
        logger.info("info...");
        logger.warn("warn...");
        logger.error("error...");

    }
}

运行查看结果:
这里写图片描述

默认日志等级为info所以debug日志没有打印

- 使用{}来打印日志

logger.info("name = {} , password = {}",name,password);

- 使用注解打印日志
首先加入依赖

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

然后在加上@Slf4j 注解 ,在方法中使用log对象打印(idea中有时候可用需要添加插件lombok)

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {

    private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);

    @Test
    public  void  test() {

        String name = "imooc";
        String password = "123456";
        log.info("name = {} , password = {}",name,password);
        log.warn("warn...");
        log.error("error...");
        //Logger.debug("debug...");
        //logger.info("name = {} , password = {}",name,password);
        //logger.warn("warn...");
        //logger.error("error...");

    }
}

Logback的配置

日志配置可以分为两种:

  1. application.yml(配置简单,所做的事情也简单,不适用生产)
logging:
  pattern:
    console: "%d - %msg%n"         #输出日志的格式
  level:
    com.zhihua.sell.LoggerTest: debug          #指定类的日志级别
  #path: D:\log                                #指定输出到文件夹
  file: D:\log\sell.log                        #输出到指定文件
  1. logback-spring.xml (主流配置)
**

<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">
            <level>ERRORlevel>
            <onMatch>DENYonMatch>  
            <onMismatch>ACCEPTonMismatch>
        filter> 

        
        <encoder>
            <pattern>
                %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>D:\log\info.%d.logfileNamePattern>
        rollingPolicy>
    appender>


    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERRORlevel>
        filter>
        <encoder>
            <pattern>
                %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>D:\log\error.%d.logfileNamePattern>
        rollingPolicy>
    appender>

    
    <root level = "info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    root>

configuration>**

你可能感兴趣的:(springboot)