0

I am using log4j2 jdbc appender for logging in my database. My log4j2 properties file currently is:

customLevel.DIAG =350 

# JDBC appender
appender.db.type = Jdbc
appender.db.name = databaseAppender
appender.db.tableName = db_name.test
appender.db.cf.type = ConnectionFactory
appender.db.cf.class = com.myproject.ConnectionFactory
appender.db.cf.method = getConnection
appender.db.col2.type = Column
appender.db.col2.name = message
appender.db.col2.pattern = %m
appender.db.col3.type = Column
appender.db.col3.name = category
appender.db.col3.pattern = %M{1}
appender.db.col4.type = Column
appender.db.col4.name = timestamp
appender.db.col4.isEventTimestamp = true
appender.db.col5.type = Column
appender.db.col5.name = log_level
appender.db.col5.pattern = %-5p
appender.db.filter.threshold.type = ThresholdFilter
appender.db.filter.threshold.level = diag
appender.db.filter.threshold.onMatch = Accept
appender.db.filter.threshold.onMismatch = Deny

The information gets successfully stored in database when I log my events using:

LOG.log(Level.forName("DIAG", 350), "a diagnostic message");

But now I have to store multiple kinds of messages (like quantity, ordered to whom) for which I have to make a model.

public class LogModel {

String quantity, orderedTo, amount;

public LogModel(String quantity, String orderedTo, String amount) {
    this.quantity = quantity;
    this.orderedTo = orderedTo;
    this.amount = amount;
}

public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public String getOrderedTo() {
    return orderedTo;
}

public void setOrderedTo(String orderedTo) {
    this.orderedTo = orderedTo;
}

public String getAmount() {
    return amount;
}

public void setAmount(String amount) {
    this.amount = amount;
}
} 

I have defined the column fields in my mysql database accordingly for the log. My question is how can I pass this model object to the jdbc appender so that it can map the data to my database column.

Another thing I tried (that didn't required making model) was using ThreadContext as:

ThreadContext.put("amount", amount);

And accordingly in log4j2 properties file:

appender.db.col3.type = Column
appender.db.col3.name = amount
appender.db.col3.pattern = %X{amount}

But is this the only option that I have? Like I have to clear the threadContext everytime right or else the value in threadcontext is working for another of my log. I am actually rooting to work with model object if possible. Something like this:

LogModel logModel = new LogModel("100", "Mr. John", "12345");
LOG.log(Level.forName("DIAG", 350), logModel);

Your guidance will be much appreciated. Thanks.

Ujjwal Jung Thapa
  • 604
  • 2
  • 8
  • 31
  • My first thought is try using a [MapMessage](https://logging.apache.org/log4j/2.x/manual/messages.html#MapMessage) – D.B. May 26 '19 at 16:18
  • @D.B. any idea on how to use MapMessage. I had known about it but couldn't find how to use it. – Ujjwal Jung Thapa May 27 '19 at 02:24
  • Please see sample code in [another answer of mine](https://stackoverflow.com/a/47235348/3284624) – D.B. May 28 '19 at 03:17

0 Answers0