log4j2日志记录到oracle数据库
1、下载包:jdk1.7 版本用比较老的
commons-dhcp-1.4.jar:https://dlcdn.apache.org//commons/dbcp/binaries/commons-dbcp-1.4-bin.zip
commons-pool-1.6.jar:https://dlcdn.apache.org//commons/pool/binaries/commons-pool-1.6-bin.zip
官网:
https://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender
2、添加到项目中
3.准备连接工厂:ConnectionFactory
package com.lm.tech.schedulejob.logs;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnection;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;
import cn.hutool.core.codec.Base64;
import com.lm.tech.schedulejob.utils.YWDBUtils;//数据库的连接类,
/**
* 日志的连接池
* @author DH03
*
*/
public class ConnectionFactory {
private static interface Singleton {
final ConnectionFactory INSTANCE = new ConnectionFactory();
}
private DataSource dataSource;
private ConnectionFactory() {
try{
Properties properties = new Properties();
Map<String, String> map=YWDBUtils.getLogDBMap();//获取配置项的map
properties.setProperty("user", map.get("user"));
properties.setProperty("password", Base64.decodeStr(map.get("pwd"))); // or get properties from some configuration file
GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>();
DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
YWDBUtils.getLogDBUrl(),//获取配置项的的url
properties
);
new PoolableConnectionFactory(
connectionFactory, pool, null, "SELECT 1", 3, false, false, Connection.TRANSACTION_READ_COMMITTED
);
this.dataSource = new PoolingDataSource(pool);
}catch(Exception e){
e.printStackTrace();
this.dataSource = null;
}
}
public static Connection getDatabaseConnection() throws SQLException {