Search code examples
javalogging

How to I format my log message


I would like to format my log message before being printed out on the console Logger.fine

e.g. How do I format "{0} has {1} apples with him", so the inputs are John and 10

I would prefer a logging framework which provides a solution to this and I don't want to format these messages separately. JDK6 specific logging classes don't seem to have these granularity.


Solution

  • Use MessageFormat:

    String s = MessageFormat.format("{0} has {1} apples with him", "John", 10);
    

    or String.format:

    String s = String.format("%1$s has %2$d apples with him", "John", 10);