JDBC附加程序的数据源

JDBC附加程序的数据源

本文介绍了如何使用Spring BoneCPDataSource bean作为Log4j 2 JDBC附加程序的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将log4j2消息记录到关系数据库中.

I would like to log log4j2 messages into relational database.

有关JDBC附加程序的文档,请此处.我可以将以下来源用作数据库连接提供程序:

Documentation for JDBC appender is here. I can use as database connection provider these sources:

  • ConnectionFactory
  • 数据源
  • DriverManager

但是有什么方法可以使用整个应用程序中使用的数据源bean(com.jolbox.bonecp.BoneCPDataSource)?

but is there some way how to use datasource bean (com.jolbox.bonecp.BoneCPDataSource) what we use in entire application?

推荐答案

您可以分4个步骤进行操作(步骤2和3是Web应用程序的log4j2配置):

You can do it in 4 steps (step 2 & 3 are log4j2 config for web app):

步骤1:创建log4j2.xml文件(不带JDBC附加程序)并将其放在WEB-INF文件夹中

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>

</Configuration>

第2步:将log4j2-web-2.x.jar添加到WEB-INF/lib.

这是 maven存储库,您可以下载如果您使用maven,则从那里进行jar或将依赖项复制粘贴到pom.xml中.

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.

第3步:根据servlet api版本配置web.xml

有关更多详细信息,请参见log4j2 文档.这是在Servlet 2.5 Web应用程序中配置log4j的方法.

See log4j2 documentation for more details. Here is how you configure log4j in a servlet 2.5 web app.

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<!-- Logg -->
<listener>
    <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<filter>
    <filter-name>log4jServletFilter</filter-name>
    <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<!-- /Logg -->

<!-- ... -->
</web-app>

步骤4:创建一个spring bean,注入您的DataSource bean,并在@PostConstruct方法中动态添加JDBC Appender配置

这是代码示例(已通过log4j 2.1和spring 3.5测试)

Here is the code sample ( tested with log4j 2.1 and spring 3.5)

import java.sql.Connection;
import java.sql.SQLException;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.db.jdbc.ColumnConfig;
import org.apache.logging.log4j.core.appender.db.jdbc.ConnectionSource;
import org.apache.logging.log4j.core.appender.db.jdbc.JdbcAppender;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class LogUtils{

  @Autowired
  private DataSource dataSource;

  //inner class
  class Connect implements ConnectionSource {
    private DataSource dsource;
    public Connect(DataSource dsource) {
        this.dsource = dsource;
    }
    @Override
    public Connection getConnection() throws SQLException {
        return this.dsource.getConnection();
    }

  }

  public LogUtils() {
    System.out.println("LogUtils");
  }

  @PostConstruct
  private void init(){
    System.out.println("init LogUtils");
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    ColumnConfig[] cc = {
            ColumnConfig.createColumnConfig(config, "date", null, null, "true", null, null),
            ColumnConfig.createColumnConfig(config, "level", "%level", null, null, null, null),
            ColumnConfig.createColumnConfig(config, "logger", "%logger", null, null, null, null),
            ColumnConfig.createColumnConfig(config, "message", "%message", null, null, null, null),
            ColumnConfig.createColumnConfig(config, "throwable", "%ex{short}", null, null, null, null),
            ColumnConfig.createColumnConfig(config, "salarie_id", "%X{SALARIE_ID}", null, null, null, null)
    } ;
    Appender appender = JdbcAppender.createAppender("databaseAppender", "true", null, new Connect(dataSource), "0", "sicdb.bo_log", cc);
    appender.start();
    config.addAppender(appender);
    LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    loggerConfig.addAppender(appender, null, null);
    ctx.updateLoggers();
  }

  public DataSource getDataSource() {
    return dataSource;
  }

  public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
  }


}

**局限性:链接到文档 **

** Limitations : link to doc**

这篇关于如何使用Spring BoneCPDataSource bean作为Log4j 2 JDBC附加程序的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 01:13