<div dir="ltr"><div><div>For more details:<br><br></div>Logback: <a href="https://logback.qos.ch/manual/mdc.html">https://logback.qos.ch/manual/mdc.html</a><br><br></div>Log4j: <a href="https://logging.apache.org/log4j/2.x/manual/thread-context.html">https://logging.apache.org/log4j/2.x/manual/thread-context.html</a><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Feb 25, 2017 at 1:01 PM, Elliot Huntington <span dir="ltr"><<a href="mailto:elliot.huntington@gmail.com" target="_blank">elliot.huntington@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I could be wrong, but I think you are looking for MDC.<br><br><a href="https://www.slf4j.org/manual.html" target="_blank">https://www.slf4j.org/manual.<wbr>html</a><br><br></div>See the section titled "Mapped Diagnostic Context (MDC) support"<br><br></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Sat, Feb 25, 2017 at 12:34 PM, Shanholtz, Jeff <span dir="ltr"><<a href="mailto:jeff.shanholtz@intel.com" target="_blank">jeff.shanholtz@intel.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
<div link="#0563C1" vlink="#954F72" lang="EN-US">
<div class="m_-5722184561745217473m_-8116745032076564464WordSection1">
<p class="MsoNormal">I have a scenario where I have 1, 2, or 3 pieces of data (guids) which I want to to log with all messages in order to track records as they traverse their way through a series of microservices.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Currently I’ve written my own wrapper class with my own debug, info, warn, error methods that take these 3 parameters, followed by the message field, followed by the optional parameters.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">There must be a better way, plus this has the disadvantage of rendering the line number useless in log messages because the line numbers are the line numbers from my log wrapper class, not the actual code doing the logging.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">What is the best way to handle this scenario? Googling has failed me, as has the FAQ, but it’s probably because I don’t know the right terms to search for. I’m using log4j2 as the underlying logger FWIW.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Here’s a simplified version of my log wrapper (i.e. most methods stripped out because they are redundant for the purpose of this question):<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal" style="background:white;text-autospace:none"><b><span style="font-size:9.0pt;font-family:"Courier New";color:navy">public class
</span></b><span style="font-size:9.0pt;font-family:"Courier New";color:black">Log {<br>
</span><b><span style="font-size:9.0pt;font-family:"Courier New";color:navy">private
</span></b><span style="font-size:9.0pt;font-family:"Courier New";color:black">Logger
</span><b><span style="font-size:9.0pt;font-family:"Courier New";color:#660e7a">logger</span></b><span style="font-size:9.0pt;font-family:"Courier New";color:black">;<br>
<br>
</span><b><span style="font-size:9.0pt;font-family:"Courier New";color:navy">public
</span></b><span style="font-size:9.0pt;font-family:"Courier New";color:black">Log(Class clazz) {<br>
</span><b><span style="font-size:9.0pt;font-family:"Courier New";color:#660e7a">logger
</span></b><span style="font-size:9.0pt;font-family:"Courier New";color:black">= LoggerFactory.<i>getLogger</i>(clazz)<wbr>;<br>
}<br>
<br>
</span><b><span style="font-size:9.0pt;font-family:"Courier New";color:navy">public void
</span></b><span style="font-size:9.0pt;font-family:"Courier New";color:black">info(Object xGuid, Object yGuid, String zGuid, String message, Object... args) {<br>
</span><b><span style="font-size:9.0pt;font-family:"Courier New";color:#660e7a">logger</span></b><span style="font-size:9.0pt;font-family:"Courier New";color:black">.info(guidifiedMessa<wbr>ge(xGuid, yGuid, zGuid, message), args);<br>
}<br>
}<u></u><u></u></span></p>
<p class="MsoNormal" style="background:white;text-autospace:none"><span style="font-size:9.0pt;font-family:"Courier New";color:black"><u></u> <u></u></span></p>
<p class="MsoNormal">The “guidifiedMessage” method just prefixes the message string with whatever guids are present.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">So is there a better way? Or if not, is there at least a way to get the line number specified in my log4j properties to reflect the caller of this Log class rather than the line
<i>within </i>the Log class?<u></u><u></u></p>
</div>
</div>
<br></div></div>______________________________<wbr>_________________<br>
slf4j-user mailing list<br>
<a href="mailto:slf4j-user@qos.ch" target="_blank">slf4j-user@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/slf4j-user" rel="noreferrer" target="_blank">http://mailman.qos.ch/mailman/<wbr>listinfo/slf4j-user</a><br></blockquote></div><br></div>
</blockquote></div><br></div>