Jman Jman - 1 year ago 138
Java Question

How to configure logback to append special prefix for each object?

I have a class that takes string parameter in its constructor which represents it.

And i want to append this string as prefix at log messages. How can I do it in logback ?

For example;

MyClass classA = new MyClass("0999445454");

MyClass classB = new MyClass("0999446464");


The method of MyClass ;

public void doSomething() {"Doing Something...");

//Do something

So i want to see log messages like that ;

[INFO] [0999445454]: Doing Something...
[INFO] [0999446464]: Doing Something...

xav xav
Answer Source

I don't know how you currently instantiate your logger, but you can use your "string parameter which represents" your object to name this logger:

logger = LoggerFactory.getLogger("0999445454");

For example, if this string identifier is stored in an attribute id, you can do that:

Logger logger = null;

public MyClass(String id) {
    logger = org.slf4j.LoggerFactory.getLogger(id);
    // ...

public void doSomething()  {"Doing Something...");
    // Do something

You will then need to use the following Pattern in your Logback configuration file:

<appender name="..." class="...Appender">
        <pattern>[%level] [%logger]: %msg%n</pattern>

Because %logger is the logger name, which is the parameter given to org.slf4j.LoggerFactory.getLogger()

If you want to avoid creating a Logger for each ID, you can also use Mapped Diagnostic Context (MDC):

logger = LoggerFactory.getLogger(MyClass.class);
org.slf4j.MDC.put("id", "your ID");"Doing Something...");

You shall then add field %mdc in your Pattern (i.e. Logback configuration file):

<pattern>[%level] [%mdc{id}]: %msg%n</pattern>

So you shall call MDC.put() before a log each time you want to log with a different ID. What you could do in your case is something like:

public class MyClass {
    private Logger logger = LoggerFactory.getLogger(MyClass.class);
    private final String id;
    private static final Object MDC_MUTEX = new Object();

    public MyClass(String id) { = id;
        // ...

    public void doSomething()  {
        log("Doing Something...");
        // Do something...

    private void log(String message) {
        synchronized(MDC_MUTEX) {
            MDC.put("id", id);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download