Author: carnold
Date: Sat Oct 10 17:12:53 2009
New Revision: 823889
URL: http://svn.apache.org/viewvc?rev=823889&view=rev
Log:
Bug 33717: Add %throwable{none} to suppress stack trace
Added:
logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/patternLayout.throwable
Modified:
logging/log4j/companions/extras/trunk/src/changes/changes.xml
logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java
logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/ThrowableInformationPatternConverter.java
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/PatternLayoutTestCase.java
Modified: logging/log4j/companions/extras/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/changes/changes.xml?rev=823889&r1=823888&r2=823889&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/changes/changes.xml (original)
+++ logging/log4j/companions/extras/trunk/src/changes/changes.xml Sat Oct 10
17:12:53 2009
@@ -26,6 +26,7 @@
<action action="fix" issue="46046">Track changes of default timezone in
EnhancedPatternLayout's %d conversion pattern.</action>
<action action="fix" issue="45704">DOMConfigurator.configure(URL) fails
on JRE 1.5.0_16.</action>
<action action="fix" issue="46741">Misuse of "it's" in Javadoc for
EnhancedPatternLayout.</action>
+ <action action="fix" issue="46741">Leaving out %throwable in
ConversionPattern adds throwable to logging message regardless.</action>
</release>
Modified:
logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java
URL:
http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java?rev=823889&r1=823888&r2=823889&view=diff
==============================================================================
---
logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java
(original)
+++
logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java
Sat Oct 10 17:12:53 2009
@@ -280,7 +280,9 @@
<p>Used to output the Throwable trace that has been bound to the
LoggingEvent, by
default this will output the full trace as one would normally find by a
call to Throwable.printStackTrace().
The throwable conversion word can be followed by an option in the form
<b>%throwable{short}</b>
- which will only output the first line of the ThrowableInformation.</p>
+ which will only output the first line of the ThrowableInformation, or
<b>%throwable{none}</b> which
+ will suppress the stack trace. If no %throwable pattern is provided, the
appender may provide its
+ rendering of the exception.</p>
</td>
</tr>
Modified:
logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/ThrowableInformationPatternConverter.java
URL:
http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/ThrowableInformationPatternConverter.java?rev=823889&r1=823888&r2=823889&view=diff
==============================================================================
---
logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/ThrowableInformationPatternConverter.java
(original)
+++
logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/ThrowableInformationPatternConverter.java
Sat Oct 10 17:12:53 2009
@@ -65,26 +65,21 @@
* {...@inheritdoc}
*/
public void format(final LoggingEvent event, final StringBuffer toAppendTo) {
- ThrowableInformation information = event.getThrowableInformation();
+ if (!"none".equals(option)) {
+ ThrowableInformation information = event.getThrowableInformation();
- if (information != null) {
- String[] stringRep = information.getThrowableStrRep();
+ if (information != null) {
+ String[] stringRep = information.getThrowableStrRep();
- int length = 0;
+ int length = stringRep.length;
+ if ("short".equals(option)) {
+ length = 1;
+ }
- if (option == null) {
- length = stringRep.length;
- } else if (option.equals("full")) {
- length = stringRep.length;
- } else if (option.equals("short")) {
- length = 1;
- } else {
- length = stringRep.length;
- }
-
- for (int i = 0; i < length; i++) {
- String string = stringRep[i];
- toAppendTo.append(string).append("\n");
+ for (int i = 0; i < length; i++) {
+ String string = stringRep[i];
+ toAppendTo.append(string).append("\n");
+ }
}
}
}
Modified:
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/PatternLayoutTestCase.java
URL:
http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/PatternLayoutTestCase.java?rev=823889&r1=823888&r2=823889&view=diff
==============================================================================
---
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/PatternLayoutTestCase.java
(original)
+++
logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/PatternLayoutTestCase.java
Sat Oct 10 17:12:53 2009
@@ -510,6 +510,54 @@
layout.activateOptions();
root.debug("finished mdc pattern test");
+
+ Transformer.transform(
+ OUTPUT_FILE, FILTERED,
+ new Filter[] {
+ new LineNumberFilter(), new SunReflectFilter(),
+ new JunitTestRunnerFilter(),
+ new MDCOrderFilter()
+ });
+
+ assertTrue(compare(FILTERED, WITNESS_FILE));
+ }
+
+ /**
+ Test case for throwable conversion pattern. */
+ public void testThrowable() throws Exception {
+ String OUTPUT_FILE = "patternLayout.throwable";
+ String WITNESS_FILE = "witness/pattern/patternLayout.throwable";
+
+
+ // set up appender
+ EnhancedPatternLayout layout = new EnhancedPatternLayout("%m%n");
+ Appender appender = new FileAppender(layout, OUTPUT_FILE, false);
+
+ // set appender on root and set level to debug
+ root.addAppender(appender);
+ root.setLevel(Level.DEBUG);
+
+ // output starting message
+ root.debug("starting throwable pattern test");
+ Exception ex = new Exception("Test Exception");
+ root.debug("plain pattern, no exception");
+ root.debug("plain pattern, with exception", ex);
+ layout.setConversionPattern("%m%n%throwable");
+ layout.activateOptions();
+ root.debug("%throwable, no exception");
+ root.debug("%throwable, with exception", ex);
+
+ layout.setConversionPattern("%m%n%throwable{short}");
+ layout.activateOptions();
+ root.debug("%throwable{short}, no exception");
+ root.debug("%throwable{short}, with exception", ex);
+
+ layout.setConversionPattern("%m%n%throwable{none}");
+ layout.activateOptions();
+ root.debug("%throwable{none}, no exception");
+ root.debug("%throwable{none}, with exception", ex);
+
+
Transformer.transform(
OUTPUT_FILE, FILTERED,
new Filter[] {
Added:
logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/patternLayout.throwable
URL:
http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/patternLayout.throwable?rev=823889&view=auto
==============================================================================
---
logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/patternLayout.throwable
(added)
+++
logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/patternLayout.throwable
Sat Oct 10 17:12:53 2009
@@ -0,0 +1,32 @@
+starting throwable pattern test
+plain pattern, no exception
+plain pattern, with exception
+java.lang.Exception: Test Exception
+ at org.apache.log4j.PatternLayoutTestCase.testThrowable(X)
+ at java.lang.reflect.Method.invoke(X)
+ at junit.framework.TestCase.runTest(X)
+ at junit.framework.TestCase.runBare(X)
+ at junit.framework.TestResult$1.protect(X)
+ at junit.framework.TestResult.runProtected(X)
+ at junit.framework.TestResult.run(X)
+ at junit.framework.TestCase.run(X)
+ at junit.framework.TestSuite.runTest(X)
+ at junit.framework.TestSuite.run(X)
+%throwable, no exception
+%throwable, with exception
+java.lang.Exception: Test Exception
+ at org.apache.log4j.PatternLayoutTestCase.testThrowable(X)
+ at java.lang.reflect.Method.invoke(X)
+ at junit.framework.TestCase.runTest(X)
+ at junit.framework.TestCase.runBare(X)
+ at junit.framework.TestResult$1.protect(X)
+ at junit.framework.TestResult.runProtected(X)
+ at junit.framework.TestResult.run(X)
+ at junit.framework.TestCase.run(X)
+ at junit.framework.TestSuite.runTest(X)
+ at junit.framework.TestSuite.run(X)
+%throwable{short}, no exception
+%throwable{short}, with exception
+java.lang.Exception: Test Exception
+%throwable{none}, no exception
+%throwable{none}, with exception
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]