<style>
<!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:"Lucida Sans Unicode";
panose-1:2 11 6 2 3 5 4 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:#606420;
text-decoration:underline;}
pre
{margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Lucida Sans Unicode";
color:windowtext;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
{page:Section1;}
/* List Definitions */
@list l0
{mso-list-id:1772824051;
mso-list-type:hybrid;
mso-list-template-ids:1786779664 800360878 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;}
@list l0:level1
{mso-level-start-at:0;
mso-level-number-format:bullet;
mso-level-text:\F0B7;
mso-level-tab-stop:.5in;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Symbol;
mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Lucida Sans Unicode";}
@list l0:level2
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:1.0in;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Courier New";}
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
-->
</style><div class="Section1">
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">When we are using
log4j as underlying logger, sl4j actually does double checking of
'isErrorEnabled()' for every Log, if that LEVEL is
enabled.</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p><pre><font size="2" face="Courier New"><span style="font-size: 10pt;">public void error(String format, Object arg) {</span></font></pre>
<pre><font size="2" face="Courier New"><span style="font-size: 10pt;"> if (logger.isEnabledFor(Level.ERROR)) {</span></font></pre><pre><font size="2" face="Courier New"><span style="font-size: 10pt;"> String msgStr = MessageFormatter.format(format, arg);</span></font></pre>
<pre><font size="2" face="Courier New"><span style="font-size: 10pt;"> logger.log(FQCN, Level.ERROR, msgStr, null);</span></font></pre><pre><font size="2" face="Courier New"><span style="font-size: 10pt;"> }</span></font></pre>
<pre><font size="2" face="Courier New"><span style="font-size: 10pt;"> }</span></font></pre>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">This is because,
once we pass the message to log4j</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p><pre><font size="2" face="Courier New"><span style="font-size: 10pt;">logger.log(FQCN, Level.ERROR, msgStr, null);</span></font></pre>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">log4j library again
does the check isErrorEnabled() before the actual logging. This code is the same
as the isErrorEnabled check.</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><b><font size="2" color="#7f0055" face="Lucida Sans Unicode"><span style="font-weight: bold; font-size: 10pt; color: rgb(127, 0, 85); font-family: 'Lucida Sans Unicode';">public</span></font></b><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"></span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">
</span></font><b><font size="2" color="#7f0055" face="Lucida Sans Unicode"><span style="font-weight: bold; font-size: 10pt; color: rgb(127, 0, 85); font-family: 'Lucida Sans Unicode';">void</span></font></b><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">
log(String callerFQCN, Priority level, Object message, Throwable t)
{</span></font><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"></span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">
</span></font><b><font size="2" color="#7f0055" face="Lucida Sans Unicode"><span style="font-weight: bold; font-size: 10pt; color: rgb(127, 0, 85); font-family: 'Lucida Sans Unicode';">if</span></font></b><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">(</span></font><font size="2" color="#0000c0" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: rgb(0, 0, 192); font-family: 'Lucida Sans Unicode';">repository</span></font><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">.isDisabled(level.</span></font><font size="2" color="#0000c0" face="Lucida Sans Unicode"><span style="background: silver none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(0, 0, 192); font-family: 'Lucida Sans Unicode';">level</span></font><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">))
{</span></font><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"></span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">
</span></font><b><font size="2" color="#7f0055" face="Lucida Sans Unicode"><span style="font-weight: bold; font-size: 10pt; color: rgb(127, 0, 85); font-family: 'Lucida Sans Unicode';">return</span></font></b><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">;</span></font><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"></span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">
}</span></font><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"></span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">
</span></font><b><font size="2" color="#7f0055" face="Lucida Sans Unicode"><span style="font-weight: bold; font-size: 10pt; color: rgb(127, 0, 85); font-family: 'Lucida Sans Unicode';">if</span></font></b><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">(level.isGreaterOrEqual(</span></font><b><font size="2" color="#7f0055" face="Lucida Sans Unicode"><span style="font-weight: bold; font-size: 10pt; color: rgb(127, 0, 85); font-family: 'Lucida Sans Unicode';">this</span></font></b><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">.getEffectiveLevel()))
{</span></font><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"></span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">
forcedLog(callerFQCN, level, message, t);</span></font><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"></span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">
}</span></font><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"></span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">
}</span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">To
avoid this double checking why don't we wrap the format, arguments inside an
Object, and use log4j renderer for that object to generate the final message? Or
even override toString() method of that object to return the formatted message.
</span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">Instead
of this: </span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="3" face="Times New Roman"><span style="font-size: 12pt;">logger.log(FQCN, Level.ERROR, msgStr,
null);</span></font><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';"></span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';">It
could be something like:</span></font></p>
<p class="MsoNormal"><font size="2" color="black" face="Lucida Sans Unicode"><span style="font-size: 10pt; color: black; font-family: 'Lucida Sans Unicode';"> </span></font></p><pre><font size="2" face="Courier New"><span style="font-size: 10pt;">public void error(String format, Object arg) {</span></font></pre>
<pre><font size="2" face="Courier New"><span style="font-size: 10pt;"> logger.log(FQCN, Level.ERROR, new InterpolatedMessage(formate, arg), null);</span></font></pre><pre><font size="2" face="Courier New"><span style="font-size: 10pt;">}</span></font></pre>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">Where</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">public final class
InterpolatedMessage {</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> private
final String format;</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> private
final Object arg;</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> public
InterpolatedMessage(final String format, final Object arg)
{</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">
this.format = format;</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">
this.arg = arg;</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">
}</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">
</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">
@Override</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> public
String toString() {</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">
return MessageFormatter.format(this.format,
this.arg);</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">
}</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">
</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">}</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">As you see, this
will </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<ul style="margin-top: 0in;" type="disc"><li class="MsoNormal" style=""><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">Delay the message
construction unless it really going to be used.</span></font>
</li><li class="MsoNormal" style=""><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">Avoid double
checking with if LevelEnabled, when that level is actually
enabled</span></font> </li></ul>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">The negatives
are</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<ul style="margin-top: 0in;" type="disc"><li class="MsoNormal" style=""><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">Creation of
InterpolatedMessage Object for every log statement – since the object is just
going to hold the reference of the format and args, this may not be
expensive?</span></font>
<ul style="margin-top: 0in;" type="circle"><li class="MsoNormal" style=""><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">as it avoids
construction of log message </span></font></li></ul>
</li><li class="MsoNormal" style=""><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">Using toString()
method of interpolated message is a trick! - Yes, for that we can otherwise add
log4j renderer for objects of InterpolatedMessage type.</span></font>
</li></ul>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">I just gave my
thoughts here; let me know what you guys think. I could follow up or help to
verify if this will really gives us performance
advantage.</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';"> </span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">Regards,</span></font></p>
<p class="MsoNormal"><font size="2" face="Lucida Sans Unicode"><span style="font-size: 10pt; font-family: 'Lucida Sans Unicode';">Srini</span></font></p></div>