
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, <appender name="DB" class="com.eqd.iprice.appender.DBAppender"> <connectionSource class= "ch.qos.logback.core.db.DriverManagerConnectionSource"> <dataSource class="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 This message and any attachments (the "message") is intended solely for the intended addressees and is confidential. If you receive this message in error,or are not the intended recipient(s), please delete it and any copies from your systems and immediately notify the sender. Any unauthorized view, use that does not comply with its purpose, dissemination or disclosure, either whole or partial, is prohibited. Since the internet cannot guarantee the integrity of this message which may not be reliable, BNP PARIBAS (and its subsidiaries) shall not be liable for the message if modified, changed or falsified. Do not print this message unless it is necessary,consider the environment. ------------------------------------------------------------------------------------------ Ce message et toutes les pieces jointes (ci-apres le "message") sont etablis a l'intention exclusive de ses destinataires et sont confidentiels. Si vous recevez ce message par erreur ou s'il ne vous est pas destine, merci de le detruire ainsi que toute copie de votre systeme et d'en avertir immediatement l'expediteur. Toute lecture non autorisee, toute utilisation de ce message qui n'est pas conforme a sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite. L'Internet ne permettant pas d'assurer l'integrite de ce message electronique susceptible d'alteration, BNP Paribas (et ses filiales) decline(nt) toute responsabilite au titre de ce message dans l'hypothese ou il aurait ete modifie, deforme ou falsifie. N'imprimez ce message que si necessaire, pensez a l'environnement.

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

Hi all, Thanks for Ceki's reply. Context.xml <Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource" username="test" password="test" driverClassName="MySqlDriver" url="jdbc:sqlserver://......" maxActive="8" maxIdle="3" removeAbandoned = "true" removeAbandonedTimeout = "60" logAbandoned = "true" /> logback.xml <connectionSource class= "ch.qos.logback.core.db.JNDIConnectionSource"> <param name="jndiLocation" value= "${dataSource.jndi.name}" /> </connectionSource> I have tried the 3rd approach as below but error exist. I have checked ${dataSource.jndi.name}is "jdbc/mydb". Please correct me if I am wrong for the setting. Thanks. 11:49:58,808 |-ERROR in ch.qos.logback.core.db.JNDIConnectionSource@19481b2 - Error while getting data source javax.naming.NameNotFoundException: Name jdbc is not bound in this Context at javax.naming.NameNotFoundException: Name jdbc is not bound in this Context at at org.apache.naming.NamingContext.lookup( NamingContext.java:770) at at org.apache.naming.NamingContext.lookup( NamingContext.java:153) Stephen Internet ceki@qos.ch Sent by: logback-dev-bounces@qos.ch 06/07/2012 04:43 PM Please respond to logback-dev@qos.ch To logback-dev@qos.ch cc Subject Re: [logback-dev] Question about logback 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 _______________________________________________ logback-dev mailing list logback-dev@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-dev This message and any attachments (the "message") is intended solely for the intended addressees and is confidential. If you receive this message in error,or are not the intended recipient(s), please delete it and any copies from your systems and immediately notify the sender. Any unauthorized view, use that does not comply with its purpose, dissemination or disclosure, either whole or partial, is prohibited. Since the internet cannot guarantee the integrity of this message which may not be reliable, BNP PARIBAS (and its subsidiaries) shall not be liable for the message if modified, changed or falsified. Do not print this message unless it is necessary,consider the environment. ------------------------------------------------------------------------------------------ Ce message et toutes les pieces jointes (ci-apres le "message") sont etablis a l'intention exclusive de ses destinataires et sont confidentiels. Si vous recevez ce message par erreur ou s'il ne vous est pas destine, merci de le detruire ainsi que toute copie de votre systeme et d'en avertir immediatement l'expediteur. Toute lecture non autorisee, toute utilisation de ce message qui n'est pas conforme a sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite. L'Internet ne permettant pas d'assurer l'integrite de ce message electronique susceptible d'alteration, BNP Paribas (et ses filiales) decline(nt) toute responsabilite au titre de ce message dans l'hypothese ou il aurait ete modifie, deforme ou falsifie. N'imprimez ce message que si necessaire, pensez a l'environnement.

Hi Stephen, What is the value of ${dataSource.jndi.name}? In a web-app, the value of jndiLocation should be prefixed with "java:comp/env/". Please let us know if this helps, -- Ceki http://twitter.com/#!/ceki On 07.06.2012 11:53, stephen.t.chan@asia.bnpparibas.com wrote:
Hi all,
Thanks for Ceki's reply.
Context.xml <Resourcename=/"jdbc/mydb"/auth=/"Container"/ type=/"javax.sql.DataSource"/username=/"test"/password=/"test"/ driverClassName=/"MySqlDriver"/ url=/"jdbc:sqlserver://......"/ maxActive=/"8"/ maxIdle=/"3"/ removeAbandoned= /"true"/ removeAbandonedTimeout= /"60"/ logAbandoned= /"true"/ />
logback.xml <connectionSourceclass=/"ch.qos.logback.core.db.JNDIConnectionSource"/> <paramname=/"jndiLocation"/value=/"${dataSource.jndi.name}"//> </connectionSource>
I have tried the 3rd approach as below but error exist. I have checked /${dataSource.jndi.name}/is "jdbc/mydb". Please correct me if I am wrong for the setting. Thanks.
11:49:58,808 |-ERROR in ch.qos.logback.core.db.JNDIConnectionSource@19481b2 - Error while getting data source _javax.naming.NameNotFoundException_: Name jdbc is not bound in this Context at _javax.naming.NameNotFoundException_: Name jdbc is not bound in this Context at at org.apache.naming.NamingContext.lookup(_NamingContext.java:770_) at at org.apache.naming.NamingContext.lookup(_NamingContext.java:153_)
Stephen
*Internet * *ceki@qos.ch*
*Sent by: logback-dev-bounces@qos.ch*
06/07/2012 04:43 PM Please respond to logback-dev@qos.ch
To logback-dev@qos.ch cc
Subject Re: [logback-dev] Question about logback
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

By the way, I updated the docs for JNDIConnectionSource [1]. [1] http://logback.qos.ch/manual/appenders.html#JNDIConnectionSource -- Ceki http://twitter.com/#!/ceki On 07.06.2012 18:06, ceki wrote:
Hi Stephen,
What is the value of ${dataSource.jndi.name}? In a web-app, the value of jndiLocation should be prefixed with "java:comp/env/".
Please let us know if this helps,

Hi Ceki, You are brillant. By referring to your JNDIConnectionSource docs. It works now. I give maximum stars to your revised document. Thank you very much. Stephen Internet ceki@qos.ch Sent by: logback-dev-bounces@qos.ch 06/08/2012 12:08 AM Please respond to logback-dev@qos.ch To logback-dev@qos.ch cc Subject Re: [logback-dev] Question about logback By the way, I updated the docs for JNDIConnectionSource [1]. [1] http://logback.qos.ch/manual/appenders.html#JNDIConnectionSource -- Ceki http://twitter.com/#!/ceki On 07.06.2012 18:06, ceki wrote:
Hi Stephen,
What is the value of ${dataSource.jndi.name}? In a web-app, the value of jndiLocation should be prefixed with "java:comp/env/".
Please let us know if this helps,
_______________________________________________ logback-dev mailing list logback-dev@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-dev This message and any attachments (the "message") is intended solely for the intended addressees and is confidential. If you receive this message in error,or are not the intended recipient(s), please delete it and any copies from your systems and immediately notify the sender. Any unauthorized view, use that does not comply with its purpose, dissemination or disclosure, either whole or partial, is prohibited. Since the internet cannot guarantee the integrity of this message which may not be reliable, BNP PARIBAS (and its subsidiaries) shall not be liable for the message if modified, changed or falsified. Do not print this message unless it is necessary,consider the environment. ------------------------------------------------------------------------------------------ Ce message et toutes les pieces jointes (ci-apres le "message") sont etablis a l'intention exclusive de ses destinataires et sont confidentiels. Si vous recevez ce message par erreur ou s'il ne vous est pas destine, merci de le detruire ainsi que toute copie de votre systeme et d'en avertir immediatement l'expediteur. Toute lecture non autorisee, toute utilisation de ce message qui n'est pas conforme a sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite. L'Internet ne permettant pas d'assurer l'integrite de ce message electronique susceptible d'alteration, BNP Paribas (et ses filiales) decline(nt) toute responsabilite au titre de ce message dans l'hypothese ou il aurait ete modifie, deforme ou falsifie. N'imprimez ce message que si necessaire, pensez a l'environnement.
participants (2)
-
ceki
-
stephen.t.chan@asia.bnpparibas.com