log4j2 JDBC管理器无法连接到数据库 [英] log4j2 JDBC manager cannot connect to the database
本文介绍了log4j2 JDBC管理器无法连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用mysql配置log4j v2,但它返回此错误:
I'm trying to configure log4j v2 with mysql, but it returns this error:
2014-08-01 15:35:24,819 ERROR Unable to write to database [jdbcManager{ description=databaseAppender, bufferSize=0, connectionSource=factory{ public static java.sql.Connection it.prisma.presentationlayer.webui.ConnectionFactory.getDatabaseConnection() }, tableName=logs, columns=[ { name=message, layout=%message, literal=null, timestamp=false } ] }] for appender [databaseAppender]. org.apache.logging.log4j.core.appender.AppenderLoggingException: Cannot write logging event or flush buffer; JDBC manager cannot connect to the database.
Caused by: java.sql.SQLException: Failed to obtain connection from factory method.
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://10.41.1.68:3306/test
我的配置类似于 doc ,唯一的区别:
My configuration is similar to doc, with the only difference of this:
new PoolableConnectionFactory(connectionFactory, pool, null, "SELECT 1", false, false, Connection.TRANSACTION_READ_COMMITTED);
我认为我的tomcat7配置正确,因为我可以使用log4j v1登录.
I think that my tomcat7 is well configured because I can log with log4j v1.
推荐答案
好的解决方案是:
1-创建一个Connection Factory类
1- create a Connection Factory class
import java.sql.Connection;
import java.sql.SQLException;
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;
public class ConnectionFactory {
private static interface Singleton {
final ConnectionFactory INSTANCE = new ConnectionFactory();
}
private final DataSource dataSource;
private ConnectionFactory() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(0);
}
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "root");
GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>();
DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
"jdbc:mysql://example.org:3306/exampleDb", properties
);
new PoolableConnectionFactory(connectionFactory, pool, null, "SELECT 1", 3, false, false, Connection.TRANSACTION_READ_COMMITTED);
this.dataSource = new PoolingDataSource(pool);
}
public static Connection getDatabaseConnection() throws SQLException {
return Singleton.INSTANCE.dataSource.getConnection();
}
}
2-在src/main/resources中创建一个名为log4j2.xml的配置文件
2- create a configuration file into src/main/resources called log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<JDBC name="databaseAppender" tableName="logs">
<ConnectionFactory class="my.pack.ConnectionFactory"
method="getDatabaseConnection" />
<!-- <Column name="id" literal="LOGGING.APPLICATION_LOG_SEQUENCE.NEXTVAL" /> -->
<Column name="date" isEventTimestamp="true" />
<Column name="level" pattern="%level" />
<Column name="message" pattern="%message" />
<Column name="class" pattern="%class" />
</JDBC>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console" />
<AppenderRef ref="databaseAppender" />
</Root>
</Loggers>
</Configuration>
3-创建一个像这样的表:
3- create a table like this:
4-将mysql连接器添加到{tomcat}/bin
4- add mysql connector to {tomcat}/bin
Maven提示:
<commons-dbcp.version>1.4</commons-dbcp.version>
<commons-pool.version>1.6</commons-pool.version>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>${commons-pool.version}</version>
</dependency>
这篇关于log4j2 JDBC管理器无法连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文