Search code examples
javaloggingjndilogbackappender

Logback JNDI Connection Source


Logback allows you to define an DBAppender with a JNDI data source like so:

<connectionSource class="ch.qos.logback.core.db.JNDIConnectionSource">
    <jndiLocation>java:comp/env/jdbc/dbLogging</jndiLocation>
</connectionSource>

I will be deploying this Java app as a WAR to Tomcat, but want DB logging to work when I'm testing locally in Eclipse or in a standalone Tomcat instance. Where/how do I configure the JNDI data source that Logback will use when it reads the above configuration? Thanks in advance!


Solution

  • In a standalone tomcat instance you would configure server.xml or context.xml in tomcat to define the data source as per normal (see here)

    For using it locally in Eclipse, i.e. without a Web container, you'd change your connectionSource to something like:

    <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
      <driverClass>com.mysql.jdbc.Driver</driverClass>
      <url>jdbc:mysql://host_name:3306/datebase_name</url>
      <user>username</user>
      <password>password</password>
    </connectionSource>
    

    See also, logback manual on appenders which has a tomcat example.