logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Cesar Gomez Mora" <cm...@ctp.com>
Subject Bug in PatternConverter
Date Thu, 17 Oct 2002 13:57:46 GMT
Hi all,

I have been customizing PatternLayout and PatternConverter lately to accept some of my own
modifiers. One of them is a field that will contain a 'very long' String. As I'm only interested
in the beginning of this String, I thought left aligning it will show the beginning, but to
my surprise it doesn't:

String: "1234567890"
Pattern 1: %-5.5W
Output: 67890
Pattern 2: %5.5W
Output: 67890

The problem is this seems to be a 'feature' and not a bug. So I decided to create my own PatternConverter
to handle this. To my surprise, the attributes min, max, and leftAlign are not visible to
PatternConverter siblings out of the package... so I had to define them again in myPatternConverter...
but then I couldn't read them from FormattingInfo since they are also not visible out of the

Solution: modify PatternConverter to specify attribute visibility:
/////// CODE STARTS ////////////
public abstract class PatternConverter {
  public PatternConverter next;
  protected int min = -1;
  protected int max = 0x7FFFFFFF;
  protected boolean leftAlign = false;
/////// CODE ENDS ////////////

The fix is then trivial:
/////// CODE STARTS ////////////
private class SpiderPatternConverter extends org.apache.log4j.helpers.PatternConverter

	* This method for formatting in a converter specific way has been overridden to correct a
bug in the original log4j
	public void format(StringBuffer sbuf, LoggingEvent e)
		String s= convert(e);

		if (s == null)
			if (0 < min)
				spacePad(sbuf, min);

		int len= s.length();

		/* This is a correction for a bug in the original format of the parent class PatternParser,
count on the alignment
		  for the chopping of the buffer. If left aligned chop the end, if right aligned, chop the
beggining */
		if (len > max) {
			if (leftAlign) {
			} else {
				sbuf.append(s.substring(len - max));

/////// CODE ENDS ////////////

Will this fix get into the log4j libraries anytime? Do I have to post the bug in Bugzilla?


Cesar Gomez-Mora

Cesar Gomez-Mora
Cambridge Technology Partners
The global eServices company of Novell, Inc.

To unsubscribe, e-mail:   <mailto:log4j-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:log4j-dev-help@jakarta.apache.org>

View raw message