You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2010/03/28 03:58:17 UTC
svn commit: r928319 - in /logging/log4j/trunk: src/changes/changes.xml
src/main/java/org/apache/log4j/EnhancedPatternLayout.java
src/main/java/org/apache/log4j/pattern/NameAbbreviator.java
tests/src/java/org/apache/log4j/pattern/NameAbbreviatorTest.java
Author: carnold
Date: Sun Mar 28 01:58:17 2010
New Revision: 928319
URL: http://svn.apache.org/viewvc?rev=928319&view=rev
Log:
Bug 49010: Add name abbreviator to drop n leading elements from names
Modified:
logging/log4j/trunk/src/changes/changes.xml
logging/log4j/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java
logging/log4j/trunk/src/main/java/org/apache/log4j/pattern/NameAbbreviator.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/pattern/NameAbbreviatorTest.java
Modified: logging/log4j/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/changes/changes.xml?rev=928319&r1=928318&r2=928319&view=diff
==============================================================================
--- logging/log4j/trunk/src/changes/changes.xml (original)
+++ logging/log4j/trunk/src/changes/changes.xml Sun Mar 28 01:58:17 2010
@@ -104,6 +104,7 @@
<action action="fix" issue="49005">TelnetAppenderTest.testIt fails on gcj.</action>
<action action="fix" issue="49006">Eclipse Java Compiler and gcj do not support -source=1.2.</action>
<action action="fix" issue="49007">CachedDateFormatTest fails on Apache Harmony.</action>
+ <action action="add" issue="49010">Add %p{-2} pattern to EnhancedPatternLayout to drop two leading elements from name.</action>
</release>
Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java?rev=928319&r1=928318&r2=928319&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java Sun Mar 28 01:58:17 2010
@@ -27,6 +27,15 @@ import org.apache.log4j.spi.LoggingEvent
// Anders Kristensen <ak...@dynamicsoft.com>
/**
+ * This class is an enhanced version of org.apache.log4j.PatternLayout
+ * which was originally developed as part of the abandoned log4j 1.3
+ * effort and has been available in the extras companion.
+ * This pattern layout should be used in preference to
+ * org.apache.log4j.PatternLayout except when compatibility
+ * where PatternLayout has been extended either through subclassing
+ * or alternative pattern parsers.
+ *
+ *
* <p>A flexible layout configurable with pattern string. The goal of this class
* is to {@link #format format} a {@link LoggingEvent} and return the results
* in a {@link StringBuffer}. The format of the result depends on the
@@ -82,15 +91,12 @@ import org.apache.log4j.spi.LoggingEvent
<td>Used to output the category of the logging event. The
category conversion specifier can be optionally followed by
- <em>precision specifier</em>, that is a decimal constant in
- brackets.
+ NameAbbreviator pattern.
- <p>If a precision specifier is given, then only the corresponding
- number of right most components of the category name will be
- printed. By default the category name is printed in full.
-
- <p>For example, for the category name "a.b.c" the pattern
- <b>%c{2}</b> will output "b.c".
+ <p>For example, for the category name "alpha.beta.gamma" the pattern
+ <b>%c{2}</b> will output the last two elements ("beta.gamma"),
+ <b>%c{-2}</b> will remove two elements leaving "gamma",
+ <b>%c{1.}</b> will output "a.b.gamma".
</td>
</tr>
@@ -103,12 +109,14 @@ import org.apache.log4j.spi.LoggingEvent
can be optionally followed by <em>precision specifier</em>, that
is a decimal constant in brackets.
- <p>If a precision specifier is given, then only the corresponding
- number of right most components of the class name will be
- printed. By default the class name is output in fully qualified form.
+ <td>Used to output the category of the logging event. The
+ category conversion specifier can be optionally followed by
+ NameAbbreviator pattern.
- <p>For example, for the class name "org.apache.xyz.SomeClass", the
- pattern <b>%C{1}</b> will output "SomeClass".
+ <p>For example, for the category name "alpha.beta.gamma" the pattern
+ <b>%c{2}</b> will output the last two elements ("beta.gamma"),
+ <b>%c{-2}</b> will remove two elements leaving "gamma",
+ <b>%c{1.}</b> will output "a.b.gamma".
<p><b>WARNING</b> Generating the caller class information is
slow. Thus, its use should be avoided unless execution speed is
@@ -405,7 +413,7 @@ import org.apache.log4j.spi.LoggingEvent
@author Ceki Gülcü
- @since 0.8.2 */
+ @since 1.2.16 */
public class EnhancedPatternLayout extends Layout {
/** Default pattern string for log output. Currently set to the
string <b>"%m%n"</b> which just prints the application supplied
Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/pattern/NameAbbreviator.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/pattern/NameAbbreviator.java?rev=928319&r1=928318&r2=928319&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/pattern/NameAbbreviator.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/pattern/NameAbbreviator.java Sun Mar 28 01:58:17 2010
@@ -24,7 +24,6 @@ import java.util.List;
/**
* NameAbbreviator generates abbreviated logger and class names.
*
- * @author Curt Arnold
*/
public abstract class NameAbbreviator {
/**
@@ -36,6 +35,7 @@ public abstract class NameAbbreviator {
* Gets an abbreviator.
*
* For example, "%logger{2}" will output only 2 elements of the logger name,
+ * %logger{-2} will drop 2 elements from the logger name,
* "%logger{1.}" will output only the first character of the non-final elements in the name,
* "%logger{1~.2~} will output the first character of the first element, two characters of
* the second and subsequent elements and will use a tilde to indicate abbreviated characters.
@@ -54,18 +54,29 @@ public abstract class NameAbbreviator {
}
int i = 0;
-
- while (
- (i < trimmed.length()) && (trimmed.charAt(i) >= '0')
- && (trimmed.charAt(i) <= '9')) {
- i++;
+ if (trimmed.length() > 0) {
+ if (trimmed.charAt(0) == '-') {
+ i++;
+ }
+ for (;
+ (i < trimmed.length()) &&
+ (trimmed.charAt(i) >= '0') &&
+ (trimmed.charAt(i) <= '9');
+ i++) {
+ }
}
+
//
// if all blanks and digits
//
if (i == trimmed.length()) {
- return new MaxElementAbbreviator(Integer.parseInt(trimmed));
+ int elements = Integer.parseInt(trimmed);
+ if (elements >= 0) {
+ return new MaxElementAbbreviator(elements);
+ } else {
+ return new DropElementAbbreviator(-elements);
+ }
}
ArrayList fragments = new ArrayList(5);
@@ -193,6 +204,42 @@ public abstract class NameAbbreviator {
}
/**
+ * Abbreviator that drops starting path elements.
+ */
+ private static class DropElementAbbreviator extends NameAbbreviator {
+ /**
+ * Maximum number of path elements to output.
+ */
+ private final int count;
+
+ /**
+ * Create new instance.
+ * @param count maximum number of path elements to output.
+ */
+ public DropElementAbbreviator(final int count) {
+ this.count = count;
+ }
+
+ /**
+ * Abbreviate name.
+ * @param buf buffer to append abbreviation.
+ * @param nameStart start of name to abbreviate.
+ */
+ public void abbreviate(final int nameStart, final StringBuffer buf) {
+ int i = count;
+ for(int pos = buf.indexOf(".", nameStart);
+ pos != -1;
+ pos = buf.indexOf(".", pos + 1)) {
+ if(--i == 0) {
+ buf.delete(nameStart, pos + 1);
+ break;
+ }
+ }
+ }
+ }
+
+
+ /**
* Fragment of an pattern abbreviator.
*
*/
Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/pattern/NameAbbreviatorTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/pattern/NameAbbreviatorTest.java?rev=928319&r1=928318&r2=928319&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/pattern/NameAbbreviatorTest.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/pattern/NameAbbreviatorTest.java Sun Mar 28 01:58:17 2010
@@ -23,8 +23,6 @@ import junit.framework.TestCase;
/**
* Tests for NameAbbrevator.
*
- * @author Curt Arnold
- *
*/
public class NameAbbreviatorTest extends TestCase {
/**
@@ -300,4 +298,39 @@ public class NameAbbreviatorTest extends
abbrev.abbreviate(fieldStart, buf);
assertEquals("DEBUG - .", buf.toString());
}
+
+ /**
+ * Check that getAbbreviator("-1").abbreviate() drops first name element.
+ *
+ */
+ public void testMinusOne() {
+ NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("-1");
+ StringBuffer buf = new StringBuffer("DEBUG - ");
+ int fieldStart = buf.length();
+ buf.append("org.example.foo.bar");
+ abbrev.abbreviate(fieldStart, buf);
+ assertEquals("DEBUG - example.foo.bar", buf.toString());
+
+ buf.setLength(0);
+ buf.append("DEBUG - ");
+ fieldStart = buf.length();
+ buf.append("bar");
+ abbrev.abbreviate(fieldStart, buf);
+ assertEquals("DEBUG - bar", buf.toString());
+
+ buf.setLength(0);
+ buf.append("DEBUG - ");
+ fieldStart = buf.length();
+ abbrev.abbreviate(fieldStart, buf);
+ assertEquals("DEBUG - ", buf.toString());
+
+ buf.setLength(0);
+ buf.append("DEBUG - ");
+ fieldStart = buf.length();
+ buf.append(".");
+ abbrev.abbreviate(fieldStart, buf);
+ assertEquals("DEBUG - ", buf.toString());
+
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org