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.

有帮助吗?

解决方案 3

For pure java applications:

System.getProperty("org.xml.sax.driver") 
System.getProperty("java.version")
System.getProperty("java.vm.version")
System.getProperty("os.name")
System.getProperty("os.version")
System.getProperty("os.arch")

其他提示

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:

System.out.println("----Java System Properties----");       
System.getProperties().list(System.out);

System.out.println("----System Environment Variables----");
Map<String, String> env = System.getenv();
Set<String> keys = env.keySet();
for (String key : keys) {
    System.out.println(key + "=" + env.get(key));
}

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

Check out the Javadoc for System.getProperties() which documents the properties that are guaranteed to exist in every JVM.

In addition, for java servlet applications:

response.getCharacterEncoding()
request.getSession().getId()
request.getRemoteHost()
request.getHeader("User-Agent") 
pageContext.getServletConfig().getServletContext().getServerInfo()

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top