Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Column Value Dynamically JDBC Appender : Log4j2

I Have JDBC appender configured in log4j2 xml

<JDBC name="databaseAppender" tableName="TL_FX_LOG">
  <ConnectionFactory class="org.apache.jmeter.protocol.java.test.ConnectionFactory" method="getDatabaseConnection" />
  <Column name="LOG_DATE" isEventTimestamp="true" />
   <Column name="LOG_DESC" pattern="%message" />
      <Column name="LOG_Batch" literal="someproperty" />

I need to set the value of literal(LOG_Batch) from Java Code.

Logger.debug("hello");

hello will be passed as %message value .But if i have a variable

String log_batch="test";

how to pass the log_batch variable value to JDBC appender.

like image 780
saurabh goyal Avatar asked Feb 16 '26 12:02

saurabh goyal


1 Answers

Any pattern supported by PatternLayout can be used in a column pattern, so you could put that value in the ThreadContext map (ThreadContext.put("log_batch", "test");) in your code and insert it in the DB by configuring the appender with <Column name="LOG_Batch" pattern="%X{log_batch}" />.

like image 198
Remko Popma Avatar answered Feb 19 '26 01:02

Remko Popma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!