经过Arthas来动态打印日志

需求
线上一个Spring boot应用 经过MyBatis来操做数据库 定位一个线上问题 想动态打印sql日志html

解决
经过Arthas工具来实现上述的需求java

$ ognl '@org.slf4j.LoggerFactory@getLogger("com.foo.cpts.mapper")'
@Logger[
    serialVersionUID=@Long[5454405123156820674],
    FQCN=@String[ch.qos.logback.classic.Logger],
    name=@String[com.foo.cpts.mapper],
    level=null,
    effectiveLevelInt=@Integer[20000],
    parent=@Logger[Logger[com.foo.cpts]],
    childrenList=@CopyOnWriteArrayList[isEmpty=false;size=2],
    aai=null,
    additive=@Boolean[true],
    loggerContext=@LoggerContext[ch.qos.logback.classic.LoggerContext[default]],
]

刚开始Mapper包对应的日志级别是null 将其改为DEBUGgit

$ ognl '@org.slf4j.LoggerFactory@getLogger("com.foo.cpts.mapper").setLevel(@ch.qos.logback.classic.Level@DEBUG)'
null

$ ognl '@org.slf4j.LoggerFactory@getLogger("com.foo.cpts.mapper")'
@Logger[
    serialVersionUID=@Long[5454405123156820674],
    FQCN=@String[ch.qos.logback.classic.Logger],
    name=@String[com.foo.cpts.mapper],
    level=@Level[DEBUG],
    effectiveLevelInt=@Integer[10000],
    parent=@Logger[Logger[com.foo.cpts]],
    childrenList=@CopyOnWriteArrayList[isEmpty=false;size=2],
    aai=null,
    additive=@Boolean[true],
    loggerContext=@LoggerContext[ch.qos.logback.classic.LoggerContext[default]],
]

此时当调用Mapper方法时 能打印出sql日志了 一样关闭sql日志 改为info便可github

$ ognl '@org.slf4j.LoggerFactory@getLogger("com.foo.cpts.mapper").setLevel(@ch.qos.logback.classic.Level@INFO)'
null

补充
由于打印sql日志对应的配置是sql

logging.level.com.foo.cpts.mapper=DEBUG

刚开始想着经过下面的方式来实现数据库

ognl '@java.lang.System@setProperty("logging.level.com.foo.cpts.mapper","DEBUG")'

可是该方式并未起做用app

相关文章
相关标签/搜索