ASF Bugzilla – Attachment 22514 Details for
Bug 45721
[PATCH] when showing a stack trace - include the relative package versions and optional jar name to aid debugging
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
here's an updated patch that lets this feature be disabled via calling ThrowableInformation.setIncludeVersionInformation(false).set
add_package_version_and_jar_file_information_to_stack_traces_that_can_be_disabled.patch (text/plain), 6.25 KB, created by
James Strachan
on 2008-09-02 07:01:50 UTC
(
hide
)
Description:
here's an updated patch that lets this feature be disabled via calling ThrowableInformation.setIncludeVersionInformation(false).set
Filename:
MIME Type:
Creator:
James Strachan
Created:
2008-09-02 07:01:50 UTC
Size:
6.25 KB
patch
obsolete
>Index: tests/src/java/org/apache/log4j/spi/ThrowableInformationTest.java >=================================================================== >--- tests/src/java/org/apache/log4j/spi/ThrowableInformationTest.java (revision 691242) >+++ tests/src/java/org/apache/log4j/spi/ThrowableInformationTest.java Tue Sep 02 14:31:23 BST 2008 >@@ -20,7 +20,9 @@ > > import java.io.PrintWriter; > >+import org.apache.log4j.config.PropertySetterException; > >+ > /** > * Unit tests for ThrowableInformation. > */ >@@ -299,4 +301,20 @@ > String[] rep2 = ti.getThrowableStrRep(); > assertEquals("Hello, World", rep2[0]); > } >+ >+ public void testStackTracePackageName() throws Exception { >+ ThrowableInformation ti = new ThrowableInformation( >+ new PropertySetterException("Hello")); >+ String[] rep = ti.getThrowableStrRep(); >+ boolean found = false; >+ for (int i = 0, size = rep.length; i < size; i++) { >+ String line = rep[i]; >+ //System.out.println(line); >+ if (line.indexOf("[") > 0) { >+ found = true; >-} >+ } >+ } >+ assertTrue("Found a package version for the JDK", found); >+ } >+ >+} >Index: src/main/java/org/apache/log4j/spi/ThrowableInformation.java >=================================================================== >--- src/main/java/org/apache/log4j/spi/ThrowableInformation.java (revision 691242) >+++ src/main/java/org/apache/log4j/spi/ThrowableInformation.java Tue Sep 02 14:31:23 BST 2008 >@@ -19,6 +19,7 @@ > > import java.io.*; > import java.util.ArrayList; >+import java.net.URL; > > /** > * ThrowableInformation is log4j's internal representation of >@@ -40,6 +41,24 @@ > private transient Throwable throwable; > private String[] rep; > >+ private static boolean includeVersionInformation = true; >+ >+ /** >+ * Returns true if version information should be included in stack traces >+ */ >+ public static >+ boolean isIncludeVersionInformation() { >+ return includeVersionInformation; >+ } >+ >+ /** >+ * Allows the extra version information included in stack traces to be disabled >+ */ >+ public static >+ void setIncludeVersionInformation(boolean includeVersionInformation) { >+ ThrowableInformation.includeVersionInformation = includeVersionInformation; >+ } >+ > public > ThrowableInformation(Throwable throwable) { > this.throwable = throwable; >@@ -75,7 +94,7 @@ > try { > String line = reader.readLine(); > while(line != null) { >- lines.add(line); >+ lines.add(formatExceptionLine(line)); > line = reader.readLine(); > } > } catch(IOException ex) { >@@ -90,6 +109,126 @@ > } > return (String[]) rep.clone(); > } >+ >+ /** >+ * Appends the optional text [jarName:versionNumber] to each line of the stack trace to >+ * indicate what name of jar the code comes from along with which version was found on the classpath >+ */ >+ protected >+ String formatExceptionLine(String line) { >+ if (!isIncludeVersionInformation()) { >+ return line; >-} >+ } >+ String trimmed = line.trim(); >+ if (trimmed.startsWith("at ")) { >+ // lets try append the package version to this line >+ String classAndMethod = trimmed.substring(3).trim(); >+ int idx = classAndMethod.indexOf('('); >+ if (idx > 0) { >+ classAndMethod = classAndMethod.substring(0, idx); >+ } >+ idx = classAndMethod.lastIndexOf('.'); >+ String className = classAndMethod; >+ String method = ""; >+ if (idx > 0) { >+ method = classAndMethod.substring(idx + 1); >+ className = className.substring(0, idx); >+ } >+ String packageName = ""; >+ idx = className.lastIndexOf('.'); >+ if (idx > 0) { >+ packageName = className.substring(0, idx); >+ } >+ if (packageName.length() > 0) { >+ String version = getVersion(packageName); >+ String jarFile = getJarNameOfClass(className); >+ if (notNullOrBlank(version)) { >+ if (notNullOrBlank(jarFile)) { >+ return line + " [" + jarFile + ":" + version + "]"; >+ } >+ else { >+ return line + " [" + version + "]"; >+ } >+ } >+ else if (notNullOrBlank(jarFile)) { >+ return line + " [" + jarFile + "]"; >+ } >+ } >+ } >+ return line; >+ } > >+ protected static boolean notNullOrBlank(String text) { >+ return text != null && text.length() > 0; >+ } > >+ /** >+ * Attempts to deduce the package version number of the given package >+ * that is currently on the classpath >+ */ >+ protected >+ String getVersion(String packageName) { >+ try { >+ Package aPackage = Package.getPackage(packageName); >+ if (aPackage != null) { >+ return aPackage.getImplementationVersion(); >+ } >+ } catch (Exception e) { >+ // ignore exception >+ } >+ return ""; >+ } >+ >+ /** >+ * Uses the context class path or the current global class loader to >+ * deduce the file that the given class name comes from >+ */ >+ protected >+ String getJarNameOfClass(String className) { >+ try { >+ Class type = findClass(className); >+ if (type != null) { >+ URL resource = type.getClassLoader().getResource(type.getName().replace('.', '/') + ".class"); >+ if (resource != null) { >+ String text = resource.toString(); >+ int idx = text.lastIndexOf('!'); >+ if (idx > 0) { >+ text = text.substring(0, idx); >+ // now lets remove all but the file name >+ idx = text.lastIndexOf('/'); >+ if (idx > 0) { >+ text = text.substring(idx + 1); >+ } >+ idx = text.lastIndexOf('\\'); >+ if (idx > 0) { >+ text = text.substring(idx + 1); >+ } >+ return text; >+ } >+ } >+ } >+ } >+ catch (Exception e) { >+ // ignore >+ } >+ return ""; >+ } >+ >+ private Class findClass(String className) { >+ try { >+ return Thread.currentThread().getContextClassLoader().loadClass(className); >+ } catch (ClassNotFoundException e) { >+ try { >+ return Class.forName(className); >+ } catch (ClassNotFoundException e1) { >+ try { >+ return getClass().getClassLoader().loadClass(className); >+ } catch (ClassNotFoundException e2) { >+ return null; >+ } >+ } >+ } >+ } >+} >+ >+
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 45721
:
22506
| 22514 |
22519