Java.util.logging message template question

NetBeans recommended that I change the way we write record statements containing string concatenation, specifying Convert string concatenation to a message template so that the statement, for example:

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

changed to

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

The problem is that now the date is not printed. Instead, the string "{0}" literatlly is printed instead. Is there anything else I could do?

+4
source share
2 answers

Assuming the code snippet you submitted is not the source code causing the problems ... Having one apostrophe in your message will result in the type of problem you described. java.util.logging.Logger.log sends a java.text.MessageFormat message that requires you to avoid apostrophes .

For instance:

 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. 
+3
source

I know that PrintStream has a format method that works this way, although Java uses C-like % prefix instead of a C # -like {} shell. But Logger does not have such a method. Instead, you call the log override, which in my experience writes only a string exactly as specified, and does nothing with the Object parameter.

0
source

All Articles