I am trying to get the LogBack DBAppender to work from a programmatic configuration, but just can't seem to get it functioning.
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);
Any idea what could be wrong? I'm seeing a log in the console made, but nothing goes to the database. Been fighting this one for a while and would appreciate any insight!
I believe @ebensing found the problem, and it was the LoggerContext
. Here is a working version, with the big difference being 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With