java运行时跟踪库替换system.out.println

发布于 2024-10-09 19:35:06 字数 435 浏览 0 评论 0原文

您听说过任何库可以让我在运行时设置对特定方法的跟踪吗?

而不是在我的文件中添加(和删除)大量 System.out.println代码(并且必须重新编译和重新部署)我想要一个神奇的东西,它可以为所选方法的每次调用打印出一行,而不需要对代码进行任何更改。这无需重新编译即可工作,因此需要某种 JVM 代理(或需要某种非标准 JVM?)。听起来像是切面编程的工作?

典型的场景是启动应用程序,动态配置跟踪的方法(在单独的文件或类似文件中),然后每次调用选定的方法时,都会将其名称(和参数)打印到 System.out (或一些日志文件)。

当然,人们可以想到数十个附加功能,但这个基本集将是一个很棒的工具。顺便说一句,我也使用 Eclipse 交互式调试器,不仅仅是 System.out 跟踪技术,但两者都有一些优点,有时 Eclipse 还不够。

Have you heard of any library which would allow me to set up tracing for specific methods at runtime?

Instead of adding (and removing) lots of System.out.println in my code (and having to re-compile and re-deploy) I would like to have a magic thing which would print out a line for each call of selected method without any change in the code. This would work without re-compiling, so some kind of JVM agent (or some non-standard JVM would be needed?). Sounds like a job for aspect programming?

A typical scenario would be to start an application, configure the traced methods dynamically (in a separate file or similar) and then everytime a selected method is called a line with its name (and arguments) is printed out to System.out (or some log file).

Naturally one could think of tens of additional features, but this basic set would be a great tool. BTW, I use Eclipse interactive debugger too, not only the System.out tracing technique, but both have some advantages and sometimes Eclipse is not enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

痴梦一场 2024-10-16 19:35:06

是的,您所指的是所谓的面向方面编程。为 Java 提供此功能的典型库是 AspectJ。您定义所谓的切入点,本质上是类和方法名称的正则表达式,包括通配符,以及在每个切入点执行的代码(称为建议)。这对于日志记录、安全检查和类似的横切问题很有用。

您可以通过配置打开和关闭切入点建议。您可以在方法调用之前、方法返回之后甚至抛出异常之后执行通知。参数也是可用的。

为此,需要一个aspectj java 代理。

Yes what you are referring to is known as Aspect oriented programming. A typical library providing this for Java is AspectJ. You define what are called pointcuts, essentially regular expressions for classes and method names, including wildcards, and the code to execute at each pointcut, known as an advice. This is useful for logging and also security checks and similar cross cutting concerns.

You can turn pointcut advices on and off through configuration. You can have an advice execute before a method call, after it returns or even after it throws an exception. Arguments are also available.

An aspectj java agent is needed for this to work.

西瑶 2024-10-16 19:35:06

根据我的经验,这种非常详细的跟踪(比通常用于日志记录的跟踪更详细)作为一种调试技术表明单元测试和集成测试不足。

In my experience, that kind of very detailed tracing (much more detailed than one would normally use for logging) as a debugging technique is indicative of insufficient unit testing and integration testing.

错々过的事 2024-10-16 19:35:06

您可以使用名为 InTrace 的工具来执行此操作。

注意:InTrace 是我编写的一个免费开源工具。

You can do this using a tool called InTrace.

NOTE: InTrace is a free and open source tool which I have written.

迷途知返 2024-10-16 19:35:06

Log4J 对于根据“日志级别”(DEBUG、INFO、WARN、FATAL)禁用日志记录很有用。

您在配置文件中指定您希望在日志中出现的最低级别,例如,不要记录低于 INFO 级别的任何内容,然后瞧!

Log4J useful for disabling logging depending on "log-level" (DEBUG, INFO, WARN, FATAL).

You specify in configuration file what the least level you want to appear in logs, e.g., don't log anything below INFO level, and voila!

心碎无痕… 2024-10-16 19:35:06

看起来还有另一个解决方案 - 称为 Byteman。用他们自己的话说:

Byteman是一个简化Java跟踪和测试的工具
程序。 Byteman 允许您将额外的 Java 代码插入到您的
应用程序,无论是在 JVM 启动期间还是在启动之后加载
它已经开始运行了。注入的代码允许访问
您的任何数据并调用任何应用程序方法,包括在哪里
他们是私人的。您几乎可以在任何您想要的地方注入代码
无需提前准备原始源代码,也无需
您必须重新编译、重新打包或重新部署您的应用程序。实际上
您可以删除注入的代码并重新安装不同的代码
应用程序继续执行。

Looks like there's yet another solution - called Byteman. In their own words:

Byteman is a tool which simplifies tracing and testing of Java
programs. Byteman allows you to insert extra Java code into your
application, either as it is loaded during JVM startup or even after
it has already started running. The injected code is allowed to access
any of your data and call any application methods, including where
they are private. You can inject code almost anywhere you want and
there is no need to prepare the original source code in advance nor do
you have to recompile, repackage or redeploy your application. In fact
you can remove injected code and reinstall different code while the
application continues to execute.

誰ツ都不明白 2024-10-16 19:35:06

Jackplay 就是您正在寻找的工具。

它允许您启用方法入口和出口点的日志记录,而无需任何编码或重新部署。

它还允许重新定义方法体。它为您提供基于 Web 的 UI 作为控制面板,以启用或撤消对您的 class.methods 的跟踪。

Jackplay is the tool you are looking for.

It allows you to enable logging on method entry and exit points without any coding or redeployment.

It also allows redefining a method body. It gives you web based UI as control panel to enable or undo tracing on your class.methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文