<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Thanks for response
</span><span style="font-size:11.0pt;font-family:Wingdings;color:#1F497D">J</span><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">They sometimes run in separate threads and sometimes in same thread. With “own” logger you mean each instance should have its own unique logger? If an own logger
is used, how to prefix in best way?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">/Alex<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> slf4j-user [mailto:slf4j-user-bounces@qos.ch]
<b>On Behalf Of </b>niels<br>
<b>Sent:</b> den 24 maj 2013 11:53<br>
<b>To:</b> User list for the slf4j project<br>
<b>Subject:</b> Re: [slf4j-user] How to prefix log messages for individual Loggers?<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">If your nodes run in different threads you can use MDC. It is a ThreadLocal. Otherwise you need an own logger.<o:p></o:p></p>
<div>
<p class="MsoNormal">Niels<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><o:p> </o:p></p>
<div>
<p class="MsoNormal">2013/5/23 Alexander Poulikakos <<a href="mailto:alexander.poulikakos@ericsson.com" target="_blank">alexander.poulikakos@ericsson.com</a>><o:p></o:p></p>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Hi slf4j users<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">I have a class that communicates (i.e. sending commands) with remote nodes. I can create multiple instances of this class, where each instance communicates with a specific host.
I use a Logger object to log each command. My problem is that the command does not show which node it is sent to. What would be the best way to prefix the log message with an id that identifies which node is the receiver of a command? I’m trying to avoid “manually”
prefixing the log message for each logger call.<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">The below example is a simplified version. In reality there are plenty logger calls, many different types of nodes, different types of communication. Etc.<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">I have tried using the MDC, but effectively, due to its static context, I need to surround each logger call with,:<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> MDC.<i>put</i>(<span style="color:#2A00FF">"id"</span>,
<span style="color:#0000C0">id</span>);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:#0000C0">
logger</span>.info(command);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> MDC.<i>remove</i>(<span style="color:#2A00FF">"id"</span>);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Which is not much different from prefixing each log message<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:#0000C0">
logger</span>.info(id + “:” + command);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Any suggestions how to solve this?<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Regards,<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Alex<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">===========================================<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<b><span style="font-size:10.0pt;font-family:Consolas;color:#7F0055">package</span></b><span style="font-size:10.0pt;font-family:Consolas"> demo;</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<b><span style="font-size:10.0pt;font-family:Consolas;color:#7F0055">public</span></b><span style="font-size:10.0pt;font-family:Consolas">
<b><span style="color:#7F0055">class</span></b> Demo {</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <b><span style="color:#7F0055">public</span></b>
<b><span style="color:#7F0055">static</span></b> <b><span style="color:#7F0055">void</span></b> main(String[] args) {</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> Node n1 = <b><span style="color:#7F0055">new</span></b> Node(<span style="color:#2A00FF">"<a href="http://host.com" target="_blank">host.com</a>"</span>,
<span style="color:#2A00FF">"id1"</span>);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> n1.send(<span style="color:#2A00FF">"command1"</span>);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> Node n2 = <b><span style="color:#7F0055">new</span></b> Node(<span style="color:#2A00FF">"<a href="http://anotherhost.com" target="_blank">anotherhost.com</a>"</span>,
<span style="color:#2A00FF">"id2"</span>);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> n2.send(<span style="color:#2A00FF">"command2"</span>);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> }</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas">}</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;margin-bottom:12.0pt">===========================================<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<b><span style="font-size:10.0pt;font-family:Consolas;color:#7F0055">package</span></b><span style="font-size:10.0pt;font-family:Consolas"> demo;</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<b><span style="font-size:10.0pt;font-family:Consolas;color:#7F0055">import</span></b><span style="font-size:10.0pt;font-family:Consolas"> org.slf4j.Logger;</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<b><span style="font-size:10.0pt;font-family:Consolas;color:#7F0055">import</span></b><span style="font-size:10.0pt;font-family:Consolas"> org.slf4j.LoggerFactory;</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<b><span style="font-size:10.0pt;font-family:Consolas;color:#7F0055">public</span></b><span style="font-size:10.0pt;font-family:Consolas">
<b><span style="color:#7F0055">class</span></b> Node {</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <b><span style="color:#7F0055">private</span></b> Logger
<span style="color:#0000C0">logger</span> = LoggerFactory.<i>getLogger</i>(Node.<b><span style="color:#7F0055">class</span></b>);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <b><span style="color:#7F0055">private</span></b> String
<u><span style="color:#0000C0">id</span></u>;</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <b><span style="color:#7F0055">public</span></b> Node(String host, String id) {</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <b><span style="color:#7F0055">this</span></b>.<span style="color:#0000C0">id</span> = id;</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:#3F7F5F">
// create connection to host</span></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> }</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <b><span style="color:#7F0055">public</span></b>
<b><span style="color:#7F0055">void</span></b> send(String command){</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:#0000C0">
logger</span>.info(command);</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:#3F7F5F">
//send command to host</span></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> }</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas">}</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;margin-bottom:12.0pt">===========================================<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><br>
Using the following logback.xml<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">========================================<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas;color:teal"><</span><span style="font-size:10.0pt;font-family:Consolas;color:#3F7F7F;background:silver">configuration</span><span style="font-size:10.0pt;font-family:Consolas;color:teal">></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:teal"><</span><span style="color:#3F7F7F">appender</span>
<span style="color:#7F007F">name</span>=<i><span style="color:#2A00FF">"STDOUT"</span></i>
<span style="color:#7F007F">class</span>=<i><span style="color:#2A00FF">"ch.qos.logback.core.ConsoleAppender"</span></i><span style="color:teal">></span></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> </span><span lang="SV" style="font-size:10.0pt;font-family:Consolas;color:teal"><</span><span lang="SV" style="font-size:10.0pt;font-family:Consolas;color:#3F7F7F">encoder</span><span lang="SV" style="font-size:10.0pt;font-family:Consolas;color:teal">></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span lang="SV" style="font-size:10.0pt;font-family:Consolas"> <span style="color:teal">
<</span><span style="color:#3F7F7F">pattern</span><span style="color:teal">></span>%d{HH:mm:ss.SSS} %X{id} %<u>msg</u>%n<span style="color:teal"></</span><span style="color:#3F7F7F">pattern</span><span style="color:teal">></span></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span lang="SV" style="font-size:10.0pt;font-family:Consolas"> </span><span style="font-size:10.0pt;font-family:Consolas;color:teal"></</span><span style="font-size:10.0pt;font-family:Consolas;color:#3F7F7F">encoder</span><span style="font-size:10.0pt;font-family:Consolas;color:teal">></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:teal"></</span><span style="color:#3F7F7F">appender</span><span style="color:teal">></span></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:teal"><</span><span style="color:#3F7F7F">root</span>
<span style="color:#7F007F">level</span>=<i><span style="color:#2A00FF">"INFO"</span></i><span style="color:teal">></span></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:teal">
<</span><span style="color:#3F7F7F">appender-ref</span> <span style="color:#7F007F">
ref</span>=<i><span style="color:#2A00FF">"STDOUT"</span></i> <span style="color:teal">
/></span></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:teal"></</span><span style="color:#3F7F7F">root</span><span style="color:teal">></span></span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:10.0pt;font-family:Consolas;color:teal"></</span><span style="font-size:10.0pt;font-family:Consolas;color:#3F7F7F;background:silver">configuration</span><span style="font-size:10.0pt;font-family:Consolas;color:teal">></span><o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><br>
_______________________________________________<br>
slf4j-user mailing list<br>
<a href="mailto:slf4j-user@qos.ch">slf4j-user@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/slf4j-user" target="_blank">http://mailman.qos.ch/mailman/listinfo/slf4j-user</a><o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</body>
</html>