需求背景
SpringBoot用法:動態修改日誌輸出級別
問題痛點
SpringBoot在 spring-boot-starter-actuator 模塊中提供了日誌相關的EndPoint,通過該EndPoint可以在項目運行時不需要重啓服務就可以修改日誌的打印級別,解決了以前修改日誌打印級別必須要重啓服務的煩惱。
技術點
1. 集成actuator依賴組件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 配置EndPoint
# log
management:
endpoints:
web:
exposure:
include: loggers
代碼演示
1. 項目目錄結構
2. pom.xml依賴組件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.md</groupId>
<artifactId>spring-boot2-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>spring-boot2-log-level</artifactId>
<packaging>jar</packaging>
<name>spring-boot2-log-level</name>
<description>Spring Boot, MVC, Rest API for App</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 構建成可運行的Web項目 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib-ext-spring</artifactId>
</dependency>
<!-- swagger集成 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<!-- 默認swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!-- 更易用第三方swagger-ui組件 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. application.yml配置
server:
port: 9090
# log
management:
endpoints:
web:
exposure:
include: loggers
4. LogController設置類
我把接口做成了通過接口代碼訪問。當然,你也可以通過直接請求接口
LogController:
package com.md.demo.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.md.demo.util.HttpRequestUtil;
import com.md.demo.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
/**
* @author Minbo
*/
@RestController
@RequestMapping("/log")
@Api(tags = { "接口-演示" })
@Slf4j
public class LogController {
@Autowired
private Environment environment;
/**
* http://localhost:9090/testLog
*
* @return
*/
@GetMapping("/testLog")
public String testLog() {
log.debug("this is debug log");
log.info("this is info log");
return "Hello greetings from spring-boot2-log-level";
}
/**
* http://localhost:9090/getLogger
*
* @return
*/
@ApiOperation(value = "獲得當前項目:日誌輸出級別", httpMethod = "GET")
@GetMapping("/getLogger")
public JsonResult getLogger() {
String port = this.environment.getProperty("local.server.port");
String url = "http://localhost:" + port + "/actuator/loggers/com.md";
String result = HttpRequestUtil.sendGet(url);
log.info("當前項目:日誌輸出級別,url={},result={}", url, result);
return JsonResult.ok(result);
}
/**
* http://localhost:9090/setLogger
*
* @return
*/
@ApiOperation(value = "設置當前項目:日誌輸出級別(INFO/DEBUG)", httpMethod = "POST")
@PostMapping("/setLogger")
public JsonResult setLogger(@RequestHeader String logLevel) {
String port = this.environment.getProperty("local.server.port");
String url = "http://localhost:" + port + "/actuator/loggers/com.md";
String param = "{\"configuredLevel\":\"" + logLevel + "\"}";
String result = HttpRequestUtil.sendJsonPost(url, param);
log.info("設置當前項目:日誌輸出級別,url={},param={},result={}", url, param, result);
return JsonResult.ok();
}
}
5. 啓動類
Application:
package com.md.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
/**
* 程序主入口
*
* @author Minbo
*
*/
@SpringBootApplication
@EnableSwaggerBootstrapUI
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
/**
* 開啓過濾器功能
*
* @return
*/
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}
/**
* 跨域過濾器
*
* @return
*/
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
接口測試
1. 啓動後,訪問地址:http://localhost:9090/doc.html (已集成了swagger2框架,Swagger集成用法教程)
2. 獲得當前項目:日誌輸出級別
3. 設置當前項目:日誌輸出級別(INFO/DEBUG)
設置成INFO級別:
查看最新日誌級別:
4. 接口日誌輸出測試
@GetMapping("/testLog")
public String testLog() {
log.debug("this is debug log");
log.info("this is info log");
return "Hello greetings from spring-boot2-log-level";
}
控制檯輸出:
只輸出了info級別日誌,證明已生效
完整源碼下載
該系列教程
我的專欄
至此,全部介紹就結束了
-------------------------------
-------------------------------
關於我(個人域名)
期望和大家一起學習,一起成長,共勉,O(∩_∩)O謝謝
歡迎交流問題,可加個人QQ 469580884,
或者,加我的羣號 751925591,一起探討交流問題
不講虛的,只做實幹家
Talk is cheap,show me the code