如何在java中检索有用的系统信息?
在 Java 应用程序中,哪些系统信息是有用的(尤其是在跟踪异常或其他问题时)?
我正在考虑有关异常、java/os 信息、内存/对象消耗、io 信息、环境/enchodings 等的详细信息。
Which system information are useful - especially when tracing an exception or other problems down - in a java application?
I am thinking about details about exceptions, java/os information, memory/object consumptions, io information, environment/enchodings etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除了明显的异常堆栈跟踪之外,您可以获得的信息越多越好。因此,您应该获取所有系统属性以及环境变量。另外,如果您的应用程序有一些设置,请获取它们的所有值。当然,您应该将所有这些信息放入您的日志文件中,为了简单起见,我使用了 System.out 她:
对于大多数情况,这将是“太多”信息,但对于大多数情况,堆栈跟踪就足够了。一旦遇到棘手的问题,您会很高兴拥有所有“额外”信息
Besides the obvious - the exception stack trace - the more info you can get is better. So you should get all the system properties as well as environment variables. Also if your application have some settings, get all their values. Of course you should put all this info into your log file, I used System.out her for simplicity:
For most cases this will be "too much" information, but for most cases the stack trace will be enough. Once you will get a tough issue you will be happy that you have all that "extra" information
查看 Javadoc对于
System.getProperties()
,它记录了保证存在于每个 JVM 中的属性。Check out the Javadoc for
System.getProperties()
which documents the properties that are guaranteed to exist in every JVM.对于纯java应用程序:
For pure java applications:
此外,对于java servlet应用程序:
In addition, for java servlet applications:
对我真正有帮助的一件事是查看我的类是从哪里加载的。
obj.getClass().getProtectionDomain().getCodeSource().getLocation();
注意:protectiondomain 可以为 null,代码源也可以为 null,因此需要进行所需的 null 检查
One thing that really helps me- to see where my classes are getting loaded from.
obj.getClass().getProtectionDomain().getCodeSource().getLocation();
note: protectiondomain can be null as can code source so do the needed null checks