Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-557

ServletContextListener to relocate logback config, implementation provided

    XMLWordPrintable

Details

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • 0.9.20
    • None
    • None

    Description

      In spring there is a Log4jConfigListener which can be used to reconfigure the default location of the log4j config. I always liked to use it because so I could relocate my log4j.xml from the classes directory next to the web.xml so the config files were at one place. Because I did not find something like that I wrote one for logback. I think it could be included into logback-classic.

      ch.qos.logback.classic.servlet.LogbackConfigListener configuration is very easy:

      1. Add the listener to the web.xml (it should be the first listener so logback gets configured before the other listeners start up):
      <listener>
      <listener-class>ch.qos.logback.classic.servlet.LogbackConfigListener</listener-class>
      </listener>

      2. Add a context-param to web.xml to specify the location of the logback config:
      <context-param>
      <param-name>logbackConfigLocation</param-name>
      <param-value>/WEB-INF/log.xml</param-value>
      </context-param>

      And thats it. The location is resolved in the following way (similar to spring aproach):

      • if the location starts with "/" it is resolved as servlet context resource
      • otherwise an URL is constructed from it. So you can use locations like this also: file:/foo/bar/logback.xml or file:d:/foo/bar/logback.xml
      • if the the first method fails a java.io.FileInputStream is created from the location to be used as input for logback configuration.
      • If none of the above worked an error is logged to servlet log but no exception is thrown.

      The location can contain system properties, those gets resolved. So a location can be configured for example to: file:${user.home}/configuration/log.xml

      The original spring listener only worked with expanded wars this logback variant does not have this requirement.

      Attachments

        Activity

          People

            logback-dev Logback dev list
            akiraly Attila Király
            Votes:
            9 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated: