在 Spring Boot 应用程序中在运行时更改日志记录级别 [英] Change logging level at runtime in spring boot application

查看:24
本文介绍了在 Spring Boot 应用程序中在运行时更改日志记录级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Spring Boot 应用程序中在运行时更改日志记录级别(从 DEBUG 到 ERROR 或 WARN).代码如下.

I am trying to change the logging levels (from DEBUG to ERROR or WARN) at runtime in spring boot application. The code is given below.

我已关注以下帖子,但仍然发布更改日志记录级别的请求无效.请查看下面的请求,但应用程序没有响应,我也没有在日志中看到任何错误.

I have followed the below posts, but still posting the request to change the logging level did not work. Please look below for request and no response from application and I do not see any error in the logs either.

我该怎么做在运行时更改日志级别而无需重新启动 Spring Boot 应用程序

https://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#production-ready-logger-configuration

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>

    <artifactId>spring-boot-web-spring-security</artifactId>
    <packaging>jar</packaging>
    <name>Spring Boot Web Spring Security</name>
    <description>Spring Boot Web Spring Security Example</description>
    <url>https://www.mkyong.com</url>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>

        <!-- hot swapping, disable cache for template, enable live reload -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- Optional, for bootstrap -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

控制器类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;



@Controller
public class DefaultController {

    Logger logger = LoggerFactory.getLogger(DefaultController.class);

    @GetMapping("/")
    public String home1() {
        logger.info("returning default /");
        return "/home";
    }

    @GetMapping("/home")
    public String home() {
        logger.debug("returning home");
        return "/home";
    }

    @GetMapping("/admin")
    public String admin() {
        logger.warn("returning admin");
        return "/admin";
    }

    @GetMapping("/user")
    public String user() {
        logger.error("returning user");
        return "/user";
    }

    @GetMapping("/about")
    public String about() {
        logger.trace("returning about");
        return "/about";
    }

    @GetMapping("/login")
    public String login() {
        logger.warn("returning login");
        return "/login";
    }

    @GetMapping("/403")
    public String error403() {
        return "/error/403";
    }

}

SpringSecurityConfig

  protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable()
        .authorizeRequests()
                .antMatchers("/", "/home", "/about","/proxy/**","/app/**","/admin/**","/loggers/**","/loggers/de.springbootbuch/**",
                        "/loggers/de.springbootbuch/").permitAll()
                .anyRequest().authenticated()
                .antMatchers("/admin/").hasAnyRole("ADMIN")
                .antMatchers("/user/**").hasAnyRole("USER")
                //.antMatchers("/admin/**").hasAnyRole("ADMIN")
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll()
                .and()
                .exceptionHandling().accessDeniedHandler(accessDeniedHandler);  
    }

Application.properties对这个文件有什么要处理的吗

logging.level.org.springframework.security=ERROR
logging.level.com.test=DEBUG
management.security.enabled=false

发送更改日志级别的请求:

 curl -X "POST" "http://localhost:8080/loggers/de.springbootbuch"      -H "Content-Type: application/json; charset=utf-8"      -d $'{
  "configuredLevel": "WARN"
}'

curl -X "POST" "http://localhost:8080/loggers/de.springbootbuch"      -H "Content-Type: application/json; charset=utf-8"      -d $'{
  "configuredLevel": "ERROR"
}'

推荐答案

您可以使用 spring-boot 执行器的 loggers 端点来更改所需类的日志记录级别.在 spring-boot 1.5 中,您可以使用:

You can use spring-boot actuator's loggers endpoint to change logging level of desired class. In spring-boot 1.5, you can use:

`curl -X "POST" "http://localhost:8080/loggers/<class or package name>" -H "Content- 
Type: application/json; charset=utf-8" -d $'{
"configuredLevel": "ERROR"
}'`

从 spring-boot 执行器端点需要以 actuator 为前缀.

Starting with spring-boot actuator endpoints need to be prefixed with actuator.

这篇关于在 Spring Boot 应用程序中在运行时更改日志记录级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆