vinod hy
vinod hy

Reputation: 895

logback encoder pattern with custom string values

Below is my current logback encoder pattern,

<encoder>
            <pattern>%d{yyyy.MM.dd HH:mm:ss} | %-5level | [%thread] | \(%class{25}:%line\) - %msg%n</pattern>
</encoder>

Eg: 2017-06-05 11:15:21 | DEBUG | [Thread-10] | (c.s.n.f.s.m.NmsngMessagingImpl:450) - Control reached send()

in java classes, an example of logger is as shown below,

logger.info("request received for the message " + messRecv);

The above pattern works fine for the example shown.

Now i have another example as shown below,

[timestamp][loglevel][systemid][package][class][method][id]message

systemId and id are new entries which are custom values.

May i know how to pass them through logger.

Upvotes: 0

Views: 2460

Answers (1)

sheltem
sheltem

Reputation: 3825

You can add them to the MDC:

MDC.put("systemId", "value");
MDC.put("id", "value2");

The MDC (mapped diagnostic context) is a map of key-value pairs that are managed on a per thread basis.

They can be referenced in the configuration via %X{mdcName}, so your pattern would look like this:

<pattern>%d{yyyy.MM.dd HH:mm:ss} | %-5level | %X{systemId} ... %msg%n</pattern>

%X{systemId} will be replaced with the thread's value for "systemId" in the MDC.

Upvotes: 3

Related Questions