gpt4 book ai didi

log4j2 附加程序与 sql server 数据库对话

转载 作者:行者123 更新时间:2023-12-01 19:07:04 26 4
gpt4 key购买 nike

此网址

https://logging.apache.org/log4j/log4j-2.0/manual/appenders.html

有这个例子:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
<Appenders>
<JDBC name="databaseAppender" tableName="dbo.application_log">
<DataSource jndiName="java:/comp/env/jdbc/LoggingDataSource" />
<Column name="eventDate" isEventTimestamp="true" />
<Column name="level" pattern="%level" />
<Column name="logger" pattern="%logger" />
<Column name="message" pattern="%message" />
<Column name="exception" pattern="%ex{full}" />
</JDBC>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="databaseAppender"/>
</Root>
</Loggers>
</Configuration>

当我尝试连接到 sqlserver 数据库时......

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<JDBC name="SQLServerAppender" tableName="dbo.LogEntry">
<DataSource jndiName="jdbc:sqlserver://MyMachine\\MyInstance:1433;databaseName=LoggingDB;applicationName=myappname;integratedSecurity=true;" />
<Column name="EntryDateUtc" isEventTimestamp="true" />
<Column name="LOGGER" pattern="%C" />
<Column name="Level" pattern="%level" />
<Column name="Message" pattern="%m" />
<Column name="UserName" pattern="%x" />
<Column name="Priority" pattern="%p" />
<Column name="ElapsedMilliseconds" pattern="%r" />
<Column name="ThreadName" pattern="%t" />
<Column name="ThrowableMessage" pattern="%throwable " />
</JDBC>

我收到如下错误:

ERROR No ConnectionSource provided: connectionSource
ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.db.jdbc.JdbcAppender for element JDBC org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element JDBC are invalid: field 'connectionSource' has invalid value 'null'

如何在 xml 配置中设置连接字符串以与 sqlserver 通信?

尽管它与文档不匹配,但我尝试了这个:

   <Appenders>
<JDBC name="SQLServerAppender" tableName="dbo.LogEntry">

<ConnectionSource jndiName="jdbc:sqlserver

当然没用。

什么是神奇的语法糖? #帮助

谢谢。

最佳答案

我认为问题是 JNDI 未启用。

这是我的配置log4j2.xml:

<bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg ref="dataSource"/>
</bean>

<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceProxy"/>
</bean>

<bean id="jdbcTemplate" class="xx.com.xxxxx.framework.database.xxxxxJDBCTemplate">
<constructor-arg ref="dataSourceProxy"/>
</bean>

<bean id="DBSession" class="xx.com.xxxxx.framework.database.DBConnectionManager" lazy-init="true">
<constructor-arg type="java.lang.String" value="DBSession"/>
<constructor-arg ref="dataSourceProxy"/>
<constructor-arg ref="transactionManager"/>
<property name="template" ref="jdbcTemplate"/>
</bean>

在我们将log4j升级到log4j2之前,它可以正常工作。升级后,我的 weblogic 控制台输出错误:

ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.db.jdbc.JdbcAppender for element JDBC org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element JDBC are invalid: field 'connectionSource' has invalid value 'null'
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.injectFields(PluginBuilder.java:210)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:121)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1120)
....

根据Apache documentation ,默认情况下禁用配置有 DataSource 的 Log4j JDBC Appender。您可以尝试添加java选项-Dlog4j2.enableJndiJdbc=true来使用JNDI的java协议(protocol)。

当我们将 set JAVA_OPTIONS=-Dlog4j2.enableJndiJdbc=true 添加到我们的 weblogic 中时,错误就消失了。希望对您有帮助。

关于log4j2 附加程序与 sql server 数据库对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52092235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com