
On 07.06.2012 10:24, stephen.t.chan@asia.bnpparibas.com wrote:
Hi all,
I am running tomcat6 as http server and specify jdbc connection(with login credential) in context.xml. Besides, I have defined to use DBAppender in logback.xml to write log to database. My question is, instead of directly write the login credential in the logback.xml file, how to use property substitution to retrieve the credential from tomcat context.xml?
for example,
<appendername=/"DB"/class=/"com.eqd.iprice.appender.DBAppender"/> <connectionSourceclass=/"ch.qos.logback.core.db.DriverManagerConnectionSource"/>
<dataSourceclass=/"javax.sql.DataSource"/> <driverClass>${driverClass}</driverClass> <url>${url}</url> <user>${username}</user> <password>${password}</password> </dataSource> </connectionSource> </appender>
Those ${XXXX} are retrieved from tomcat context.xml .
Thanks.
Stephen
Hi Stephen, Here are a couple of approaches you could try. Approach 1) If there is a programmatic way to access the variables defined in context.xml, then you could easily write a custom property definer [1]. As long as you can access variables defined in context.xml, and that's a relatively big if, writing a custom property definer should be a piece of cake. Approach 2) If it is not possible to access the variables defined in context.xml, you can redefine them in web.xml as JNDI variables. Logback can obtain variables from JNDI. This approach has the inconvenience of forcing you to define variables twice, once in content.xml and once in web.xml. Approach 3) The best approach in my opinion is to obtain a connection source from JNDI. The connection source is defined in Tomcat and logback retrieves and uses it. See JNDIConnectionSource in DBAppender documentation [3]. Please keep us posted on your progress. Cheers, [1] http://logback.qos.ch/manual/configuration.html#definingPropsOnTheFly [2] http://logback.qos.ch/manual/configuration.html#insertFromJNDI [3] http://logback.qos.ch/manual/appenders.html#DBAppender -- Ceki http://twitter.com/#!/ceki