点餐系统---日志

1. 创建Loggertest类

点餐系统---日志_第1张图片

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

    private final Logger logger = LoggerFactory.getLogger(Loggertest.class);
    @Test
    public void test1(){
        String name = "jess";
        String password = "123456";

        //日志的级别;        
        //由低到高   trace
        log.debug("debug...");
        log.info("info...");
        log.warn("warn...");
        log.info("name:{},password:{}",name,password);
        log.error("error...");

    }
}

没有输出debug级别的信息,因为系统默认的级别为Info,比info级别大的能够输出,比info级别小的不能输出

2. lombok工具的使用

由于我们写日志类每次都要写当前类,为了方便,使用lombok提供的一个注解:@Slf4j

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

    @Test
    public void test1(){
        String name = "jess";
        String password = "123456";
        log.debug("debug...");
        log.info("info...");
        log.warn("warn...");
        log.info("name:{},password:{}",name,password);
        log.error("error...");
    }
}

3. 日志的配置(logback-spring.xml)


<configuration>

	
    
    
        <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>C:\Users\jess\Desktop\java web\sell\日志\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>C:\Users\jess\Desktop\java web\sell\日志\error.%d.logfileNamePattern>
        rollingPolicy>
    appender>

    <root level="info">
        <appender-ref ref="consolelog" />
        <appender-ref ref="fileInfolog" />
        <appender-ref ref="fileErrorlog" />
    root>
configuration>

在这里插入图片描述

你可能感兴趣的:(#)