1818
1919package com .cloud .utils .log ;
2020
21- import java .io .File ;
21+ import java .io .PrintWriter ;
2222import java .lang .reflect .Method ;
23- import java .net .URL ;
24- import java .security .CodeSource ;
2523import java .util .ArrayList ;
26- import java .util .HashMap ;
27- import java .util .Map ;
2824
2925import org .apache .log4j .spi .ThrowableRenderer ;
3026
@@ -70,22 +66,20 @@ public CglibThrowableRenderer() {
7066 @ Override
7167 public String [] doRender (final Throwable th ) {
7268 try {
73- Object [] noArgs = null ;
7469 ArrayList <String > lines = new ArrayList <String >();
7570 Throwable throwable = th ;
7671 lines .add (throwable .toString ());
77- Map classMap = new HashMap ();
7872 int start = 0 ;
7973 do {
80- Object [] elements = ( Object []) getStackTraceMethod . invoke ( throwable , noArgs );
74+ StackTraceElement [] elements = throwable . getStackTrace ( );
8175 for (int i = 0 ; i < elements .length - start ; i ++) {
82- if ( elements [i ] instanceof StackTraceElement ) {
83- StackTraceElement stack = ( StackTraceElement ) elements [ i ] ;
84- if ( stack . getFileName (). equals ( "<generated>" ) || stack . getFileName (). equals ( "MethodProxy.java" )) {
85- continue ;
86- }
76+ StackTraceElement element = elements [i ];
77+ String filename = element . getFileName () ;
78+ String method = element . getMethodName ();
79+ if (( filename != null && filename . equals ( "<generated>" )) || ( method != null && method . equals ( "invokeSuper" ))) {
80+ continue ;
8781 }
88- lines .add (formatElement ( elements [ i ], classMap ));
82+ lines .add (" \t at " + element . toString ( ));
8983 }
9084 if (start != 0 ) {
9185 lines .add ("\t ... " + start + " more" );
@@ -98,81 +92,15 @@ public String[] doRender(final Throwable th) {
9892 } while (throwable != null );
9993 return lines .toArray (new String [lines .size ()]);
10094 } catch (Exception ex ) {
95+ PrintWriter pw = new PrintWriter (System .err );
96+ ex .printStackTrace (pw );
97+ pw = new PrintWriter (System .out );
98+ ex .printStackTrace (pw );
99+ ex .printStackTrace ();
101100 return null ;
102101 }
103102 }
104103
105- /**
106- * Format one element from stack trace.
107- *
108- * @param element element, may not be null.
109- * @param classMap map of class name to location.
110- * @return string representation of element.
111- */
112- private String formatElement (final Object element , final Map classMap ) {
113- StringBuffer buf = new StringBuffer ("\t at " );
114- buf .append (element );
115- try {
116- String className = getClassNameMethod .invoke (element , (Object [])null ).toString ();
117- Object classDetails = classMap .get (className );
118- if (classDetails != null ) {
119- buf .append (classDetails );
120- } else {
121- Class cls = findClass (className );
122- int detailStart = buf .length ();
123- buf .append ('[' );
124- try {
125- CodeSource source = cls .getProtectionDomain ().getCodeSource ();
126- if (source != null ) {
127- URL locationURL = source .getLocation ();
128- if (locationURL != null ) {
129- //
130- // if a file: URL
131- //
132- if ("file" .equals (locationURL .getProtocol ())) {
133- String path = locationURL .getPath ();
134- if (path != null ) {
135- //
136- // find the last file separator character
137- //
138- int lastSlash = path .lastIndexOf ('/' );
139- int lastBack = path .lastIndexOf (File .separatorChar );
140- if (lastBack > lastSlash ) {
141- lastSlash = lastBack ;
142- }
143- //
144- // if no separator or ends with separator (a directory)
145- // then output the URL, otherwise just the file name.
146- //
147- if (lastSlash <= 0 || lastSlash == path .length () - 1 ) {
148- buf .append (locationURL );
149- } else {
150- buf .append (path .substring (lastSlash + 1 ));
151- }
152- }
153- } else {
154- buf .append (locationURL );
155- }
156- }
157- }
158- } catch (SecurityException ex ) {
159- }
160- buf .append (':' );
161- Package pkg = cls .getPackage ();
162- if (pkg != null ) {
163- String implVersion = pkg .getImplementationVersion ();
164- if (implVersion != null ) {
165- buf .append (implVersion );
166- }
167- }
168- buf .append (']' );
169- classMap .put (className , buf .substring (detailStart ));
170- }
171- } catch (Exception ex ) {
172- }
173- return buf .toString ();
174- }
175-
176104 /**
177105 * Find class given class name.
178106 *
0 commit comments