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 Cesar Gomez Mora <cm...@ctp.com> on 2002/10/17 15:57:46 UTC

Bug in PatternConverter

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 package!

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);
			return;
		}

		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) {
				sbuf.append(s.substring(0,max-1));
			} 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?

Cheers,

Cesar Gomez-Mora

--
Cesar Gomez-Mora
Cambridge Technology Partners
The global eServices company of Novell, Inc.
http://www.ctp.com 

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>