【问题标题】:How to initialize log4j with Spring Boot application?如何使用 Spring Boot 应用程序初始化 log4j?
【发布时间】:2019-11-15 06:57:46
【问题描述】:

我有一个 Spring Boot 应用程序,我想在这个应用程序中使用 log4j。问题是我有一个 JDBC 附加程序,例如 (log4j2.xml);

<JDBC name="customDBAppender" tableName="mytable">
    <ConnectionFactory
                class="com.example.logger.ConnectionFactory" method="getConnection" />
    ....
</JDBC>

我有一个静态 getConnection 方法,我需要在这个方法中访问我的数据库属性(用户名、密码)。

我认为 log4j 使用反射来创建与此方法的连接(甚至在 Spring 上下文初始化之前),因此我无法使用 Spring 注入我的数据库属性。有没有办法注入这个属性?

我的 ConnectionFactory 类;

public class ConnectionFactory {

    public static Connection getConnection() throws SQLException {
        Connection connection = new Connection("dbusername", "dbpassword".....)
        ....
    }

}

【问题讨论】:

标签: java spring spring-boot log4j2


【解决方案1】:

就像你猜到的那样,你不能以这种方式配置你的 jdbc-appender。 相反,您需要从 log4j2 配置中删除 jdbc appender,并在 Bean 中实用地创建它。例如在 @Configuration bean 中。

另外请注意,即使这样会起作用,您也应该始终使用连接池而不是单个连接池,否则它会真正降低您的应用性能。

那么做如下:

@Configuration
public class JdbcAppenderConfiguration {
    @Autowired
    private DataSource dataSource;

    //Or @PostConstruct
    @EventListener
    public void handleContextStart(ContextStartedEvent cse) {
        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();
    }
}

【讨论】:

  • 您用来包装 DataSourceConnect 对象是什么?
猜你喜欢
  • 2011-05-22
  • 1970-01-01
  • 2013-07-02
  • 2018-11-13
  • 2014-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多