1

I am using log4j 2 to log messages into the mySQL DB using JDBC appender. I want to make that process asynchronous because it should support large no of users to log the data into DB at the same time. I have to prevent lock contention. I have log4j.properties as follows:

log4j.rootLogger = DEBUG, DB

# Define the DB appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender

# Set JDBC URL
log4j.appender.DB.URL=jdbc:mysql://xxx.xxx.xxx.xxx:3306/test

# Set Database Driver
log4j.appender.DB.driver=com.mysql.jdbc.Driver

# Set database user name and password
log4j.appender.DB.user=root
log4j.appender.DB.password=root

# Set the SQL statement to be executed.
log4j.appender.DB.sql=INSERT INTO logs VALUES('%X{username}','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m')

# Define the layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
  1. What are the changes required in the above log4j.properties to behave asynchronously or create the corresponding log4j.xml?
  2. How can I configure the above properties to have a buffer size to do a batch insert into the database?
KFC
  • 541
  • 1
  • 4
  • 17
  • 1
    https://logging.apache.org/log4j/2.x/manual/appenders.html look at Async appender. You could wrap your jdbc appender in an Async appender – Indraneel Bende Feb 25 '18 at 02:49
  • As for your second question, there is a buffer size parameter . Please refer the above link – Indraneel Bende Feb 25 '18 at 02:52
  • @IndraneelBende It says to get the credentials. How can I put the credentials in the log4j.xml instead of putting the credentials into the java file? – KFC Feb 25 '18 at 02:53
  • why would u want to do that? Its easier in a java file. – Indraneel Bende Feb 25 '18 at 03:28
  • As to answer your question, the different options are documented here- https://logging.apache.org/log4j/2.0/manual/configuration.html#PropertySubstitution I prefer using bundle. – Indraneel Bende Feb 25 '18 at 03:29
  • https://stackoverflow.com/questions/38859509/how-to-make-log4j2-configurable-by-environment-using-spring-boot-1-3-6-release This is a good example for bundle. – Indraneel Bende Feb 25 '18 at 03:35

0 Answers0