Wrote a small middleware, open source

The project address, you can click a star when you pass by :)

github.com/saigu/LogLe…

1. Why do you need LogLevelSwitch

With the continuous expansion of the business scale, the rapid location of each online fault is a huge challenge for developers.

The output of business-critical logic through logs is one of the effective means of locating problems. However, excessive logging will cause additional overhead to the system, and in extreme cases, may even lead to system downtime.

Therefore, in order to take into account both performance and fast location in fault scenarios, we need to be able to adjust the log level in real time, so as to output more log information to troubleshoot online problems, or reduce the performance consumption caused by log printing.

Wrote an open source small middleware - dynamic log level switch at runtime

Based on the above background, our LogLevelSwitch was born.

LogLevleSwitch is embedded in the application in the form of middleware. Based on the hot update capability of the configuration center, it realizes the ability to dynamically adjust the log level when the application is running.

2. Features

  • Compatibility : Support Log4j, Log4j2, Logback, etc.
  • Extensibility : You can use the default local configuration file-based configuration modification method ( only for local testing and learning ). In production, it is "strongly recommended" to access your own configuration center via SPI to achieve hot updates.
  • Ease of use : Easy access, outstanding effect, you can use it in just 5 minutes.

3. Basic Architecture

Wrote an open source small middleware - dynamic log level switch at runtime

Introduce our LogLevelSwitch in the application Application.

LogLevelSwitch contains two core components, LogContext and SwitchContext.

  • SwitchContext: Save the switch status and specific Logger configuration information obtained from the configuration center. By monitoring the configuration center message, the switch content is updated in real time, and the modification notification of the Logger level is realized.
  • LogContext: Saves the original Logger and log level information of the application, and can update or restore the Logger level according to the switch configuration.

4.Quick Start

Not much to say, come and try it.

4.1 普通spring项目

只用三步即可完成。

  • STEP 1: 应用中pom引入依赖

    io.github.saigu log-switch-core 1.0.0-beta
  • STEP 2: 构建config Bean

    @Configuration public class LogLevelSwitchConfig { @Bean LogLevelSwitch logLevelSwitch() { return new LogLevelSwitch(); } }

  • STEP 3: 接入配置中心

声明配置中心的SPI实现。

在resource路径下新建 META-INF/services,创建文件名为
io.github.saigu.log.level.sw.listener.ConfigListener的文件,并写入需要的「实现类名」。

实现一:项目自带的LocalFile配置中心

如果你还没有自己的配置中心,那就使用我们自带的基于本地配置文件进行本地测试学习。

「实现类名」为
io.github.saigu.log.level.sw.listener.LocalFileListener

注意,生产上 强烈推荐 通过「实现二」接入你自己的配置中心,实现热更新。

在resource目录下新建LocalSwitch.json文件:

Wrote an open source small middleware - dynamic log level switch at runtime

然后填写开关配置:

{
  "status": "on",
  "loggerBeans": [
    {
      "name": "all",
      "level": "error"
    }
  ]
}
复制代码

实现二:自定义SPI扩展配置,接入自己的配置中心

如果你已经有了自己的配置中心,那就可以通过SPI扩展配置,接入自己的配置中心。

「实现类名」为你自己的实现类名。

4.2 springboot项目

两步接入。

  • STEP 1: 应用中pom引入依赖

    io.github.saigu log-switch-starter 1.0.0-beta
  • STEP 2: 接入配置中心
    同「方式一」

5.关键配置

SwitchContext是我们的关键配置:

参数名

含义

可选值

status

开关状态

"off": disable, use the application "on": enable, use the configuration level of the configuration center.

List

log level list

If the name of the first LoggerBean in the list is all, it affects the global logger level

Reference example:

{
  "status": "on",
  "loggerBeans": [
    {
      "name": "all",
      "level": "error"
    }
  ]
}
复制代码

6. Precautions

In order to standardize the use of logs and avoid affecting the effect of the downgrade switch, the implementation of the log framework that the project depends on will be detected during initialization.

Note that if the log prompts

"There are multiple log framework implementations, it is recommended to keep only one, otherwise it will affect the effect of the log downgrade switch"

Indicates that multiple logging framework implementations have been detected in the application.

It is recommended to remove redundant logging frameworks from the pom to ensure that a unique logging framework is used.

7. Detailed design TL;DR

This article is still focused on the introduction, and I will talk about the implementation later, so stay tuned.

I've seen it all to the end, originality is not easy, please follow and like it~

Reorganize the knowledge fragments and build a Java knowledge map: github.com/saigu/JavaK… (It is very convenient to check historical articles)

Guess you like

Origin juejin.im/post/7085215463499792414