与SQL Server数据库对话的log4j2附加程序

时间:2018-08-30 08:16:03

标签: log4j2

此网址

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

那当然没用。

神奇的语法糖是什么? #help

谢谢。

1 个答案:

答案 0 :(得分:0)

DataSource jndiName的值不是SQL Server连接字符串。如果要使用SQL Server连接字符串,则在Log4J配置中放入以下内容:

<appender name="SQLServer" class="org.apache.log4j.jdbc.JDBCAppender">
   <param name="url" value="jdbc:sqlserver://MyMachine\\MyInstance:1433;databaseName=LoggingDB;applicationName=myappname;"/>
   <param name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
   <param name="user" value="user_id"/>
   <param name="password" value="password"/>
   <param name="sql" value="INSERT INTO LOG4J_TABLE VALUES('%x','%d','%C','%p','%m')"/>
   <layout class="org.apache.log4j.PatternLayout"></layout>
</appender>

<logger name="log4j.rootLogger" additivity="false">
   <level value="DEBUG"/>
   <appender-ref ref="SQLServer"/>
</logger>

这将写入您之前必须已创建的表,例如:

CREATE TABLE LOG4J_TABLE (
User_Id     VARCHAR(20)    NOT NULL,
Date_Stamp  DATETIME       NOT NULL,
Logger      VARCHAR(100)   NOT NULL,
Level       VARCHAR(10)    NOT NULL,
Message     VARCHAR(8000)  NOT NULL
)

请注意,您不能直接从Java使用integratedSecurity=true,但必须指定用户名和密码。