Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.logging message template question

Tags:

java

logging

NetBeans recommended that I change the way that logging statements that have string concatenation are written, stating Convert string concatenation to a message template so that a statement such as:

log.severe("Completed at:  " + new Date());

got changed to

log.log(Level.SEVERE, "Completed at:  {0}", new Date());

The problem is that now the Date doesn't get printed. Instead, the string "{0}" literatlly gets printed instead. Is there something else I was suppose to do?

like image 520
Winter Avatar asked Oct 15 '22 02:10

Winter


1 Answers

Assuming that the code snippet you posted is not the original code that was causing problems... Having a single apostrophe in your message will cause the type of a problem you described. java.util.logging.Logger.log passes the message to java.text.MessageFormat which requires you to escape apostrophes.

For example:

log.log( Level.FINE, "Can't handle {0}.", id );
log.log( Level.FINE, "Can''t handle {0}.", id );

Logs:

Cant handle {0}.
Can't handle ID0001.
like image 131
Maine Avatar answered Oct 16 '22 14:10

Maine