Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Another good logging API is SLF4J. It can be configured to also intercept log APIs for Log4J, Java Util Logging, and Jakarta Commons Logging. And it can also be configured to <em>use</em> various logging implementations, including Log4J, Logback, Java Util Logging, and one or two others. This gives it enormous flexibility. It was developed by the author of Log4J to be its successor.</p> <p>Of relevance to this question, the SLF4J API has a mechanism to concatenate string valued expressions into a log message. The following calls are equivalent, but the second is about 30x faster to process if you're not outputting debug level messages, since the concatenation is avoided:</p> <pre><code>logger.debug("The new entry is " + entry + "."); logger.debug("The new entry is {}.", entry); </code></pre> <p>There's a two argument version too:</p> <pre><code>logger.debug("The new entry is {}. It replaces {}.", entry, oldEntry); </code></pre> <p>And for more than two you can pass in an array of Object like this:</p> <pre><code>logger.debug("Value {} was inserted between {} and {}.", new Object[] {newVal, below, above}); </code></pre> <p>This is a nice terse format that eliminates clutter. </p> <p>Example source is from the <a href="http://www.slf4j.org/faq.html" rel="nofollow noreferrer">SLF4J FAQ</a>.</p> <p>Edit: Here's a possible refactoring of your example:</p> <pre><code>try { doSomething(someObject.getValue()); } catch (BadThingsHappenException bthe) { throw new UnhandledException( MessageFormatter.format("An error occurred when setting value. [value={}]", someObject.getValue()), bthe); } </code></pre> <p>Or if this pattern occurs more than a few places you could write a set of static methods that capture the commonality, something like:</p> <pre><code>try { doSomething(someObject.getValue()); } catch (BadThingsHappenException bthe) { throwFormattedException(logger, bthe, "An error occurred when setting value. [value={}]", someObject.getValue())); } </code></pre> <p>and of course the method would also put the formatted message out on the logger for you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload