code学习

arthas生产日志动态级别调整

作者:一马归一码jj
[arthas@96150]$ logger
 name                             ROOT
 class                            ch.qos.logback.classic.Logger
 classLoader                      org.springframework.boot.loader.LaunchedURLClassLoader@1936f0f5
 classLoaderHash                  1936f0f5
 level                            INFO
 effectiveLevel                   INFO
 additivity                       true
 codeSource                       jar:file:/home/tongyu/20221124181413_majie/install/sn/bin/structure-note-server.jar!/BOOT-INF/lib/logback-classic-1.2.4.jar!/
 appenders                        name            console
                                  class           ch.qos.logback.core.ConsoleAppender
                                  classLoader     org.springframework.boot.loader.LaunchedURLClassLoader@1936f0f5
                                  classLoaderHash 1936f0f5
                                  target          System.out
                                  name            dailyRolling
                                  class           ch.qos.logback.core.rolling.RollingFileAppender
                                  classLoader     org.springframework.boot.loader.LaunchedURLClassLoader@1936f0f5
                                  classLoaderHash 1936f0f5
                                  file            /home/tongyu/./.pm2/logs/structure-note-server-20221124-all-0.log
                                  name            errorFile
                                  class           ch.qos.logback.core.rolling.RollingFileAppender
                                  classLoader     org.springframework.boot.loader.LaunchedURLClassLoader@1936f0f5
                                  classLoaderHash 1936f0f5
                                  file            /home/tongyu/./.pm2/logs/structure-note-server-20221124-error-0.log

[arthas@96150]$ logger --name ROOT --level debug
Update logger level fail. Try to specify the classloader with the -c option. Use `sc -d CLASSNAME` to find out the classloader hashcode.
[arthas@96150]$ Update logger level fail. Try to specify the classloader with the -c option. Use `sc -d CLASSNAME` to find out the classloader hashcode.
[arthas@96150]$ logger -c 1936f0f5 --name ROOT --level debug
Update logger level success.
[arthas@96150]$ logger -c 1936f0f5 --name ROOT --level info
Update logger level success.

[arthas@96150]$ logger -n tech.tongyu.bct.structurenote --level debug
Update logger level fail. Try to specify the classloader with the -c option. Use `sc -d CLASSNAME` to find out the classloader hashcode.
[arthas@96150]$ logger -n tech.tongyu.bct.structurenote
 name                             tech.tongyu.bct.structurenote
 class                            ch.qos.logback.classic.Logger
 classLoader                      org.springframework.boot.loader.LaunchedURLClassLoader@1936f0f5
 classLoaderHash                  1936f0f5
 level                            DEBUG
 effectiveLevel                   DEBUG
 additivity                       true
 codeSource                       jar:file:/home/tongyu/20221124181413_majie/install/sn/bin/structure-note-server.jar!/BOOT-INF/lib/logback-classic-1.2.4.jar!/

[arthas@96150]$ logger -c 1936f0f5 -n tech.tongyu.bct.structurenote --level info
Update logger level success.
[arthas@96150]$ logger -c 1936f0f5 -n tech.tongyu.bct.structurenote --level debug
Update logger level success.           

生产日志级别动态调整,目前使用arthas,生产项目使用logback配置日志

1.启动arthas

java -jar arthas-boot.jar

启动成功后,选择自己对应的java进程

2.调整日志级别

1.logger 查看ROOT信息

2. logger --name ROOT --level debug 
调整发现,日志级别并未改变

3.logger -n tech.tongyu.bct.structurenote --level debug
报错提示使用-c hashcode

4.logger -n tech.tongyu.bct.structurenote
获取hashcode

5.logger -c 1936f0f5 -n tech.tongyu.bct.structurenote --level info
发现debug日志不打印
exit退出后,level还是info

6.logger -c 1936f0f5 -n tech.tongyu.bct.structurenote --level debug
重新指定日志级别,日志级别重回deug,正常打印debug日志
生产定位问题后记得日志级别复原