de.huxhorn.lilith.data.logging
Class MessageFormatter

java.lang.Object
  extended by de.huxhorn.lilith.data.logging.MessageFormatter

public class MessageFormatter
extends java.lang.Object

Replacement for org.slf4j.helpers.MessageFormatter.

In contrast to the mentioned class, the formatting of message pattern and arguments into the actual message is split into three parts:

  1. Counting of placeholders in the message pattern (cheap)
  2. Conversion of argument array into an ArgumentResult, containing the arguments converted to String as well as an optional Throwable if available (relatively cheap)
  3. Replacement of placeholders in a message pattern with arguments given as String[]. (most expensive)

That way only the first two steps have to be done during event creation while the most expensive part, i.e. the actual construction of the message, is only done on demand.


Nested Class Summary
static class MessageFormatter.ArgumentResult
          This is just a simple class containing the result of an evaluateArgument call.
 
Constructor Summary
MessageFormatter()
           
 
Method Summary
static int countArgumentPlaceholders(java.lang.String messagePattern)
          Counts the number of unescaped placeholders in the given messagePattern.
static MessageFormatter.ArgumentResult evaluateArguments(java.lang.String messagePattern, java.lang.Object[] arguments)
          This method returns a MessageFormatter.ArgumentResult which contains the arguments converted to String as well as an optional Throwable.
static java.lang.String format(java.lang.String messagePattern, java.lang.String[] arguments)
          Replace placeholders in the given messagePattern with arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageFormatter

public MessageFormatter()
Method Detail

format

public static java.lang.String format(java.lang.String messagePattern,
                                      java.lang.String[] arguments)
Replace placeholders in the given messagePattern with arguments.

Parameters:
messagePattern - the message pattern containing placeholders.
arguments - the arguments to be used to replace placeholders.
Returns:
the formatted message.

countArgumentPlaceholders

public static int countArgumentPlaceholders(java.lang.String messagePattern)
Counts the number of unescaped placeholders in the given messagePattern.

Parameters:
messagePattern - the message pattern to be analyzed.
Returns:
the number of unescaped placeholders.

evaluateArguments

public static MessageFormatter.ArgumentResult evaluateArguments(java.lang.String messagePattern,
                                                                java.lang.Object[] arguments)

This method returns a MessageFormatter.ArgumentResult which contains the arguments converted to String as well as an optional Throwable.

If the last argument is a Throwable and is NOT used up by a placeholder in the message pattern it is returned in MessageFormatter.ArgumentResult.getThrowable() and won't be contained in the created String[].
If it is used up getThrowable will return null even if the last argument was a Throwable!

Parameters:
messagePattern - the message pattern that to be checked for placeholders.
arguments - the argument array to be converted.
Returns:
a MessageFormatter.ArgumentResult containing the converted arformatted message and optionally a Throwable.