I need a log in log4j2 as below:
14:28:00.404 app_name="splunk sample app" method_name=main desc="sample log"
<PatternLayout pattern="%d app_name=\"%X{app_name}\" method_name=%M(%L) %m %n"/>
Pattern is failing because of double quotes. What is the escape character for double quate so the value of the key value pair is within double quotes in the log.
ThreadContext.put("app_name", "splunk sample app");
The PatternLayout class extends the abstract org. apache. log4j. Layout class and overrides the format() method to structure the logging information according to a supplied pattern.
The format characters used in log4j are, L- it is used to output the line number from where the logging request was processed or issued. m- It is used to output the application supplied message related to the logging event. p- It is used to output the priority of the logging event.
org.apache.log4j.helpers Formats a Date in the format "dd MMM yyyy HH:mm:ss,SSS" for example, "06 Nov 1994 15:49:37,459". Appends to sbuf the date in the format "dd MMM yyyy HH:mm:ss,SSS" for example, "06 Nov 1994 08:49:37,459".
In your pattern, simply replace the two \"
with "
(the XML entity for the "
character):
<PatternLayout pattern="%d app_name="%X{app_name}" method_name=%M(%L) %m %n"/>
It will then log what you want:
14:28:00.404 app_name="splunk sample app" method_name=main desc="sample log"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With