使用arthas在线调整日志级别

2023-10-16 16:33:36 浏览数 (1)

线上服务器出现问题,需要临时调整日志级别,明确定位问题。传统的方法是修改日志配置文件logback-spring.xml,然后重启,这样比较慢,而且也可能漏掉因为重启,导致问题无法立马复现。 使用arthas可以不影响服务运行的情况下,动态调整日志级别

日志命令用法

  • 常用用法
代码语言:javascript复制
logger [-c <value>] [--classLoaderClass <value>] [-h] [--include-no-appender] [-l <value>] [-n <value>]
-c 类加载器hashcode
--classLoaderClass 类加载器
-l 设置的日志级别,比如debug、info
-n 日志名字,默认是ROOT
  • 帮助命令
代码语言:javascript复制
[arthas@5345]$ logger -h
 USAGE:
   logger [-c <value>] [--classLoaderClass <value>] [-h] [--include-no-appender] [-l <value>] [-n <value>]

 SUMMARY:
   Print logger info, and update the logger level

 Examples:
   logger
   logger -c 327a647b
   logger -c 327a647b --name ROOT --level debug
   logger --include-no-appender

 WIKI:
   https://arthas.aliyun.com/doc/logger

 OPTIONS:
 -c, --classloader <value>                               classLoader hashcode, if no value is set, default value is SystemClassLoader
     --classLoaderClass <value>                          The class name of the special class's classLoader.
 -h, --help                                              this help
     --include-no-appender                               include the loggers which don't have appenders, default value false
 -l, --level <value>                                     set logger level
 -n, --name <value>                                      logger name

获取日志信息

代码语言:javascript复制
[arthas@5345]$ logger info -n ROOT
 name                        ROOT
 class                       ch.qos.logback.classic.Logger
 classLoader                 org.springframework.boot.loader.LaunchedURLClassLoader@433c675d
 classLoaderHash             433c675d
 level                       DEBUG
 effectiveLevel              DEBUG
 additivity                  true
 codeSource                  jar:file:/data/worker/student-center/student-center.jar!/BOOT-INF/lib/logback-classic-1.2.11.jar!/
 appenders                   name            WARN
                             class           ch.qos.logback.core.rolling.RollingFileAppender
                             classLoader     org.springframework.boot.loader.LaunchedURLClassLoader@433c675d
                             classLoaderHash 433c675d
                             file            logs/warn.log
                             name            ALL
                             class           ch.qos.logback.core.rolling.RollingFileAppender
                             classLoader     org.springframework.boot.loader.LaunchedURLClassLoader@433c675d
                             classLoaderHash 433c675d
                             file            logs/all.log
                             name            WEWORK_ROBOT
                             class           com.musk.ums.warning.WeworkRobotLogbackAppender
                             classLoader     org.springframework.boot.loader.LaunchedURLClassLoader@433c675d
                             classLoaderHash 433c675d
  • name: 日志名字
  • classLoaderHash: 类加载器hashcode,后面会用到
  • level: 当前的日志级别
  • effectiveLevel: 有效的日志级别,因为日志级别会受到层级继承影响,可能与level不一样

设置日志级别

代码语言:javascript复制
# -c 433c675d是类加载器的hashcode,从logger info的输出结果中可以找到
[arthas@5345]$ logger -c 433c675d --name ROOT --level INFO
Update logger level success.

0 人点赞