Java Language log4j / log4j2 Properties-File to log to DB

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


For this example to work you'll need a JDBC driver compatible to the system the database is running on. An opensource one that allows you to connect to DB2 databases on an IBM System i can be found here: JT400

Even though this example is DB2 specific, it works for almost every other system if you exchange the driver and adapt the JDBC URL.

# Root logger option
log4j.rootLogger= ERROR, DB

# Redirect log messages to a DB2
# Define the DB appender   

# Set JDBC URL (!!! adapt to your target system !!!)

# Set Database Driver (!!! adapt to your target system !!!)

# Set database user name and password

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

# Define the layout for file appender

Got any Java Language Question?