logback 日志级别动态切换的终极方案 (Java ASM 使用)- 今日头条

本文由 简悦 SimpRead 转码, 原文地址 www.toutiao.com

一切皆有因果,所有事情,都有事件驱动。本方案的日志级别切换是由这样的背景下产生的: 单个生产环境上,有几百近千个微服务。

一切皆有因果,所有事情,都有事件驱动。本方案的日志级别切换是由这样的背景下产生的:

  • 单个生产环境上,有几百近千个微服务
  • 日志级别切换不重启服务,要求即时生效果
  • 由业务开发人员去修改代码或增加相关依赖配置等涉及面广,推动进度慢
  • 后期动态实时过滤垃圾日志,减少 io 和磁盘空间成本

Top

在跟敌人发起战争之前,只有先发解敌方的情况,才能做到百战百胜。要想对 logback 的日志级别做动态切换,首先至少对 logback 做个初步的了解、和看看它有没有提供现成的实现方案。下面简单介绍一下 logback 跟这次需求有关的内容。

logback 是 java 的日志开源组件,是 log4j 创始人写的,目前主要分为 3 个模块

  1. logback-core: 核心代码模块
  2. logback-classic:log4j 的一个改良版本,同时实现了 slf4j 的接口
  3. logback-access: 访问模块与 Servlet 容器集成提供通过 Http 来访问日志的功能
  4. ContextInitializer 类是 logback 自动配置流程的逻辑实现
  5. 日志级别由 Logger 维护和使用。其成员变量 Level 正是由 Logger 维护
  6. Logger 中有 filterAndLog_0_Or3Plus、filterAndLog_1、filterAndLog_2 三个不同参数的过滤日志输出方法
  7. Logger 中的 setLevel 就是对日志级别的维护

https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/774285c2103f4cb89d7d76884c09b4ea?from=pc

Top

在满头苦干之前,先了解市面上的方案。是设计师们乃至产品大佬们寻求最优解决方案的思路。

这个方案是 logback 自带现成的实现,只要开启配置就可以实现所谓的日志级别动态切换。配置方法:在 logback 的配置文件中,增加定时扫描器即可,如:

1
<configuration scan="true" scanPeriod="30 seconds" debug="false">

该方案可以不需要研发成本,运维人员自己配上并能使用。

它的缺点是:

  • 每次调整扫描间隔时间都要重启服务
  • 90% 以上的扫描都是无用功,因为生产上的日志级别不可能经常有切换需求,也不允许这么做
  • 生效不实时,如果设定在一分钟或几分钟扫描一次,那么让日志级别调整后生效就不是即时生效的,不过这个可以忽略
  • 该方案满足不了我们的垃圾日志丢弃的需求,比如根据某些关键字丢弃日志的输出。针对这种历史原因打印很多垃圾日志的情况,考虑到时间成本,不可能让业务研发去优化。

当然,还有其它方案,如:自己定义接口 api。来直接调用 Logger 中的 setLevel 方法,达到调整级别的目的;springboot 的集成。

这些方案都不避免不了专主于业务开发角色的参与。

通过 asm 动态修改指令,该方案除了能满足调整日志级别即时生效之外。还可以满足过滤日志的需求

具体实现如下,在这里就不对 asm 做介绍了,不了解的同学,需要先去熟悉 asm、java agent 和 jvm 的指令:

一、idea 创建 maven 工程

https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/774285c2103f4cb89d7d76884c09b4ea?from=pc

二、maven 引入依赖

https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/ac6db822b4264930b945c02074411d62?from=pc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<dependencies>
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>7.1</version>
        </dependency>
        <dependency>
            <artifactId>asm-commons</artifactId>
            <groupId>org.ow2.asm</groupId>
            <version>7.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/tools.jar</systemPath>
        </dependency>
    </dependencies>

<build>
  <plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.2.0</version>
          <configuration>
              <archive>
                  <manifestEntries>
                      <!-- 主程序启动类 -->
                      <Agent-Class>
                          agent.LogbackAgentMain
                      </Agent-Class>
                      <!-- 允许重新定义类 -->
                      <Can-Redefine-Classes>true</Can-Redefine-Classes>
                      <!-- 允许转换并重新加载类 -->
                      <Can-Retransform-Classes>true</Can-Retransform-Classes>
                  </manifestEntries>
              </archive>
          </configuration>
      </plugin>
      <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <encoding>UTF-8</encoding>
              <compilerArguments>
                  <verbose />
                  <!-- 将jdk的依赖jar打入项目中-->
                  <bootclasspath>${java.home}/lib/rt.jar</bootclasspath>
              </compilerArguments>
          </configuration>
      </plugin>
  </plugins>
</build>

三、编写 attrach 启动类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package agent;

import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;

/**
 * @author dengbp
 * @ClassName LogbackAgentMain
 * @Description attach 启动器
 * @date 3/25/22 6:27 PM
 */
public class LogbackAgentMain {

    private static String FILTER_CLASS = "ch.qos.logback.classic.Logger";

    public static void agentmain(String agentArgs, Instrumentation inst) throws UnmodifiableClassException {
        System.out.println("agentArgs:" + agentArgs);
        inst.addTransformer(new LogBackFileTransformer(agentArgs), true);
        Class[] classes = inst.getAllLoadedClasses();
        for (int i = 0; i < classes.length; i++) {
            if (FILTER_CLASS.equals(classes[i].getName())) {
                System.out.println("----重新加载Logger开始----");
                inst.retransformClasses(classes[i]);
                System.out.println("----重新加载Logger完毕----");
                break;
            }
        }
    }
}

四、实现字节码转换处理器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package agent;


import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.ClassWriter;
import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;

/**
 * @author dengbp
 * @ClassName LogBackFileTransformer
 * @Description 字节码文件转换器
 * @date 3/25/22 6:25 PM
 */
public class LogBackFileTransformer implements ClassFileTransformer {

    private final String level;
    private static String CLASS_NAME = "ch/qos/logback/classic/Logger";


    public LogBackFileTransformer(String level) {
        this.level = level;
    }

    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
        if (!CLASS_NAME.equals(className)) {
            return classfileBuffer;
        }
        ClassReader cr = new ClassReader(classfileBuffer);
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);
        ClassVisitor cv1 = new LogBackClassVisitor(cw, level);
        /*ClassVisitor cv2 = new LogBackClassVisitor(cv1);*/
        // asm框架使用到访问模式和责任链模式
        // ClassReader 只需要 accept 责任链中的头节点处的 ClassVisitor即可
        cr.accept(cv1, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG);
        System.out.println("end...");
        return cw.toByteArray();
    }
}

五、实现 Logger 元素的访问者

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package agent;

import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * @author dengbp
 * @ClassName LogBackClassVisitor
 * @Description Logger类元素访问者
 * @date 3/25/22 5:01 PM
 */
public class LogBackClassVisitor extends ClassVisitor {
    private final String level;
    /**
     * asm版本
     */
    private static final int ASM_VERSION = Opcodes.ASM4;

    public LogBackClassVisitor(ClassVisitor classVisitor, String level) {
        super(ASM_VERSION, classVisitor);
        this.level = level;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
                                     String[] exceptions) {
        MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
        return new LogFilterMethodVisitor(api, mv, access, name, descriptor, level);
    }
}

六、最后实现 Logger 关键方法的访问者

该访问者 (类),实现日志级别的切换,需要对 Logger 的三个日志过滤方法进行指令的修改。原理是把命令行入参的日志级别参数值覆盖其成员变量 effectiveLevelInt 的值,由于篇幅过大,只贴核心部分代码,请看下面:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package agent;

import jdk.internal.org.objectweb.asm.Label;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.commons.AdviceAdapter;
import org.objectweb.asm.Opcodes;

/**
 * @author dengbp
 * @ClassName LogFilterMethodVisitor
 * @Description Logger类日志过滤方法元素访问者
 * @date 3/25/22 5:01 PM
 */
public class LogFilterMethodVisitor extends AdviceAdapter {

    private String methodName;
    private final String level;
    private static final String filterAndLog_1 = "filterAndLog_1";
    private static final String filterAndLog_2 = "filterAndLog_2";
    private static final String filterAndLog_0_Or3Plus = "filterAndLog_0_Or3Plus";

    protected LogFilterMethodVisitor(int api, MethodVisitor methodVisitor, int access, String name, String descriptor, String level) {
        super(api, methodVisitor, access, name, descriptor);
        this.methodName = name;
        this.level = level;
    }

    /**
     * Description 在访问方法的头部时被访问
     * @param
     * @return void
     * @Author dengbp
     * @Date 3:36 PM 4/1/22
     **/

    @Override
    public void visitCode() {
        System.out.println("visitCode method");
        super.visitCode();
    }

    @Override
    protected void onMethodEnter() {
        System.out.println("开始重写日志级别为:"+level);
        System.out.println("----准备修改方法----");
        if (filterAndLog_1.equals(methodName)) {
            modifyLogLevel_1();
        }
        if (filterAndLog_2.equals(methodName)) {
            modifyLogLevel_2();
        }
        if (filterAndLog_0_Or3Plus.equals(methodName)) {
            modifyLogLevel_3();
        }
        System.out.println("重写日志级别成功....");
    }

其中 modifyLogLevel_1(); modifyLogLevel_2();modifyLogLevel_3(); 分别对应 filterAndLog_1、filterAndLog_2、filterAndLog_0_Or3Plus 方法指令的修改。下面只贴 modifyLogLevel_1 的实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
     * Description 修改目标方法:filterAndLog_1
     * @param
     * @return void
     * @Author dengbp
     * @Date 2:20 PM 3/31/22
     **/

    private void modifyLogLevel_1(){
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(390, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitLdcInsn(level);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "ch/qos/logback/classic/Level", "toLevel", "(Ljava/lang/String;)Lch/qos/logback/classic/Level;", false);
        mv.visitFieldInsn(Opcodes.GETFIELD, "ch/qos/logback/classic/Level", "levelInt", "I");
        mv.visitFieldInsn(Opcodes.PUTFIELD, "ch/qos/logback/classic/Logger", "effectiveLevelInt", "I");
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(392, l1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, "ch/qos/logback/classic/Logger", "loggerContext", "Lch/qos/logback/classic/LoggerContext;");
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 3);
        mv.visitVarInsn(Opcodes.ALOAD, 4);
        mv.visitVarInsn(Opcodes.ALOAD, 5);
        mv.visitVarInsn(Opcodes.ALOAD, 6);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "ch/qos/logback/classic/LoggerContext", "getTurboFilterChainDecision_1", "(Lorg/slf4j/Marker;Lch/qos/logback/classic/Logger;Lch/qos/logback/classic/Level;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Throwable;)Lch/qos/logback/core/spi/FilterReply;", false);
        mv.visitVarInsn(Opcodes.ASTORE, 7);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLineNumber(394, l2);
        mv.visitVarInsn(Opcodes.ALOAD, 7);
        mv.visitFieldInsn(Opcodes.GETSTATIC, "ch/qos/logback/core/spi/FilterReply", "NEUTRAL", "Lch/qos/logback/core/spi/FilterReply;");
        Label l3 = new Label();
        mv.visitJumpInsn(Opcodes.IF_ACMPNE, l3);
        Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLineNumber(395, l4);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, "ch/qos/logback/classic/Logger", "effectiveLevelInt", "I");
        mv.visitVarInsn(Opcodes.ALOAD, 3);
        mv.visitFieldInsn(Opcodes.GETFIELD, "ch/qos/logback/classic/Level", "levelInt", "I");
        Label l5 = new Label();
        mv.visitJumpInsn(Opcodes.IF_ICMPLE, l5);
        Label l6 = new Label();
        mv.visitLabel(l6);
        mv.visitLineNumber(396, l6);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitLabel(l3);
        mv.visitLineNumber(398, l3);
        mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]{"ch/qos/logback/core/spi/FilterReply"}, 0, null);
        mv.visitVarInsn(Opcodes.ALOAD, 7);
        mv.visitFieldInsn(Opcodes.GETSTATIC, "ch/qos/logback/core/spi/FilterReply", "DENY", "Lch/qos/logback/core/spi/FilterReply;");
        mv.visitJumpInsn(Opcodes.IF_ACMPNE, l5);
        Label l7 = new Label();
        mv.visitLabel(l7);
        mv.visitLineNumber(399, l7);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitLabel(l5);
        mv.visitLineNumber(402, l5);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitVarInsn(Opcodes.ALOAD, 3);
        mv.visitVarInsn(Opcodes.ALOAD, 4);
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
        mv.visitInsn(Opcodes.DUP);
        mv.visitInsn(Opcodes.ICONST_0);
        mv.visitVarInsn(Opcodes.ALOAD, 5);
        mv.visitInsn(Opcodes.AASTORE);
        mv.visitVarInsn(Opcodes.ALOAD, 6);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "ch/qos/logback/classic/Logger", "buildLoggingEventAndAppend", "(Ljava/lang/String;Lorg/slf4j/Marker;Lch/qos/logback/classic/Level;Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V", false);
        Label l8 = new Label();
        mv.visitLabel(l8);
        mv.visitLineNumber(403, l8);
        mv.visitInsn(Opcodes.RETURN);
        Label l9 = new Label();
        mv.visitLabel(l9);
        mv.visitLocalVariable("this", "Lch/qos/logback/classic/Logger;", null, l0, l9, 0);
        mv.visitLocalVariable("localFQCN", "Ljava/lang/String;", null, l0, l9, 1);
        mv.visitLocalVariable("marker", "Lorg/slf4j/Marker;", null, l0, l9, 2);
        mv.visitLocalVariable("level", "Lch/qos/logback/classic/Level;", null, l0, l9, 3);
        mv.visitLocalVariable("msg", "Ljava/lang/String;", null, l0, l9, 4);
        mv.visitLocalVariable("param", "Ljava/lang/Object;", null, l0, l9, 5);
        mv.visitLocalVariable("t", "Ljava/lang/Throwable;", null, l0, l9, 6);
        mv.visitLocalVariable("decision", "Lch/qos/logback/core/spi/FilterReply;", null, l2, l9, 7);
        mv.visitMaxs(9, 8);
        mv.visitEnd();
    }

七、最后再编写加载 attach Agent 的加载类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import com.sun.tools.attach.VirtualMachine;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author dengbp
 * @ClassName MyAttachMain
 * @Description jar 执行命令:
 * @date 3/25/22 4:12 PM
 */
public class MyAttachMain {
    private static final int ARGS_SIZE = 2;

    public static void main(String[] args) {
        if (args == null || args.length != ARGS_SIZE) {
            System.out.println("请输入进程id和日志级别(ALL、TRACE、DEBUG、INFO、WARN、ERROR、OFF),如:31722 info");
            return;
        }
        VirtualMachine vm = null;
        try {
            System.out.println("修改的进程id:" + args[0]);
            vm = VirtualMachine.attach(args[0]);
            System.out.println("调整日志级别为:" + args[1]);
            vm.loadAgent(getJar(), args[1]);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (vm != null) {
                try {
                    vm.detach();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static String getJar() throws UnsupportedEncodingException {
        String jarFilePath = MyAttachMain.class.getProtectionDomain().getCodeSource().getLocation().getFile();
        jarFilePath = java.net.URLDecoder.decode(jarFilePath, "UTF-8");
        int beginIndex = 0;
        int endIndex = jarFilePath.length();
        if (jarFilePath.contains(".jar")) {
            endIndex = jarFilePath.indexOf(".jar") + 4;
        }
        if (jarFilePath.startsWith("file:")) {
            beginIndex = jarFilePath.indexOf("file:") + 5;
        }

        jarFilePath = jarFilePath.substring(beginIndex, endIndex);
        System.out.println("jar path:" + jarFilePath);
        return jarFilePath;
    }
}

八、打包执行

  • 寻找目标程序

https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/3c596978d0f04b72b090d4512e93ba8f?from=pc

  • 执行 jar
1
java  -Xbootclasspath/a:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/tools.jar  -cp change-log-agent-1.0.1.jar MyAttachMain 52433  DEBUG
1
java  -Xbootclasspath/a:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/tools.jar  -cp change-log-agent-1.0.1.jar MyAttachMain 52433 ERROR
1
java  -Xbootclasspath/a:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/tools.jar  -cp change-log-agent-1.0.1.jar MyAttachMain 52433 INFO
  • 效果

https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/472bafc09a9c4cdca3365cd40e9c6dc7?from=pc

https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/07700325cd734bf5a0943df0c4a857d4?from=pc

PS: 如果出现校验失败 (caused by: java.lang.verifyerror),请配上 jvm 参数:-noverify

Top

通过 attach 探针动态修改指令技术,可以在服务不停的情况下,实现部分代码的热部署; 也可以对代码的增强处理。下一期:代码热部署工具

本文来自 https://www.cnblogs.com/dengbangpang/p/16086813.html