Qrox
Qrox

Reputation: 63

Java logger message template doesn't work

I was using a logger, java.util.logging.Logger, in my program. First I wrote something like this:

logger.log(Level.INFO, "message"+str);

and the IDE told me I can use a message template to improve efficiency, so the IDE changed the above to:

logger.log(Level.INFO, "message{0}", str);

but when I ran the program the output was just:

11:43:05[INFO]message{0}

What is wrong with the above code? I have searched about the method and every site tells me that it is the right syntax, but it just doesn't seem to work.

Edit: I noticed another thread discussing this and the answer was to escape single quote. However, my original code hasn't any single quotes in the string

Upvotes: 4

Views: 1365

Answers (1)

jiangwh
jiangwh

Reputation: 66

You want to use the MessageFormat class log.info(MessageFormat.format("message{0}",string)) The string will replace the "{0}".

Upvotes: 3

Related Questions