Skip to content

Commit e8979c0

Browse files
committed
Merge pull request apache#762 from rafaelweingartner/cglibThrowableRenderer
Proposal for an improved CglibThrowableRendererFollowing our discussions on mailing list, here is a PR with a proposal for an improvement on CglibThrowableRenderer. We would still have to define a number to limit the stack traces of causes (I randomly chose 3, just to get the code running). The test case is also not good in my opinion, but I ran out of ideas (waiting for suggestions on that). * pr/762: Changed the test case to test each of the log traces in the array list. Proposal for an improved CglibThrowableRenderer Signed-off-by: Rajani Karuturi <rajani.karuturi@citrix.com>
2 parents 1bbd23e + b49d681 commit e8979c0

2 files changed

Lines changed: 127 additions & 44 deletions

File tree

‎utils/src/main/java/com/cloud/utils/log/CglibThrowableRenderer.java‎

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919

2020
package com.cloud.utils.log;
2121

22-
import java.io.PrintWriter;
2322
import java.util.ArrayList;
23+
import java.util.List;
2424

25+
import org.apache.commons.lang.StringUtils;
2526
import org.apache.log4j.spi.ThrowableRenderer;
2627

2728
/**
28-
* This renderer removes all the Cglib generated methods from the call
29+
* This renderer removes all the CGLib generated methods from the call
2930
*
30-
* Unfortunately, I had to copy out the EnhancedThrowableRenderer from
31-
* the apach libraries because EnhancedThrowableRenderer is a final class.
31+
* Unfortunately, I had to copy out there-write the EnhancedThrowableRenderer from
32+
* the Apache libraries because EnhancedThrowableRenderer is a final class.
3233
* simply override doRender. Not sure what the developers are thinking there
3334
* making it final.
3435
*
@@ -37,48 +38,45 @@
3738
*
3839
*/
3940
public class CglibThrowableRenderer implements ThrowableRenderer {
40-
/**
41-
* Construct new instance.
42-
*/
43-
public CglibThrowableRenderer() {
44-
super();
45-
}
4641

42+
private final static int MAX_NUMBER_OF_STACK_TRACES_ON_LOG_FOR_CAUSE = 3;
4743
@Override
48-
public String[] doRender(final Throwable th) {
49-
try {
50-
ArrayList<String> lines = new ArrayList<String>();
51-
Throwable throwable = th;
52-
lines.add(throwable.toString());
53-
int start = 0;
54-
do {
55-
StackTraceElement[] elements = throwable.getStackTrace();
56-
for (int i = 0; i < elements.length - start; i++) {
57-
StackTraceElement element = elements[i];
58-
String filename = element.getFileName();
59-
String method = element.getMethodName();
60-
if ((filename != null && filename.equals("<generated>")) || (method != null && method.equals("invokeSuper"))) {
61-
continue;
62-
}
63-
lines.add("\tat " + element.toString());
64-
}
65-
if (start != 0) {
66-
lines.add("\t... " + start + " more");
67-
}
68-
throwable = throwable.getCause();
69-
if (throwable != null) {
70-
lines.add("Caused by: " + throwable.toString());
71-
start = elements.length - 1;
72-
}
73-
} while (throwable != null);
74-
return lines.toArray(new String[lines.size()]);
75-
} catch (Exception ex) {
76-
PrintWriter pw = new PrintWriter(System.err);
77-
ex.printStackTrace(pw);
78-
pw = new PrintWriter(System.out);
79-
ex.printStackTrace(pw);
80-
ex.printStackTrace();
81-
return null;
44+
public String[] doRender(Throwable th) {
45+
List<String> lines = new ArrayList<String>();
46+
lines.add(th.toString());
47+
addStackTraceToList(th, lines, 0);
48+
do {
49+
th = th.getCause();
50+
if (th != null) {
51+
lines.add("Caused by: " + th.toString());
52+
addStackTraceToList(th, lines, MAX_NUMBER_OF_STACK_TRACES_ON_LOG_FOR_CAUSE);
53+
}
54+
} while (th != null);
55+
return lines.toArray(new String[lines.size()]);
56+
}
57+
58+
/**
59+
* This method adds the stack traces retrieved from {@link Throwable#getStackTrace()}
60+
* The maxNumberOfStack attribute indicates the number of stacks that will be added,
61+
* if that value is 0, then all of the stack traces will be added, otherwise the stack traces will be limited to that number
62+
* @param th
63+
* @param lines
64+
* @param maxNumberOfStack
65+
*/
66+
private void addStackTraceToList(Throwable th, List<String> lines, int maxNumberOfStack) {
67+
StackTraceElement[] elements = th.getStackTrace();
68+
if (maxNumberOfStack == 0 || maxNumberOfStack > elements.length) {
69+
maxNumberOfStack = elements.length;
70+
}
71+
for (int i = 0; i < maxNumberOfStack; i++) {
72+
StackTraceElement element = elements[i];
73+
if (StringUtils.contains(element.getClassName(), "net.sf.cglib.proxy")) {
74+
continue;
75+
}
76+
lines.add("\tat " + element.toString());
77+
}
78+
if (maxNumberOfStack < elements.length) {
79+
lines.add("\t... " + (elements.length - maxNumberOfStack) + " more");
8280
}
8381
}
8482
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.utils.log;
21+
22+
import java.lang.reflect.Method;
23+
24+
import org.apache.commons.lang.StringUtils;
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
28+
import net.sf.cglib.proxy.Enhancer;
29+
import net.sf.cglib.proxy.MethodInterceptor;
30+
import net.sf.cglib.proxy.MethodProxy;
31+
32+
public class CglibThrowableRendererTest {
33+
34+
CglibThrowableRenderer cglibThrowableRenderer = new CglibThrowableRenderer();
35+
36+
@Test
37+
public void testDoRendere() {
38+
SampleClass sampleClass = (SampleClass)Enhancer.create(SampleClass.class, new MyInvocationHandler());
39+
try {
40+
sampleClass.theFirstMethodThatCapturesAnException();
41+
} catch (Exception e) {
42+
String[] exceptions = cglibThrowableRenderer.doRender(e);
43+
assertThatTheTraceListDoesNotContainsCgLibLogs(exceptions);
44+
}
45+
}
46+
47+
private void assertThatTheTraceListDoesNotContainsCgLibLogs(String[] exceptions) {
48+
for (String s : exceptions) {
49+
Assert.assertEquals(false, isCgLibLogTrace(s));
50+
}
51+
}
52+
53+
private boolean isCgLibLogTrace(String s) {
54+
return StringUtils.contains(s, "net.sf.cglib.proxy");
55+
}
56+
57+
static class SampleClass {
58+
public void theFirstMethodThatCapturesAnException() {
59+
try {
60+
methodThatCapturesAndThrowsException();
61+
} catch (Exception e) {
62+
throw new RuntimeException(e);
63+
}
64+
}
65+
66+
private void methodThatCapturesAndThrowsException() throws Exception {
67+
try {
68+
methodThatThrowsAnError();
69+
} catch (Error e) {
70+
throw new Exception("Throws an exception", e);
71+
}
72+
}
73+
74+
private void methodThatThrowsAnError() {
75+
throw new Error("Exception to test the CglibThrowableRenderer.");
76+
}
77+
}
78+
79+
static class MyInvocationHandler implements MethodInterceptor {
80+
@Override
81+
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
82+
return proxy.invoke(new SampleClass(), args);
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)