Springboot 日志

Springboot默认使用Logback作为日志框架实现

  1. 配置logback.xml
    

    
        ${LOG_PATH}/info.log
        
            ${LOG_PATH}/info-%d{yyyyMMdd}.log.%i
            
                500MB
            
            2
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n
            
        
    

    
        
            ERROR
        
        ${LOG_PATH}/error.log
        
            ${LOG_PATH}/error-%d{yyyyMMdd}.log.%i
            
            
                500MB
            
            2
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n

            
        
    
    
    
        
        
    
    
    
    
```
2. 配置application.properties
```#logback日志配置
logging.config=classpath:logback.xml
logging.path=D:/logFile```

其中logging.path定义的是logback.xml中${LOG_PATH}
3. 在类中定义log
```package com.duck.load;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 启动加载数据
 */
@Component
@Order(value = 1)
public class DuckLoad implements CommandLineRunner{

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

    public void run(String... args){
        logger.debug(this.getClass().getName() + "日志级别 Debug" + " DuckLoad 方法加载数据");
        logger.info(this.getClass().getName() + "日志级别 Info" + " DuckLoad 方法加载数据");
        logger.error(this.getClass().getName() + "日志级别 Error" + " DuckLoad 方法加载数据");
    }
}```
3. 启动查看后台日志
![image.png](https://upload-images.jianshu.io/upload_images/13686663-32705fa57f851a55.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
因为我们定义的日志级别是INFO,所以类中的DEBUG级别是不输出的,我们指输出INFO以上的日志(INFO ERROR)
4. 查看文件生成地址
![image.png](https://upload-images.jianshu.io/upload_images/13686663-31018fd94f52e7de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
日志文件生成路径在配置文件中logging.path定义

你可能感兴趣的:(Springboot 日志)