0

如何在 log4j2 中连接 jdbc appender???? 我是 log4j2.0 的新手,有人帮我解决这个问题吗?

    <?xml version="1.0" encoding="UTF-8"?>
<configuration status="error">
  <appenders>
    <Jdbc name="databaseAppender" tableName="application_log">
      <DriverManager url="jdbc:oracle:thin:@//localhost:1521/XE" username="system" password="system" />
      <Column name="level" pattern="%level" />
      <Column name="logger" pattern="%logger" />
      <Column name="message" pattern="%message" />
    </Jdbc>
  </appenders>
  <loggers>
    <root level="warn">
      <appender-ref ref="databaseAppender"/>
    </root>
  </loggers>
</configuration
4

3 回答 3

2

您的配置看起来不错。(类似于文档http://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender

问题是什么?你有错误吗?您有错误代码或堆栈跟踪吗?

如果您没有收到错误但没有任何反应,您可以尝试<configuration status="trace"在配置文件中进行设置。这会将 log4j2 内部日志消息输出到控制台,并可能有助于诊断问题。

于 2013-07-21T22:35:40.300 回答
1

尝试检查 Log4j2 的版本。

2.11 版开始,该库支持 DriverManagerConnectionSource -“一种快速而肮脏的起步方式,没有连接池”。

举个例子:

<JDBC name="databaseAppender" tableName="application_log">
     <DriverManager connectionString="jdbc:oracle:thin:@//localhost:1521/XE" 
        driverClassName="oracle.jdbc.driver.OracleDriver"
        username="system" 
        password="system" />
    <Column name="level" pattern="%level" />
    <Column name="logger" pattern="%logger" />
    <Column name="message" pattern="%message" />
</JDBC>
于 2019-03-06T15:18:11.897 回答
0

我认为我们不能在 JDBC appender 中使用 DriverManager。您需要使用 JNDI 名称获取连接或创建具有返回连接对象的静态方法的 connectionFactory 类。

以下链接将为您提供帮助。 http://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender

这是您修改后的代码,我认为它应该可以工作。

    <?xml version="1.0" encoding="UTF-8"?>
<configuration status="error">
  <appenders>
    <Jdbc name="databaseAppender" tableName="application_log">
      <ConnectionFactory class="net.example.db.ConnectionFactory" method="getDatabaseConnection" />
      <Column name="level" pattern="%level" />
      <Column name="logger" pattern="%logger" />
      <Column name="message" pattern="%message" />
    </Jdbc>
  </appenders>
  <loggers>
    <root level="warn">
      <appender-ref ref="databaseAppender"/>
    </root>
  </loggers>
</configuration

这是 ConnectionFactory.java 代码。您需要根据您的数据库更改用户、通行证和连接。

package net.example.db;

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() {
        Properties properties = new Properties();
        properties.setProperty("user", "logging");
        properties.setProperty("password", "abc123"); // or get properties from some configuration file

        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();
    }
}

这对我有用。它也应该对你有用。

于 2014-03-27T06:01:52.953 回答