项目运行日志打印等级实现外部配置
日志等级配置项不能直接通过外部方式注入,由于加载顺序的问题会导致读取不到
- No enum constant org.springframework.boot.logging.LogLevel.${config.out.log-level}
1. 按前面配置外部配置文件的流程,配置日志等级
#SpringBoot项目外部配置过渡文件
#项目Main方法运行端口
server.port=${config.out.server-port:8002}
#运行日志打印级别
logging.level.com.skxe=${config.out.log-level}
#springboot-configout项目外部配置文件
#项目Main方法运行端口
config.out.server-port=8003
#项目名称
config.out.server-name=项目外部文件测试
#运行日志打印级别
config.out.log-level=debug
2. 通过Java代码,在读取配置文件属性的同时,实现日志级别的切换
取消上面中的logging.level配置项
package com.skxe.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
@ConfigurationProperties("config.out")
public class ApplicationOutConfig {
private LoggingSystem loggingSystem;
public ApplicationOutConfig(LoggingSystem loggingSystem) {
this.loggingSystem = loggingSystem;
}
private Integer serverPort;
private String serverName;
private String[] logLevels; //项目运行日志打印级别
public Integer getServerPort() {
return serverPort;
}
public void setServerPort(Integer serverPort) {
this.serverPort = serverPort;
System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println(" ------->>> 项目Main方法运行端口: " + serverPort +" <<<-------");
System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
}
public String getServerName() {
return serverName;
}
public void setServerName(String serverName) {
this.serverName = serverName;
System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println(" ------->>> 项目名称: "+ serverName +" <<<-------");
System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
}
public String[] getLogLevels() {
return logLevels;
}
public void setLogLevels(String[] logLevels) {
this.logLevels = logLevels;
if (null != logLevels){
for (String logLevel : logLevels) {
String[] ll = logLevel.split("=");
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println(" ------->>>项目日志记录级别 | "+ ll[0] + " | " + ll[1] + " ");
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
LogLevel level = LogLevel.valueOf(ll[1].trim().toUpperCase());
loggingSystem.setLogLevel(ll[0], level);
}
}
}
}
org.springframework.boot.logging.LoggingSystem支持的日志类型:
- org.springframework.boot.logging.logback.LogbackLoggingSystem
- org.springframework.boot.logging.log4j2.Log4J2LoggingSystem
- org.springframework.boot.logging.java.JavaLoggingSystem
日志的设置这里使用了字符串数组,是为了更好的自定义自己的不同包/类的日志级别
#springboot-configout项目外部配置文件
#项目Main方法运行端口
config.out.server-port=8003
#项目名称
config.out.server-name=项目外部文件测试
#运行日志打印级别
config.out.log-levels=com.skxe=debug
3. 通过接口测试日志等级的设置是否生效
通过加载顺序可以知道,我们的设置只是在对应的配置生效以后,之前的不会生效,
项目可以在 中先配置默认值,这里的配置主要是应对生产运行日志
package com.skxe.demo.api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/log")
@RestController
public class LogTest {
private Logger log = LoggerFactory.getLogger(this.getClass());
@RequestMapping("/test/level")
public String testConfig(){
log.debug("当前运行日志级别 | debug");
log.info("当前运行日志级别 | info");
log.warn("当前运行日志级别 | warn");
log.error("当前运行日志级别 | error");
String logLevel = null;
if (log.isDebugEnabled()) {
logLevel = "debug";
}
else if (log.isInfoEnabled()) {
logLevel = "info";
}
else if (log.isWarnEnabled()) {
logLevel = "warn";
}
else if (log.isErrorEnabled()) {
logLevel = "error";
}
return "log-level | " + logLevel;
}
}
- 项目打印日志:
2020-03-07 19:10:34.678 DEBUG 5224 --- [nio-8003-exec-1] com.skxe.demo.api.LogTest : 当前运行日志级别 | debug
2020-03-07 19:10:34.679 INFO 5224 --- [nio-8003-exec-1] com.skxe.demo.api.LogTest : 当前运行日志级别 | info
2020-03-07 19:10:34.679 WARN 5224 --- [nio-8003-exec-1] com.skxe.demo.api.LogTest : 当前运行日志级别 | warn
2020-03-07 19:10:34.679 ERROR 5224 --- [nio-8003-exec-1] com.skxe.demo.api.LogTest : 当前运行日志级别 | error
接口返回参数:
GET http://localhost:8003/log/test/level
HTTP/1.1 200
Content-Type: application/json
Content-Length: 18
Date: Sat, 07 Mar 2020 11:10:34 GMT
Keep-Alive: timeout=60
Connection: keep-alive
log-level | debug
网友评论