-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arthas实践--使用redefine排查应用奇怪的日志来源 #263
Labels
Comments
很6 |
666 |
6666666 |
redefine error! java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the schema (add/remove fields) |
@xujunhua555 redefine不能改field,只能改函数体 |
好的 |
66666666666666666666 |
秀,学到了 |
666 |
没想到居然是直接改jdk源码。。这操作真是太骚了!! |
优秀 |
优秀🐂 |
如果日志用的是占位符,是不是就不可用了 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
背景
随着应用越来越复杂,依赖越来越多,日志系统越来越混乱,有时会出现一些奇怪的日志,比如:
那么怎样排查这些奇怪的日志从哪里打印出来的呢?因为搞不清楚是什么logger打印出来的,所以想定位就比较头疼。
下面介绍用arthas的redefine命令快速定位奇怪日志来源。
修改StringBuilder
首先在java代码里,字符串拼接基本都是通过
StringBuilder
来实现的。比如下面的代码:实际上生成的字节码也是用
StringBuilder
来拼接的:在java的logger系统里,输出日志时通常也是
StringBuilder
来实现的,最终会调用StringBuilder.toString()
,那么我们可以修改StringBuilder
的代码来检测到日志来源。StringBuilder.toString()
的原生实现是:修改为:
增加的逻辑是:当String里包含
No credential found
时打印出当前栈,这样子就可以定位日志输出来源了。编绎修改过的StringBuilder
其实很简单,在IDE里把
StringBuilder
的代码复制一份,然后贴到任意一个工程里,然后编绎即可。也可以直接用javac来编绎:
启动应用,使用Arthas redefine修改过的StringBuilder
启动应用后,在奇怪日志输出之前,先使用arthas attach应用,再redefine StringBuilder:
当执行到输出
[] [] [] No credential found
的logger代码时,会打印当前栈。实际运行结果是:可以看到是
spas.sdk
打印出了[] [] [] No credential found
的日志。总结
The text was updated successfully, but these errors were encountered: