Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PatternLayout double quote escape character in log4j2

Tags:

java

log4j2

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");
like image 423
Sibish Avatar asked Jun 19 '14 22:06

Sibish


People also ask

What is PatternLayout in log4j2?

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.

Which of the following are format characters used in log4j?

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.

How do I change the date format in log4j?

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".


1 Answers

In your pattern, simply replace the two \" with &quot; (the XML entity for the " character):

<PatternLayout pattern="%d app_name=&quot;%X{app_name}&quot; 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"
like image 200
xav Avatar answered Sep 28 '22 09:09

xav