我试图让LogBack DBAppender从程序化配置开始工作,但似乎无法让它正常运行。
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
DBAppender dbAppender = new DBAppender();
dbAppender.setContext(lc);
DriverManagerConnectionSource connectionSource = new DriverManagerConnectionSource();
connectionSource.setDriverClass("com.mysql.jdbc.Driver");
connectionSource.setUrl(loggingConnectionInfo.getUri());
connectionSource.setUser(loggingConnectionInfo.getUser());
connectionSource.setPassword(loggingConnectionInfo.getPassword());
connectionSource.setContext(lc);
connectionSource.start();
dbAppender.setConnectionSource(connectionSource);
dbAppender.start();
logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logger.setLevel(Level.DEBUG);
logger.addAppender(dbAppender);
知道可能出现什么问题吗?我在控制台上看到了一个日志,但没有任何内容进入数据库。已经有一段时间与这个人抗争了,并希望有任何见解!
答案 0 :(得分:1)
我相信@ebensing发现了问题,而且是LoggerContext
。这是一个工作版本,差别很大的是logger.getLoggerContext()
:
Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logger.setLevel(Level.DEBUG);
DriverManagerConnectionSource connSource = new DriverManagerConnectionSource();
connSource.setDataSource(ds);
connSource.setContext(logger.getLoggerContext());
connSource.start();
DBAppender dbAppender = new DBAppender();
dbAppender.setConnectionSource(connSource);
dbAppender.setContext(logger.getLoggerContext());
dbAppender.start();
logger.addAppender(dbAppender);