Sl4J日志使用

日志框架使用配置

1、日志框架Slf4j的引入

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

2、日志框架Slf4j的基本使用

使用注解@Slf4j,日志调用方法log.debug()等

    @Test
    public void contextLoads() {
        log.debug("debug......");
        log.info("info.......");
        log.error("error.......");
        log.info("info.......");
        log.info("info.......");
    }

3、日志框架变量输出形式

log.debug(“name {},password {}”,name,password) 如图

4、日志框架配置文件

  • 基本配置application.yml
logging:
  pattern:
    console: "%d - %msg%n"   #日志输出格式
  file:/var/log/tomcat/sell.log  #日志文件输出位置
  level:
    com.sell.LoggerTest: debug  # 某个输出的日志级别
  • 标准xml配置–建立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>/var/log/tomcat/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>/var/log/tomcat/sell/error.%d.logfileNamePattern>
        rollingPolicy>
    appender>
    
    <root level="info">
        
        <appender-ref ref="consoleLog" />
        
        <appender-ref ref="fileInfoLog" />
        
        <appender-ref ref="fileErrorLog" />
    root>

configuration>

你可能感兴趣的:(框架知识梳理)