How to add customized parameters to Log4j2’s JSONLAYOUT?
Also is there a way to add pattern to the JSONLAYOUT’s message element?
I have tried the options listed here ->
logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout
Please help!
How to add customized parameters to Log4j2’s JSONLAYOUT?
Also is there a way to add pattern to the JSONLAYOUT’s message element?
I have tried the options listed here ->
logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout
Please help!
As I understand it you're looking for a way to customize the format of the JSON output from the
JSONLayout
in a manner similar to how you can customize thePatternLayout
by specifying "Conversion Patterns".I believe the answer is that you can't customize the
JSONLayout
in the same manner. You can select the various pieces of information you want to be included in the message. For example, the documentation shows parameters likeproperties
:So you can set various parameters to include certain kinds of information, but you don't have the direct control over the specific items that are included.
What you could do instead is to use
ObjectMessage
along with a JSON library to generate a JSON message. However, this would generate JSON within JSON (assuming you still wish to useJSONLayout
with this approach). Here is some sample code to illustrate:A class with a main method to generate a log message:
The log4j2.xml config file:
The output from the above:
As you can see, the message name value pair has a value that is a JSON string. In order to parse this you would have to parse the outer object as JSON, pull the message field and then parse its value as JSON as well.
However, if you use a different layout such as a very basic
PatternLayout
like this:<PatternLayout pattern="%m%n"/>
You will be able to generate just one level of JSON output and therefore only have to parse once. However, you would have to write your logic to obtain all of the data that you need in your message and stuff it into your map (and JSON object) because now you're simply dumping the contents of the map.
Sample output using same java code with the layout changed to
PatternLayout
as described above:{"myKey":"myValue"}
EDIT:
You could even do something like the following if you want to use the "Conversion Patterns" of
PatternLayout
, output logs in JSON format, and not have to write the logic to obtain some of the specifics:Sample output:
Solution is to explicitly add log4j2’s 2.10.0 version. This version supports custom parameter in JSONLayout!