This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes java.util.logging: MessageFormat-style parameters or String.format()/printf style Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.util.logging: MessageFormat-style parameters or String.format()/printf style" Watch "java.util.logging: MessageFormat-style parameters or String.format()/printf style" New topic
Author

java.util.logging: MessageFormat-style parameters or String.format()/printf style

Faraz Syed
Greenhorn

Joined: Feb 12, 2009
Posts: 10
I am wondering if there are any performance or efficiency reasons to use MessageFormat-style parameters to log messages e.g.:



as opposed to using String.format() to format a log message containing printf-style specifiers:



In all the documentation/tutorials I've found for the java.util.logging facility, I haven't come across much info on using the MessageFormat-style approach for passing parameters to log messages.

I'd like to know what are the best practices for parameterized logging when using the java.util.logging for Logging.

Thanks.
Jacob Anawalt
Ranch Hand

Joined: Oct 27, 2010
Posts: 32

Within the context of logging performance and efficiency, an advantage of the MessageFormat example over the String.format is that if logger.isLoggable(Level.FINER) is false, the MessageFormat example may do less work. It will create a new Object array with the values of transactionId and will call getName like the String.format example, but it may skip the string formatting.

MessageFormat may be a little heavier than String.format so your performance may be better if you used String.format, StringBuilder or String concatenation (+) in a loggable test if you're not logging FINER all the time. Especially if you have no localization needs in logging. Eg:



The SLF4J library encourages the parameterized logging and has a built-in formatter that is lighter than MessageFormat. They address performance for the not-logging case in their FAQ entry titled "What is the fastest way of (not) logging?". There are some long drawn-out discussions in the Hadoop project about testing if a logging level is enabled or not, like HADOOP-6884, that shed some more insights on this subject.

Jacob
 
 
subject: java.util.logging: MessageFormat-style parameters or String.format()/printf style
 
Similar Threads
Sun's Logger to log message in App Server log (SystemOut.log in WAS)
Java 5 logging in JSPs and Tomcat
How to stop the console Logging using Java.util.Logging
Log message in SystemOut.log in WAS5.1x using Sun's logging API
String.format vs java.text.MessageFormat