<div dir="ltr">You can write your own log4j layout.</div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/5/25 Alexander Poulikakos <span dir="ltr"><<a href="mailto:alexander.poulikakos@ericsson.com" target="_blank">alexander.poulikakos@ericsson.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Hi<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">With category-name do you mean the name of the logger? Something like this?<u></u><u></u></span></p>
<p class="MsoNormal" style="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">
</span><b><span style="font-size:10.0pt;font-family:Consolas;color:#7f0055">class</span></b><span style="font-size:10.0pt;font-family:Consolas"> MyClass {</span><span style="font-size:10.0pt;font-family:Consolas"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">
</span><b><span style="font-size:10.0pt;font-family:Consolas;color:#7f0055">private</span></b><span style="font-size:10.0pt;font-family:Consolas"> Logger
</span><span style="font-size:10.0pt;font-family:Consolas;color:#0000c0">logger</span><span style="font-size:10.0pt;font-family:Consolas">;</span><span style="font-size:10.0pt;font-family:Consolas"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">
</span><b><span style="font-size:10.0pt;font-family:Consolas;color:#7f0055">public</span></b><span style="font-size:10.0pt;font-family:Consolas"> MyClass(String id) {</span><span style="font-size:10.0pt;font-family:Consolas"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">
</span><span style="font-size:10.0pt;font-family:Consolas;color:#0000c0">logger</span><span style="font-size:10.0pt;font-family:Consolas"> = LoggerFactory.<i>getLogger</i>(MyClass.</span><b><span style="font-size:10.0pt;font-family:Consolas;color:#7f0055">class</span></b><span style="font-size:10.0pt;font-family:Consolas">.getName()
+ </span><span style="font-size:10.0pt;font-family:Consolas;color:#2a00ff">"."</span><span style="font-size:10.0pt;font-family:Consolas"> + id);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"> }</span><span style="font-size:10.0pt;font-family:Consolas"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">}</span><span style="font-size:10.0pt;font-family:Consolas"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Then I can add something like this in my log4j.properties (or similar in logback.xml)<br>
</span><span style="font-size:10.0pt;font-family:Consolas">log4j.appender.A1.layout.ConversionPattern=</span><span style="font-size:10.0pt;font-family:Consolas;color:#2a00ff">%c</span><b><span style="font-size:10.0pt;font-family:Consolas;color:#7f0055">{1}</span></b><span style="font-size:10.0pt;font-family:Consolas">
</span><span style="font-size:10.0pt;font-family:Consolas;color:#2a00ff">%m</span><span style="font-size:10.0pt;font-family:Consolas">
</span><span style="font-size:10.0pt;font-family:Consolas;color:#2a00ff">%n</span><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Which works well for the logger in MyClass, but the problem now is that every other Loggers in my system (which are out of my control i.e. 3PP libraries) will
also be prefixed with something irrelevant. Anyway to avoid that?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">/Alex<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></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:<a href="mailto:slf4j-user-bounces@qos.ch" target="_blank">slf4j-user-bounces@qos.ch</a>]
<b>On Behalf Of </b>niels<br>
<b>Sent:</b> den 24 maj 2013 22:04</span></p><div><div class="h5"><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?<u></u><u></u></div></div><p></p><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">Well the simplest way is to put the prefix in the category-name and make the Logger as an instance-variable. The other solution, was to write an extension which provides special methods. <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><u></u> <u></u></p>
<div>
<p class="MsoNormal">2013/5/24 Alexander Poulikakos <<a href="mailto:alexander.poulikakos@ericsson.com" target="_blank">alexander.poulikakos@ericsson.com</a>><u></u><u></u></p>
<div>
<div>
<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><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span><u></u><u></u></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?</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">/Alex</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span><u></u><u></u></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:<a href="mailto:slf4j-user-bounces@qos.ch" target="_blank">slf4j-user-bounces@qos.ch</a>]
<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?</span><u></u><u></u></p>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></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.<u></u><u></u></p>
<div>
<p class="MsoNormal">Niels<u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"> <u></u><u></u></p>
<div>
<p class="MsoNormal">2013/5/23 Alexander Poulikakos <<a href="mailto:alexander.poulikakos@ericsson.com" target="_blank">alexander.poulikakos@ericsson.com</a>><u></u><u></u></p>
<div>
<div>
<p class="MsoNormal">Hi slf4j users<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">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.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">The below example is a simplified version. In reality there are plenty logger calls, many different types of nodes, different types of communication. Etc.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">I have tried using the MDC, but effectively, due to its static context, I need to surround each logger call with,:<u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:#0000c0">
logger</span>.info(command);</span><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> MDC.<i>remove</i>(<span style="color:#2a00ff">"id"</span>);</span><u></u><u></u></p>
<p class="MsoNormal">Which is not much different from prefixing each log message<u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:#0000c0">
logger</span>.info(id + “:” + command);</span><u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Any suggestions how to solve this?<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Regards,<u></u><u></u></p>
<p class="MsoNormal">Alex<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">===========================================<u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> n1.send(<span style="color:#2a00ff">"command1"</span>);</span><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> </span><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> n2.send(<span style="color:#2a00ff">"command2"</span>);</span><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> }</span><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas">}</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">===========================================<u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> </span><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:#3f7f5f">
// create connection to host</span></span><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> }</span><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> </span><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:#0000c0">
logger</span>.info(command);</span><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> <span style="color:#3f7f5f">
//send command to host</span></span><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> }</span><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas">}</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">===========================================<u></u><u></u></p>
<p class="MsoNormal"><br>
Using the following logback.xml<u></u><u></u></p>
<p class="MsoNormal">========================================<u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="text-autospace:none">
<span style="font-size:10.0pt;font-family:Consolas"> </span><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal" style="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><u></u><u></u></p>
<p class="MsoNormal"><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><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><br>
_______________________________________________<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" target="_blank">http://mailman.qos.ch/mailman/listinfo/slf4j-user</a><u></u><u></u></p>
</div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal"><br>
_______________________________________________<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" target="_blank">http://mailman.qos.ch/mailman/listinfo/slf4j-user</a><u></u><u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div></div></div>
</div>
<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><br></blockquote></div><br></div>