我正在尝试以编程方式配置 Logback 的 DBAppender,但不知道出了什么问题。它适用于下面给出的 logback.xml 配置
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<driverClass>com.mysql.jdbc.Driver</driverClass>
<jdbcUrl>jdbc:mysql://localhost:3306/mydatabase</jdbcUrl>
<user>myuser</user>
<password>mypwd</password>
</connectionSource>
</appender>
现在,当我尝试在这样的代码中获取和配置 Logger 时
Logger logger = (Logger) LoggerFactory.getLogger("dbAppender");
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
DriverManagerConnectionSource dmcs = new DriverManagerConnectionSource();
dmcs.setDriverClass("com.mysql.jdbc.Driver");
dmcs.setUrl("jdbc:mysql://localhost:3306/mydatabase");
dmcs.setUser("myuser");
dmcs.setPassword(mypwd);
dmcs.setContext(lc);
DBAppender dbapp = (DBAppender) logger.getAppender("DB");
//it returns an appender with properties in logback.xml file
if(dbapp != null) {
dbapp.stop();
dbapp.setConnectionSource(dmcs);
dbapp.start();
}
它抛出异常
java.lang.IllegalStateException: DBAppender cannot function if the JDBC driver does not support getGeneratedKeys method *and* without a specific SQL dialect
at ch.qos.logback.core.db.DBAppenderBase.start(DBAppenderBase.java:62)
at ch.qos.logback.classic.db.DBAppender.start(DBAppender.java:96)
没有办法设置方言或 setGeneratedKey,我需要什么才能使其工作。