mirror of
https://gitee.com/arthas/arthas.git
synced 2024-12-04 13:17:59 +08:00
208 lines
10 KiB
Markdown
208 lines
10 KiB
Markdown
# logger
|
||
|
||
[`logger`在线教程](https://arthas.aliyun.com/doc/arthas-tutorials?language=cn&id=command-logger)
|
||
|
||
::: tip
|
||
查看 logger 信息,更新 logger level
|
||
:::
|
||
|
||
### 使用参考
|
||
|
||
#### 查看所有 logger 信息
|
||
|
||
以下面的`logback.xml`为例:
|
||
|
||
```xml
|
||
<?xml version="1.0" encoding="UTF-8"?>
|
||
<configuration>
|
||
<appender name="APPLICATION" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||
<file>app.log</file>
|
||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||
<fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
|
||
<maxFileSize>100MB</maxFileSize>
|
||
<maxHistory>60</maxHistory>
|
||
<totalSizeCap>2GB</totalSizeCap>
|
||
</rollingPolicy>
|
||
<encoder>
|
||
<pattern>%logger{35} - %msg%n</pattern>
|
||
</encoder>
|
||
</appender>
|
||
|
||
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
|
||
<appender-ref ref="APPLICATION" />
|
||
</appender>
|
||
|
||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||
<encoder>
|
||
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n
|
||
</pattern>
|
||
<charset>utf8</charset>
|
||
</encoder>
|
||
</appender>
|
||
|
||
<root level="INFO">
|
||
<appender-ref ref="CONSOLE" />
|
||
<appender-ref ref="ASYNC" />
|
||
</root>
|
||
</configuration>
|
||
```
|
||
|
||
使用`logger`命令打印的结果是:
|
||
|
||
```bash
|
||
[arthas@2062]$ logger
|
||
name ROOT
|
||
class ch.qos.logback.classic.Logger
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
level INFO
|
||
effectiveLevel INFO
|
||
additivity true
|
||
codeSource file:/Users/hengyunabc/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar
|
||
appenders name CONSOLE
|
||
class ch.qos.logback.core.ConsoleAppender
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
target System.out
|
||
name APPLICATION
|
||
class ch.qos.logback.core.rolling.RollingFileAppender
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
file app.log
|
||
name ASYNC
|
||
class ch.qos.logback.classic.AsyncAppender
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
appenderRef [APPLICATION]
|
||
```
|
||
|
||
从`appenders`的信息里,可以看到
|
||
|
||
- `CONSOLE` logger 的 target 是`System.out`
|
||
- `APPLICATION` logger 是`RollingFileAppender`,它的 file 是`app.log`
|
||
- `ASYNC`它的`appenderRef`是`APPLICATION`,即异步输出到文件里
|
||
|
||
#### 查看指定名字的 logger 信息
|
||
|
||
```bash
|
||
[arthas@2062]$ logger -n org.springframework.web
|
||
name org.springframework.web
|
||
class ch.qos.logback.classic.Logger
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
level null
|
||
effectiveLevel INFO
|
||
additivity true
|
||
codeSource file:/Users/hengyunabc/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar
|
||
```
|
||
|
||
#### 查看指定 classloader 的 logger 信息
|
||
|
||
注意 hashcode 是变化的,需要先查看当前的 ClassLoader 信息,提取对应 ClassLoader 的 hashcode。
|
||
|
||
如果你使用`-c`,你需要手动输入 hashcode:`-c <hashcode>`
|
||
|
||
```bash
|
||
[arthas@2062]$ logger -c 2a139a55
|
||
name ROOT
|
||
class ch.qos.logback.classic.Logger
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
level DEBUG
|
||
effectiveLevel DEBUG
|
||
additivity true
|
||
codeSource file:/Users/hengyunabc/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar
|
||
appenders name CONSOLE
|
||
class ch.qos.logback.core.ConsoleAppender
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
target System.out
|
||
name APPLICATION
|
||
class ch.qos.logback.core.rolling.RollingFileAppender
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
file app.log
|
||
name ASYNC
|
||
class ch.qos.logback.classic.AsyncAppender
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
appenderRef [APPLICATION]
|
||
```
|
||
|
||
对于只有唯一实例的 ClassLoader 可以通过`--classLoaderClass`指定 class name,使用起来更加方便:
|
||
|
||
`logger --classLoaderClass sun.misc.Launcher$AppClassLoader`
|
||
|
||
- 注: 这里 classLoaderClass 在 java 8 是 sun.misc.Launcher$AppClassLoader,而java 11的classloader是jdk.internal.loader.ClassLoaders$AppClassLoader。
|
||
|
||
`--classLoaderClass` 的值是 ClassLoader 的类名,只有匹配到唯一的 ClassLoader 实例时才能工作,目的是方便输入通用命令,而`-c <hashcode>`是动态变化的。
|
||
|
||
#### 更新 logger level
|
||
|
||
```bash
|
||
[arthas@2062]$ logger --name ROOT --level debug
|
||
update logger level success.
|
||
```
|
||
|
||
#### 指定 classloader 更新 logger level
|
||
|
||
默认情况下,logger 命令会在 SystemClassloader 下执行,如果应用是传统的`war`应用,或者 spring boot fat jar 启动的应用,那么需要指定 classloader。
|
||
|
||
可以先用 `sc -d yourClassName` 来查看具体的 classloader hashcode,然后在更新 level 时指定 classloader:
|
||
|
||
```bash
|
||
[arthas@2062]$ logger -c 2a139a55 --name ROOT --level debug
|
||
```
|
||
|
||
#### 查看没有 appender 的 logger 的信息
|
||
|
||
默认情况下,`logger`命令只打印有 appender 的 logger 的信息。如果想查看没有`appender`的 logger 的信息,可以加上参数`--include-no-appender`。
|
||
|
||
注意,通常输出结果会很长。
|
||
|
||
```bash
|
||
[arthas@2062]$ logger --include-no-appender
|
||
name ROOT
|
||
class ch.qos.logback.classic.Logger
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
level DEBUG
|
||
effectiveLevel DEBUG
|
||
additivity true
|
||
codeSource file:/Users/hengyunabc/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar
|
||
appenders name CONSOLE
|
||
class ch.qos.logback.core.ConsoleAppender
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
target System.out
|
||
name APPLICATION
|
||
class ch.qos.logback.core.rolling.RollingFileAppender
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
file app.log
|
||
name ASYNC
|
||
class ch.qos.logback.classic.AsyncAppender
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
appenderRef [APPLICATION]
|
||
|
||
name com
|
||
class ch.qos.logback.classic.Logger
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
level null
|
||
effectiveLevel DEBUG
|
||
additivity true
|
||
codeSource file:/Users/hengyunabc/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar
|
||
|
||
name com.alibaba
|
||
class ch.qos.logback.classic.Logger
|
||
classLoader sun.misc.Launcher$AppClassLoader@2a139a55
|
||
classLoaderHash 2a139a55
|
||
level null
|
||
effectiveLevel DEBUG
|
||
additivity true
|
||
codeSource file:/Users/hengyunabc/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar
|
||
...
|
||
```
|