I would like to log log4j2 messages into relational database.
Documentation for JDBC appender is here. I can use as database connection provider these sources:
- ConnectionFactory
- DataSource
- DriverManager
but is there some way how to use datasource bean (com.jolbox.bonecp.BoneCPDataSource) what we use in entire application?
You can do it in 4 steps (step 2 & 3 are log4j2 config for web app):
Step 1: Create log4j2.xml file (without JDBC appender) and put it in WEB-INF folder
Step 2 : Add log4j2-web-2.x.jar to WEB-INF/lib.
Here is the maven repository, you can download the jar from there or copy paste the dependency to your pom.xml if your using maven.
Step 3 : Configure web.xml depending on the servlet api version
See log4j2 documentation for more details. Here is how you configure log4j in a servlet 2.5 web app.
Step 4: Create a spring bean , inject your DataSource bean , and add JDBC Appender configuration dynamically in @PostConstruct method
Here is the code sample ( tested with log4j 2.1 and spring 3.5)
** Limitations : link to doc**