Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring LogBack DBAppender programmatically

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!

like image 283
ebensing Avatar asked Feb 24 '14 22:02

ebensing


1 Answers

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);
like image 87
Alden Avatar answered Sep 22 '22 19:09

Alden