[英]How to get the particular package from the exception stackTrace in java
I want to print only particular package from the exception stackTrace. 我只想从异常stackTrace中打印特定的程序包。 How to achieve this? 如何实现呢?
For example, consider the following stacktrace 例如,考虑以下堆栈跟踪
com.mongodb.MongoInternalException: DBPort.findOne failed
at com.mongodb.DBPort.findOne(DBPort.java:153)
at com.mongodb.DBPort.runCommand(DBPort.java:159)
at com.mongodb.DBTCPConnector.testMaster(DBTCPConnector.java:371)
at com.myapp.foo.MongoConnection.java:10
at com.myapp.foo.MongoConnection.java:27
I want to print only the package "com.myapp.foo" in the stackTrace with linenumber and methodName 我只想在具有行号和methodName的stackTrace中打印包“ com.myapp.foo”
What about something like this: 像这样的事情呢:
try {
throw new RuntimeException();
}
catch (RuntimeException e) {
StackTraceElement[] stackTrace = e.getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
String className = stackTraceElement.getClassName();
int pos = className.lastIndexOf('.');
String packageName = className.substring(0, pos);
System.out.println("Package:" + packageName +
", method: "+ stackTraceElement.getMethodName() +
", line:" + stackTraceElement.getLineNumber());
}
}
You can of course add any custom package filtration you need. 您当然可以添加所需的任何自定义包装过滤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.