[slf4j-dev] svn commit: r461 - slf4j/trunk/src/java/org/slf4j/impl

Cédrik LIME clime at improve.fr
Wed Dec 21 10:00:14 CET 2005


Selon Christian Stein <sormuras at gmx.de>:

> > - Format methods taking 1 or 2 arguments now delegate to the format method
> >   that takes in an array argument. This results in smaller code. 
> 
> ...but in creation of an Object[], if you only need the 1 or 2 arg variant
> and therefore in slight speed decrease - hey, that's fine with me!

True, but that shouldn't be a problem with modern VMs (it is a short-lived object).


There are a couple of annoying glitches with said code though:
1. unless I am abused, parsing stops at the first 'invalid
DELIM_START/DELIM_STOP pair', i.e. "{blah} {}" or "{\\}". Shouldn't we continue
parsing in search of a good delimiter pair?
2. what if I want to pass in an Object[] as an argument to the log message? For
example:
    Object[] myArray = new Integer[] {new Integer(1)};
    log.debug("Array is: {}", myArray);
In this case, I am afraid formatted string will be "Array is: 1" instead of
"Array is: [[java.lang.Integer(1)".
3. you still need to modify all Logger classes for this code to be effective!

FYI, I came up with following code yesterday (didn't post it as I didn't have a
chance to thoroughly test it): it iterates over the messagePattern delimiter
pairs instead of the arguments array.
Note that I still have to handle the case "one {} and an Object[] arg" (previous
case 2).

  Cédrik


public static String format(String messagePattern, Object[] arguments) {
	int nUsedArgs = 0;
	int last = 0;
	final int len = messagePattern.length();
	int start = messagePattern.indexOf(DELIM_START);

	StringBuffer sbuf = null;// optimisation: will be created only if necessary

	while (start != -1 && (start + 1 != len)) {
		char delimStop = messagePattern.charAt(start + 1);
		char escape = messagePattern.charAt(start - 1);
		if ((delimStop != DELIM_STOP) || (escape == '\\')) {
			// invalid DELIM_START/DELIM_STOP pair: find next one
			last = start + 2;
			start = messagePattern.indexOf(DELIM_START, last);
			continue;
		}
		if (null == sbuf) {
			sbuf = new StringBuffer(len + 24 * arguments.length);
		}
		sbuf.append(messagePattern.substring(last, start));
		sbuf.append((nUsedArgs < arguments.length) ? arguments[nUsedArgs] : null);
		++nUsedArgs;
		last = start + 2;
		start = messagePattern.indexOf(DELIM_START, last);
	}
	if (null != sbuf) {
		if (last <= len) {
			// append the characters following the last DELIM_START/DELIM_STOP pair.
			sbuf.append(messagePattern.substring(last, len));
		}
		return sbuf.toString();
	} else {
		// there was no argument in messagePattern
		return messagePattern;
	}
}



More information about the slf4j-dev mailing list