springboot中动态切换日志输出级别

springboot中动态切换log日志级别   (依靠actuator监控组件)
1、pom.xml配置

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
      <version>2.1.7.RELEASE</version>
    </dependency>
   
   <!-- 可选-->
  <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>

2、application.yml配置

server:
  port: 8082
  servlet:
    context-path: /
 
 
 
#管理端点
management:
  #server:
  #  port: 7001
  #  address: 127.0.0.1
  endpoint:
    shutdown:
      enabled: true
  endpoints:
    web:
      base-path: /actuator
      exposure:
        #include:   "*" #loggers
        include:
          - info
          - health
          - loggers
          - mappings
          - beans
          - env
          - shutdown
 
 
logging:
  level:
    root: INFO
    org.springframework.web: INFO
    org.springframework.jdbc: INFO
    com.tingcream: INFO  #项目包日志级别
  file: /log/logdemo/logdemo.log

3、 项目中代码中记录日志

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
  
@RestController
@EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true)
@SpringBootApplication
@Slf4j
public class LogDemoApplication extends SpringBootServletInitializer {
 
    public static void main(String[] args) {
        SpringApplication.run(LogDemoApplication.class, args);
    }
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(LogDemoApplication.class);
    }
 
 
    @RequestMapping("/")
    public String index(){
        log.info("这是一条info级别日志");
        log.debug("这是一条debug级别日志");
        return "hello springboot!";
    }
 
}

启动springboot项目后, 访问 http://localhost:8082/ 发现只输出了一条info级别的日志。
curl -X GET   http://192.168.0.14:8082/actuator/loggers/com.tingcream   
发现com.tingcream 的日志level配置的是info,所以debug级别的日志才没有输出过来

现在,我们使用curl 命令将com.tingcream包的日志level修改为debug 
curl -X POST http://192.168.0.14:8082/actuator/loggers/com.tingcream  -H "Content-type: application/json"  -d "{\"configuredLevel\":\"DEBUG\"}"
 

再次请求首页,发现info和debug级别的日志都输出来了。。

附,actuator常用的端点:
http://localhost:8082/actuator/health #健康
http://localhost:8082/actuator/info  #信息  获取的是application.yml中的info节点的信息,如info.app.name=xxx
http://localhost:8082/actuator/beans  #spring容器中的bean
http://localhost:8082/actuator/env  #环境变量
http://localhost:8082/actuator/metrics #指标
http://localhost:8082/actuator/mappings # controller层请求映射
http://localhost:8082/actuator/configprops # application.yml中的配置属性
优雅停机
curl -X POST http://localhost:8082/actuator/shutdown

 

在 Spring Boot 应用中,若希望配置控制台输出日志级别为 `INFO`,可以通过 `application.yml` 文件进行日志级别的设置。Spring Boot 默认使用 Logback 作为日志实现,但也可以通过依赖管理切换为 Log4j2 或其他日志框架。 ### 配置方式 #### 使用 Logback(默认) 如果使用的是默认的 Logback 日志系统,可以在 `application.yml` 中添加以下配置来设置控制台输出日志级别为 `INFO`: ```yaml logging: level: root: info ``` 该配置将全局日志级别设置为 `INFO`,包括控制台在内的所有日志输出都将遵循此级别。 #### 使用 Log4j2 若项目中使用了 Log4j2 作为日志框架,则需要在 `application.yml` 中指定 Log4j2 的配置文件路径,并在配置文件中定义日志级别输出格式。首先,在 `application.yml` 中添加如下内容以启用 Log4j2: ```yaml spring: logging: config: classpath:log4j2-spring.xml ``` 然后在 `log4j2-spring.xml` 文件中配置控制台输出日志级别为 `INFO`,示例如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration> ``` 上述配置中,`<Root level="info">` 表示将根日志级别设置为 `INFO`,并指定使用控制台输出日志信息[^1]。 ### 检查配置 为了验证日志配置是否生效,可以访问 `/actuator/configprops` 端点查看所有 `@ConfigurationProperties` 的配置项,其中包括日志相关的配置信息[^2]。确保应用已引入 Spring Boot Actuator 模块,并开启相关端点。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值