springspring-bootlog4j2pattern-layout

How to put data in logs using pattern layout log4j2


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.


Solution

  • 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