SpringBoot配置Logback日志
2020-12-19 13:33
标签:函数 exception beans 需要 sdn 选择 ons code run SpringBoot1.5.20 SpringBoot项目中,spring-boot-starter已经包含了spring-boot-starter-logging,不需要再引入依赖 .end SpringBoot配置Logback日志 标签:函数 exception beans 需要 sdn 选择 ons code run 原文地址:https://www.cnblogs.com/maggieq8324/p/13381820.html环境
前言
代码配置
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目录下)
ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy
按时间,大小切割日志文件,效果如下所示:
文章标题:SpringBoot配置Logback日志
文章链接:http://soscw.com/index.php/essay/37212.html