[slf4j-user] How to prefix log messages for individual Loggers?

Alexander Poulikakos alexander.poulikakos at ericsson.com
Sat May 25 11:03:04 CEST 2013


Hi

With category-name do you mean the name of the logger? Something like this?
public class MyClass {
       private Logger logger;
       public MyClass(String id) {
              logger = LoggerFactory.getLogger(MyClass.class.getName() + "." + id);
       }
}
Then I can add something like this in my log4j.properties (or similar in logback.xml)
log4j.appender.A1.layout.ConversionPattern=%c{1} %m %n
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?

/Alex


From: slf4j-user [mailto:slf4j-user-bounces at qos.ch] On Behalf Of niels
Sent: den 24 maj 2013 22:04
To: User list for the slf4j project
Subject: Re: [slf4j-user] How to prefix log messages for individual Loggers?

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.

2013/5/24 Alexander Poulikakos <alexander.poulikakos at ericsson.com<mailto:alexander.poulikakos at ericsson.com>>
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:slf4j-user-bounces at qos.ch<mailto:slf4j-user-bounces at qos.ch>] 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 <alexander.poulikakos at ericsson.com<mailto:alexander.poulikakos at ericsson.com>>
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
slf4j-user at qos.ch<mailto:slf4j-user at qos.ch>
http://mailman.qos.ch/mailman/listinfo/slf4j-user


_______________________________________________
slf4j-user mailing list
slf4j-user at qos.ch<mailto:slf4j-user at qos.ch>
http://mailman.qos.ch/mailman/listinfo/slf4j-user

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/slf4j-user/attachments/20130525/b9234d0b/attachment-0001.html>


More information about the slf4j-user mailing list