java-動態切換日誌級別的3種方案

1 經過Spring Boot Actuator 組件

1 修改yml配置文件

management:
  endpoints:
    web:
      exposure:
        include: 'loggers'
複製代碼

2 添加依賴

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
複製代碼

3 查看全局全部定義的日誌級別

輸入地址:http://localhost:8888/actuator/loggerscss

R

4 修改指定日誌級別(注意參數)

5 修改先後對比(注意參數)

修改前:http://localhost:8888/actuator/loggers/ROOThtml

c.y.e.m.c.QueryController.getSkuInfo 測試info級別數據日誌 
複製代碼

修改後:web

[DEBUG 2019-10-10 16:45:12.726][295553873287841793][http-nio-8888-exec-8] c.y.e.m.c.QueryController.getSkuInfo 測試DEBUG級別數據日誌 
[INFO  2019-10-10 16:45:12.727][295553873287841793][http-nio-8888-exec-8] c.y.e.m.c.QueryController.getSkuInfo 測試info級別數據日誌 
複製代碼

注: 若有按照包名配置的話,URL爲:http://localhost:8888/actuator/loggers/包名類名全路徑 (com.xxx) 在http://localhost:8888/actuator/loggers 可查看到的能夠修改。spring

參考: Spring Boot 2動態修改日誌級別apache

經過springboot admin修改

1 pom設置(注意版本):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.6.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>
 <dependency>
     <groupId>de.codecentric</groupId>
     <artifactId>spring-boot-admin-starter-server</artifactId>
     <version>2.1.1</version>
 </dependency>
 <dependency>
     <groupId>de.codecentric</groupId>
     <artifactId>spring-boot-admin-starter-client</artifactId>
     <version>2.1.1</version>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
複製代碼

2 yaml配置

我這裏是客戶端和服務端我在一塊兒的。api

anagement:
  endpoints:
    web:
      exposure:
        include: "*"
spring:
  boot:
    admin:
      client:
        url: http://127.0.0.1:8888
        instance:
          service-base-url: http://127.0.0.1:8888/
  profiles:
    active: dev
複製代碼

3 啓動類配置:增長@EnableAdminServer

@SpringBootApplication
@EnableAdminServer
public class EasyGoMiddlewareApplication {
    private final static Logger logger = LoggerFactory.getLogger(EasyGoMiddlewareApplication.class);
    public static void main(String[] args) {
    /**
     * org.apache.logging.slf4j.Log4jLoggerFactory
     */
        try {
            SpringApplication.run(EasyGoMiddlewareApplication.class, args);
            logger.info("數據同步應用啓動成功!");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("數據同步-啓動異常,e=", e);
        }
    }
}
複製代碼

4 效果

地址欄輸入:http://127.0.0.1:8888/springboot

可直接點擊對應的日誌級別,動態修改。

[DEBUG 2019-10-11 19:44:05.787][295655724684412929][http-nio-8888-exec-10] c.y.e.m.c.QueryController.getSkuInfo 測試DEBUG級別數據日誌 
[INFO  2019-10-11 19:44:05.787][295655724684412929][http-nio-8888-exec-10] c.y.e.m.c.QueryController.getSkuInfo 測試info級別數據日誌 
複製代碼

5 與swagger2使用注意

http://localhost:8888/swagger-ui.htmlbash

按照上面配置後,swagger2就原先的地址就沒法訪問啦。 需處理下資源訪問路徑:框架

@Configuration
public class SpringConfig extends WebMvcConfigurerAdapter  {
    @Bean
    public TraceInterceptor traceInterceptor(){
        return new TraceInterceptor();
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/templates/**.js").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/templates/**.css").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(traceInterceptor()).addPathPatterns("/**");
    }
複製代碼

這樣配置後,就能夠直接訪問啦。ide

3 經過slf4j相關擴展api修改(適用任何框架)

咱們項目用的是log4j2,我只展現log4j2的demo,其餘日誌框架log4j、logback同理。

也能夠參考美團這個,比較全面點。

日誌級別動態調整——小工具解決大問題

pom引入:

<dependency>
             <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-core</artifactId>
             <version>2.11.1</version>
             <scope>compile</scope>
  </dependency>
複製代碼
/**
     * 動態修改log4j2日誌框架級別
     * @return
     */
    public String updateLog4j2Level(String motifyLevel){
        Level level = Level.toLevel(motifyLevel);
        LoggerContext context = (LoggerContext) LogManager.getContext(false);
        Configuration configuration = context.getConfiguration();
        for (Map.Entry<String, LoggerConfig> entry : configuration.getLoggers().entrySet()) {
            if (entry.getValue().getLevel().intLevel() != level.intLevel()) {
                entry.getValue().setLevel(level);
            }
        }
        configuration.getLoggerConfig("ROOT").setLevel(level);
        context.updateLoggers(configuration);
        return "sucess";
    }
複製代碼

這個可自行擴展,能夠到logger packge級別的。而不是全局的。自行擴展。

相關文章
相關標籤/搜索