How to put data in logs using pattern layout log4j2

328 Views Asked by At

I am defining log4j2 configuration in log4j2-spring.xml. In this i am defining pattern layout as following:

        <Property name="LOG_PATTERN" value="%d [%thread] [%-5level] 
               [Content-Id:%X{CID}] [SessionId:%X{sessionId}] 
 [CustomerId:%X{customerId}] [%c{1.}] - %msg%n" />

Now i want to put data dynamically in customer ID but I don't know how to do this.

Any help is appreciated.

1

There are 1 best solutions below

1
Jens On BEST ANSWER

As you can read in the log4j2 documentation, put the customerId into the ThreadContext

ThreadContext.put("customerId", <your customerID>); 

And the placeholder will be replaced by log4j2