Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse message for a log4j2 JDBC Appender

Tags:

java

log4j2

In my application, I use a pre determined log messages, something like this:

MESSAGE_ID PROCESS_ID This process is running

I would like to use a JDBC Appender and split my message in three, to get put each part in a particular SQL column.

Is this possible? And How?

Thanks a lot!

like image 709
Eric Avatar asked Oct 25 '25 22:10

Eric


1 Answers

One idea is to use the ThreadContext to carry the message ID and process ID, and declare jdbc columns for each item like this:

<Column name="MESSAGE" pattern="%message" />
<Column name="MESSAGE_ID" pattern="%X{messageID}" />
<Column name="PROCESS_ID" pattern="%X{processID}" />

In your code you set the values like this:

ThreadContext.put("messageID", UUID.randomUUID().toString();
ThreadContext.put("processID", getProcessId());
...
logger.debug("this process is running");
...
like image 186
Remko Popma Avatar answered Oct 28 '25 14:10

Remko Popma