logback logging in oracle causes dialect error

2019-05-31 03:59发布

问题:

I set up database logging with slf4j and logback. I use this configuration with postgresql and sqlite without any problem

<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
    <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
        <dataSource class="com.zaxxer.hikari.HikariDataSource">
            <driverClassName>org.postgresql.Driver</driverClassName><!-- org.sqlite.JDBC-->
            <jdbcUrl>jdbc.postgresql://host:port/db_name</jdbcUrl><!-- jdbc:sqlite:local/dir/path/db_name.db-->
            <username>username</username>
            <password>password</password>
        </dataSource>
    </connectionSource>
</appender>

Now I want to send all these logs to oracle db so I changed the config file respectively

<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
    <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
        <dataSource class="com.zaxxer.hikari.HikariDataSource">
            <driverClassName>oracle.jdbc.driver.OracleDriver</driverClassName>
            <jdbcUrl>jdbc:oracle:thin:blabal@x.x.x.x:port:oraserv</jdbcUrl>
            <username>user</username>
            <password>pass</password>
        </dataSource>
    </connectionSource>
    <sqlDialect class="ch.qos.logback.core.db.dialect.OracleDialect" />
</appender>

But I got this error

15:45:05,832 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@25:76 - no applicable action for [sqlDialect], current ElementPath is [[configuration][appender][sqlDialect]]
15:45:05,837 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@26:16 - RuntimeException in Action for tag [appender] java.lang.IllegalStateException: DBAppender cannot function if the JDBC driver does not support getGeneratedKeys method *and* without a specific SQL dialect

I thought dialect configuration through xml would solve the problem, but nothing changed. Can you give me some advice to make it work with oracle?

回答1:

<appender name="db-classic-oracle" class="ch.qos.logback.classic.db.DBAppender">  
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">  
            <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">             	
                <driverClass>oracle.jdbc.driver.OracleDriver</driverClass>  
                <url>jdbc:oracle:thin:@localhost:1521:ZYD</url>  
                <user>scott</user>  
                <password>tiger</password>
                <sqlDialect class="ch.qos.logback.core.db.dialect.OracleDialect" />   
            </dataSource>  
        </connectionSource>
    </appender> 



回答2:

I figured it out just now. I guess you thought the "supporting getGeneratedKeys" means that logback can generate tables and fields for you? The answer is NO. In any case, you should run the scripts provided by logback in its zip file to build the tables by yourself. "getGeneratedKeys" is just another function, and logback supports it, which means you need not config the sqlDialect any more, from 0.9.*, you just need ignore the "sqlDialect".



回答3:

I had similar issues with Mysql. Eventually I figured it out. I don't know if it is any help for you but here is my configuration (groovy)

appender('DB', DBAppender) {
  connectionSource(DataSourceConnectionSource) {
    dataSource(MysqlDataSource) {
      url = "jdbc:mysql://127.0.0.1:3306/ts-mtt"
    }
    user = "root"
    password = ""
  }
} 

The sqlDialect should not be added. Also as you see user and password are properties of a connectionSource. I hope it is any help.