mirror of
https://gitee.com/arthas/arthas.git
synced 2024-12-05 05:37:37 +08:00
88 lines
3.4 KiB
Markdown
88 lines
3.4 KiB
Markdown
|
||
|
||
在这个案例里,动态修改应用的Logger Level。
|
||
|
||
|
||
### 查找UserController的ClassLoader
|
||
|
||
`sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash`{{execute T2}}
|
||
|
||
```bash
|
||
$ sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash
|
||
classLoaderHash 1be6f5c3
|
||
```
|
||
|
||
注意hashcode是变化的,需要先查看当前的ClassLoader信息,提取对应ClassLoader的hashcode。
|
||
|
||
如果你使用`-c`,你需要手动输入hashcode:`-c <hashcode>`
|
||
|
||
```bash
|
||
$ ognl -c 1be6f5c3 @com.example.demo.arthas.user.UserController@logger
|
||
```
|
||
|
||
对于只有唯一实例的ClassLoader可以通过`--classLoaderClass`指定class name,使用起来更加方便:
|
||
|
||
```bash
|
||
$ ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader @org.springframework.boot.SpringApplication@logger
|
||
@Slf4jLocationAwareLog[
|
||
FQCN=@String[org.apache.commons.logging.LogAdapter$Slf4jLocationAwareLog],
|
||
name=@String[org.springframework.boot.SpringApplication],
|
||
logger=@Logger[Logger[org.springframework.boot.SpringApplication]],
|
||
]
|
||
```
|
||
|
||
`--classLoaderClass` 的值是ClassLoader的类名,只有匹配到唯一的ClassLoader实例时才能工作,目的是方便输入通用命令,而`-c <hashcode>`是动态变化的。
|
||
|
||
### 用ognl获取logger
|
||
|
||
`ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader '@com.example.demo.arthas.user.UserController@logger'`{{execute T2}}
|
||
|
||
```bash
|
||
$ ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader '@com.example.demo.arthas.user.UserController@logger'
|
||
@Logger[
|
||
serialVersionUID=@Long[5454405123156820674],
|
||
FQCN=@String[ch.qos.logback.classic.Logger],
|
||
name=@String[com.example.demo.arthas.user.UserController],
|
||
level=null,
|
||
effectiveLevelInt=@Integer[20000],
|
||
parent=@Logger[Logger[com.example.demo.arthas.user]],
|
||
childrenList=null,
|
||
aai=null,
|
||
additive=@Boolean[true],
|
||
loggerContext=@LoggerContext[ch.qos.logback.classic.LoggerContext[default]],
|
||
]
|
||
```
|
||
|
||
可以知道`UserController@logger`实际使用的是logback。可以看到`level=null`,则说明实际最终的level是从`root` logger里来的。
|
||
|
||
### 单独设置UserController的logger level
|
||
|
||
`ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader '@com.example.demo.arthas.user.UserController@logger.setLevel(@ch.qos.logback.classic.Level@DEBUG)'`{{execute T2}}
|
||
|
||
再次获取`UserController@logger`,可以发现已经是`DEBUG`了:
|
||
|
||
`ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader '@com.example.demo.arthas.user.UserController@logger'`{{execute T2}}
|
||
|
||
```bash
|
||
$ ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader '@com.example.demo.arthas.user.UserController@logger'
|
||
@Logger[
|
||
serialVersionUID=@Long[5454405123156820674],
|
||
FQCN=@String[ch.qos.logback.classic.Logger],
|
||
name=@String[com.example.demo.arthas.user.UserController],
|
||
level=@Level[DEBUG],
|
||
effectiveLevelInt=@Integer[10000],
|
||
parent=@Logger[Logger[com.example.demo.arthas.user]],
|
||
childrenList=null,
|
||
aai=null,
|
||
additive=@Boolean[true],
|
||
loggerContext=@LoggerContext[ch.qos.logback.classic.LoggerContext[default]],
|
||
]
|
||
```
|
||
|
||
### 修改logback的全局logger level
|
||
|
||
通过获取`root` logger,可以修改全局的logger level:
|
||
|
||
`ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader '@org.slf4j.LoggerFactory@getLogger("root").setLevel(@ch.qos.logback.classic.Level@DEBUG)'`{{execute T2}}
|
||
|