SpringBoot配置Logback日志

环境

SpringBoot1.5.20

前言

SpringBoot项目中,spring-boot-starter已经包含了spring-boot-starter-logging,不需要再引入依赖

代码配置

  • LogConfig.java
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;

@Controller
@Configuration
public class LogConfig {
	/**
     * 获取日志对象,构造函数传入当前类,查找日志方便定位
     */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 端口
     */
    @Value("${server.port}")
    private String port;
    
    /**
     * 启动成功
     */
    @Bean
    public ApplicationRunner applicationRunner() {
        return applicationArguments -> {
            try {
                InetAddress address = InetAddress.getLocalHost();
                //获取本机内网IP
                log.info("启动成功:" + "http://" + address.getHostAddress() + ":" + port + "/");
            } catch (UnknownHostException ex) {
                ex.printStackTrace();
            }
        };
    }
}
  • application.yml
server:
  port: 8088

logging:
    path: ./logs #日志文件路径
    file: log.log #日志文件名称
    level:
       root: info #日志级别 root表示所有包,也可以单独配置具体包 fatal error warn info debug trace off
  • logback-spring.xml(src\main\resources目录下)


    
    
    
    

    
    
    
    

    
    
        
        
            
            ${LOG_HOME}/${APP_NAME}-%d{yyyy-MM-dd}.%i.log
            
            15
            200MB
        
        
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            
            
            utf8
        
    

    
    
        
        
            
            
        

        
        
        
        
    
    

    

  • 注:ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy按时间,大小切割日志文件,效果如下所示:
    SpringBoot配置Logback日志_第1张图片

.end

你可能感兴趣的:(SpringBoot配置Logback日志)