Thanks for response :)

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?

/Alex

From: slf4j-user [mailto:[email protected]] On Behalf Of niels
Sent: den 24 maj 2013 11:53
To: User list for the slf4j project
Subject: Re: [slf4j-user] How to prefix log messages for individual Loggers?

If your nodes run in different threads you can use MDC. It is a ThreadLocal. 
Otherwise you need an own logger.
Niels

2013/5/23 Alexander Poulikakos 
<[email protected]<mailto:[email protected]>>
Hi slf4j users

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.

The below example is a simplified version. In reality there are plenty logger 
calls, many different types of nodes, different types of communication. Etc.

I have tried using the MDC, but effectively, due to its static context,  I need 
to surround each logger call with,:
              MDC.put("id", id);
              logger.info(command);
              MDC.remove("id");
Which is not much different from prefixing each log message
              logger.info(id + ":" + command);

Any suggestions how to solve this?

Regards,
Alex

===========================================
package demo;
public class Demo {
       public static void main(String[] args) {
              Node n1 = new Node("host.com<http://host.com>", "id1");
              n1.send("command1");

              Node n2 = new Node("anotherhost.com<http://anotherhost.com>", 
"id2");
              n2.send("command2");
       }
}
===========================================
package demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Node {
       private Logger logger = LoggerFactory.getLogger(Node.class);
       private String id;

       public Node(String host, String id) {
              this.id = id;
              // create connection to host
       }

       public void send(String command){
              logger.info(command);
              //send command to host
       }
}
===========================================

Using the following logback.xml
========================================
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} %X{id} %msg%n</pattern>
    </encoder>
  </appender>

  <root level="INFO">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

_______________________________________________
slf4j-user mailing list
[email protected]<mailto:[email protected]>
http://mailman.qos.ch/mailman/listinfo/slf4j-user

_______________________________________________
slf4j-user mailing list
[email protected]
http://mailman.qos.ch/mailman/listinfo/slf4j-user

Reply via email to