Java如何使用arthas修改日志级别

发布时间:2022-06-17 11:55:10 作者:iii
来源:亿速云 阅读:276

这篇文章主要讲解了“Java如何使用arthas修改日志级别”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java如何使用arthas修改日志级别”吧!

arthas能够更改日志的级别,下面是logger的用法

查看logger信息,更新logger level

查看所有logger信息 logger

以下面的logback.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>

sc -d *FormulaEngine |grepclassLoaderHash

sc -d FormulaEngine.class*

使用logger命令打印的结果是:

[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的信息里,可以看到

CONSOLElogger的target是System.out

APPLICATIONlogger是RollingFileAppender,它的file是app.log

ASYNC它的appenderRefAPPLICATION,即异步输出到文件里

Java如何使用arthas修改日志级别

查看指定名字的logger信息 logger -n org.springframework.web

[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>

[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 找到需要修改日志级别的类

命令:

sc -d FreeResourceQueryActionImpl.class*

或者

sc -d *FreeResourceQueryActionImpl | grep classLoaderHash

结果:目的就是为了找出classLoaderHash

修改

[arthas@2062]$ logger --name ROOT --level debugupdate logger level success.

Java如何使用arthas修改日志级别

Java如何使用arthas修改日志级别指定classloader更新 logger level

默认情况下,logger命令会在SystemClassloader下执行,如果应用是传统的war应用,或者spring boot fat jar启动的应用,那么需要指定classloader。

可以先用sc -d yourClassName来查看具体的 classloader hashcode,然后在更新level时指定classloader:

[arthas@2062]$ logger -c 2a139a55 --name ROOT --level debug

查看没有appender的logger的信息

默认情况下,logger命令只打印有appender的logger的信息。如果想查看没有appender的logger的信息,可以加上参数--include-no-appender

注意,通常输出结果会很长。

[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...

退出arthas

为了不影响性能,解决完问题需要退出arthas

如果只是退出当前的连接,可以用quit或者exit命令。Attach到目标进程上的arthas还会继续运行,端口会保持开放,下次连接时可以直接连接上。

如果想完全退出arthas,可以执行stop命令。

退出之前恢复原来的日志级别

感谢各位的阅读,以上就是“Java如何使用arthas修改日志级别”的内容了,经过本文的学习后,相信大家对Java如何使用arthas修改日志级别这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. tomcat日志级别
  2. 修改Oracle的Job Scheduler 日志级别及删除运行日志

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java arthas

上一篇:Android使用NestedScrollView内嵌RecycleView滑动冲突问题如何解决

下一篇:crontab是不是linux自带的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》