ASF Bugzilla – Attachment 29930 Details for
Bug 51122
Include option in throwable pattern converter to control stack trace separator
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Proposed Patch
LOG4J-51122.patch (text/plain), 7.69 KB, created by
jpolsky
on 2013-02-06 19:38:07 UTC
(
hide
)
Description:
Proposed Patch
Filename:
MIME Type:
Creator:
jpolsky
Created:
2013-02-06 19:38:07 UTC
Size:
7.69 KB
patch
obsolete
>Index: tests/src/java/org/apache/log4j/pattern/ThrowableInformationPatternConverterTest.java >=================================================================== >--- tests/src/java/org/apache/log4j/pattern/ThrowableInformationPatternConverterTest.java (revision 0) >+++ tests/src/java/org/apache/log4j/pattern/ThrowableInformationPatternConverterTest.java (revision 0) >@@ -0,0 +1,159 @@ >+/* >+ * Licensed to the Apache Software Foundation (ASF) under one or more >+ * contributor license agreements. See the NOTICE file distributed with >+ * this work for additional information regarding copyright ownership. >+ * The ASF licenses this file to You under the Apache License, Version 2.0 >+ * (the "License"); you may not use this file except in compliance with >+ * the License. You may obtain a copy of the License at >+ * >+ * http://www.apache.org/licenses/LICENSE-2.0 >+ * >+ * Unless required by applicable law or agreed to in writing, software >+ * distributed under the License is distributed on an "AS IS" BASIS, >+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. >+ * See the License for the specific language governing permissions and >+ * limitations under the License. >+ */ >+ >+package org.apache.log4j.pattern; >+ >+import java.lang.reflect.Field; >+ >+import junit.framework.TestCase; >+ >+/** >+ * Unit tests for {@code ThrowableInformationPatternConverter}. >+ * >+ */ >+public class ThrowableInformationPatternConverterTest extends TestCase { >+ >+ /** >+ * Runs a given test comparing against the expected values. >+ * @param options The list of options to parse. >+ * @param expectedLines The expected lines. >+ * @param expectedSeparator The expected seperator. >+ */ >+ private static void test(final String[] options, final int expectedLines, final String expectedSeparator) throws Exception { >+ final ThrowableInformationPatternConverter o = ThrowableInformationPatternConverter.newInstance(options); >+ >+ Field linesField = o.getClass().getDeclaredField("maxLines"); >+ linesField.setAccessible(true); >+ assertEquals("maxLines", expectedLines, ((Integer)linesField.get(o)).intValue()); >+ >+ Field separatorField = o.getClass().getDeclaredField("separator"); >+ separatorField.setAccessible(true); >+ assertEquals("separator", expectedSeparator, (String)separatorField.get(o)); >+ } >+ >+ /** >+ * Test {@code %throwable } with null options. >+ */ >+ public void testNull() throws Exception { >+ test(null, Integer.MAX_VALUE, "\n"); >+ } >+ >+ /** >+ * Test {@code %throwable } >+ */ >+ public void testEmpty() throws Exception { >+ test(new String[]{}, Integer.MAX_VALUE, "\n"); >+ } >+ >+ /** >+ * Test {@code %throwable{} } with null option value. >+ */ >+ public void testOneNullElement() throws Exception { >+ test(new String[]{null}, Integer.MAX_VALUE, "\n"); >+ } >+ >+ /** >+ * Test {@code %throwable{} } >+ */ >+ public void testOneEmptyElement() throws Exception { >+ test(new String[]{""}, Integer.MAX_VALUE, "\n"); >+ } >+ >+ /** >+ * Test {@code %throwable{full} } >+ */ >+ public void testFull() throws Exception { >+ test(new String[]{"full"}, Integer.MAX_VALUE, "\n"); >+ } >+ >+ /** >+ * Test {@code %throwable{none} } >+ */ >+ public void testNone() throws Exception { >+ test(new String[]{"none"}, 0, "\n"); >+ } >+ >+ /** >+ * Test {@code %throwable{short} } >+ */ >+ public void testShort() throws Exception { >+ test(new String[]{"short"}, 1, "\n"); >+ } >+ >+ /** >+ * Test {@code %throwable{10} } >+ */ >+ public void testDepth() throws Exception { >+ test(new String[]{"10"}, 10, "\n"); >+ } >+ >+ /** >+ * Test {@code %throwable{separator(|)} } >+ */ >+ public void testSeparator() throws Exception { >+ test(new String[]{"separator(|)"}, Integer.MAX_VALUE, "|"); >+ } >+ >+ /** >+ * Test {@code %throwable{separator()} } >+ */ >+ public void testSeparatorAsEmpty() throws Exception { >+ test(new String[]{"separator()"}, Integer.MAX_VALUE, ""); >+ } >+ >+ /** >+ * Test {@code %throwable{separator(\n)} } >+ */ >+ public void testSeparatorAsDefaultLineSeparator() throws Exception { >+ test(new String[]{"separator(\n)"}, Integer.MAX_VALUE, "\n"); >+ } >+ >+ /** >+ * Test {@code %throwable{separator(|)} } >+ */ >+ public void testSeparatorAsMultipleCharacters() throws Exception { >+ test(new String[]{"separator( | )"}, Integer.MAX_VALUE, " | "); >+ } >+ >+ /** >+ * Test {@code %throwable{full}{separator(|)} } >+ */ >+ public void testFullAndSeparator() throws Exception { >+ test(new String[]{"full","separator(|)"}, Integer.MAX_VALUE, "|"); >+ } >+ >+ /** >+ * Test {@code %throwable{none}{separator(|)} } >+ */ >+ public void testNoneAndSeparator() throws Exception { >+ test(new String[]{"none","separator(|)"}, 0, "|"); >+ } >+ >+ /** >+ * Test {@code %throwable{short}{separator(|)} } >+ */ >+ public void testShortAndSeparator() throws Exception { >+ test(new String[]{"short","separator(|)"}, 1, "|"); >+ } >+ >+ /** >+ * Test {@code %throwable{10}{separator(|)} } >+ */ >+ public void testDepthAndSeparator() throws Exception { >+ test(new String[]{"10","separator(|)"}, 10, "|"); >+ } >+} >Index: src/main/java/org/apache/log4j/pattern/ThrowableInformationPatternConverter.java >=================================================================== >--- src/main/java/org/apache/log4j/pattern/ThrowableInformationPatternConverter.java (revision 1443083) >+++ src/main/java/org/apache/log4j/pattern/ThrowableInformationPatternConverter.java (working copy) >@@ -36,11 +36,21 @@ > extends LoggingEventPatternConverter { > > /** >+ * The default line separator. >+ */ >+ private static final String LINE_SEP = "\n"; >+ >+ /** > * Maximum lines of stack trace to output. > */ > private int maxLines = Integer.MAX_VALUE; > > /** >+ * The line separator. >+ */ >+ private String separator = LINE_SEP; >+ >+ /** > * Private constructor. > * @param options options, may be null. > */ >@@ -49,16 +59,25 @@ > super("Throwable", "throwable"); > > if ((options != null) && (options.length > 0)) { >- if("none".equals(options[0])) { >- maxLines = 0; >- } else if("short".equals(options[0])) { >- maxLines = 1; >- } else { >- try { >- maxLines = Integer.parseInt(options[0]); >- } catch(NumberFormatException ex) { >- } >- } >+ for (int i = 0; i < options.length; i++) { >+ if (options[i] != null) { >+ final String option = options[i].trim(); >+ if (option.length() == 0) { >+ // continue; >+ } else if (option.startsWith("separator(") && option.endsWith(")")) { >+ separator = option.substring("separator(".length(), option.length() - 1); >+ } else if("none".equals(option)) { >+ maxLines = 0; >+ } else if("short".equals(option)) { >+ maxLines = 1; >+ } else { >+ try { >+ maxLines = Integer.parseInt(option); >+ } catch(NumberFormatException ex) { >+ } >+ } >+ } >+ } > } > } > >@@ -90,9 +109,17 @@ > length = maxLines; > } > >- for (int i = 0; i < length; i++) { >- String string = stringRep[i]; >- toAppendTo.append(string).append("\n"); >+ if (length > 0) { >+ for (int i = 0, limit = length - 1; i <= limit; i++) { >+ String string = stringRep[i]; >+ toAppendTo.append(string); >+ if (i < limit) { >+ toAppendTo.append(this.separator); >+ } >+ } >+ if (LINE_SEP.equals(this.separator)) { >+ toAppendTo.append(this.separator); >+ } > } > } > }
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 51122
: 29930