Quote dynamic text when logging

Extraneous leading or trailing whitespace around text is easy to add and difficult to detect - a bad combination. If text has some logical meaning to the program (for example, the name of a configuration parameter), then extraneous whitespace around it is almost always undesired.

(This problem occurs for text data, but not for numeric data such as BigDecimal or Integer.)

To help detect such unwanted whitespace, simply quote dynamic text values when logging. It's likely a good idea to define a simple utility method for this purpose.


This example uses String.valueOf(Object):

public final class Util {

  * Surround the result of <tt>String.valueOf(aObject)</tt> with single quotes.
  public static String quote(Object aObject){
    return "'" + String.valueOf(aObject) + "'";


See Also :
Logging messages
Would you use this technique?
Yes   No   Undecided   
© 2018 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on September 21, 2013.
Over 2,000,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -