Externalized Logback configuration for web applications

Hello, I have a logging use case that I would think was relatively common, but that does not seem to be supported particularly well by logback. I wanted to see what the logback community thinks/advises. The details of the use case - I am writing java web apps to be deployed to JEE web containers. - I want the logging config to be external to the wars, so that I can use the same wars in different environments, but configure logging differently. - I want to be able to deploy several different wars to the same web container and have them use different logging configurations. The logback documentation seems to push towards either packaging the logback config in the wars, or sharing an external logging config between them. (Whereas I want separate external logging configuration for each war) I hope that makes sense. The JNDI support described here: http://logback.qos.ch/manual/loggingSeparation.html looks like it might be of use, but it does seem to require that the config files are placed in WEB-INF/classes and I would prefer they are completely external to the war. I have found a number of people who have coded up their own solutions, notably: http://blog.iprofs.nl/tag/logback/ and https://bowerstudios.com/node/896 That doesn't look too bad, but wondering what the community thinks of these approaches -- ----------------------- Perryn Fowler

Hi Perry, My use case is basically the same as yours, except im deploying wars in a tomcat and not in a full jboss et al. The solution, i came up with is based on logbacks ability to look up values in a JNDI directory and to include files based. Basically you pack a skeleton logback configuration with your war. This skeleton configuration file simply looks up the url for a file containing the real configuration using JNDI and then includes it. I have attached 3 files, i use to do all this magic, in one of my projects as an example. logback.xml is the skeleton configuration file, which goes into the war. It contains a maven pom.name reference, since i use maven filtering to ensure, the lookup key matches application. I tend to forget updating things like that when reusing code etc. context.xml is a sample context, i use when testing this particular webapp in an embedded tomcat. I have no parameters defined in web.xml, since i dont want to risk silently falling back to defaults, i prefer to go out with a bang. logback-test.xml is the actual logback configuration to be included. I know, its a bad choice of name, since its a logback magic file name. I discussed the solution with Ceki on this list under the subject 'configuration through JNDI' and got the seal of approval, and it has worked for me without any problems so far. Yours sincerely, Bjorn

On 17.08.2012 06:23, Perryn Fowler wrote:
Hello,
I have a logging use case that I would think was relatively common, but that does not seem to be supported particularly well by logback. I wanted to see what the logback community thinks/advises.
The details of the use case - I am writing java web apps to be deployed to JEE web containers. - I want the logging config to be external to the wars, so that I can use the same wars in different environments, but configure logging differently. - I want to be able to deploy several different wars to the same web container and have them use different logging configurations.
Here is a simple recipe: 1) Package logback jars in each web-app separately. 2) In web-app A, place a logback.xml file which references an external file for inclusion. Example: # --- logback.xml shipping in web-app A ------- <configuration debug="true"> <include file="/opt/logback-A.xml"> </configuration> # ---------------------------------------------- /opt/logback-A.xml would contains the config specific for web-app A. 3) Do the same for your other web-apps. 4) Declare victory. As Bjorn mention, you can parameterize the reference to the included file via JNDI (or any of the other variable types supported in logback). HTH, -- Ceki http://tinyurl.com/proLogback

Thanks guys, it looks like the bit I was missing was the ability to include externalized config files from a packaged config file. cheers Perryn On Sat, Aug 18, 2012 at 6:59 AM, ceki <ceki@qos.ch> wrote:
On 17.08.2012 06:23, Perryn Fowler wrote:
Hello,
I have a logging use case that I would think was relatively common, but that does not seem to be supported particularly well by logback. I wanted to see what the logback community thinks/advises.
The details of the use case - I am writing java web apps to be deployed to JEE web containers. - I want the logging config to be external to the wars, so that I can use the same wars in different environments, but configure logging differently. - I want to be able to deploy several different wars to the same web container and have them use different logging configurations.
Here is a simple recipe:
1) Package logback jars in each web-app separately. 2) In web-app A, place a logback.xml file which references an external file for inclusion. Example:
# --- logback.xml shipping in web-app A -------
<configuration debug="true"> <include file="/opt/logback-A.xml"> </configuration>
# ----------------------------------------------
/opt/logback-A.xml would contains the config specific for web-app A.
3) Do the same for your other web-apps.
4) Declare victory.
As Bjorn mention, you can parameterize the reference to the included file via JNDI (or any of the other variable types supported in logback).
HTH,
-- Ceki http://tinyurl.com/proLogback _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
-- ----------------------- Perryn Fowler ThoughtWorks

Thank you for the simple recipe using logback.xml. Is there an equally simple way using the groovy configuration? If I try embedding these lines into logback.groovy: shell = new GroovyShell(binding) shell.evaluate(new File("/etc/logback-override.groovy").text) I get missing property exceptions when I try to use the 'root' and 'logger' methods from the external file. Any help appreciated! Ceki Gulcu wrote:
On 17.08.2012 06:23, Perryn Fowler wrote:
Hello,
I have a logging use case that I would think was relatively common, but that does not seem to be supported particularly well by logback. I wanted to see what the logback community thinks/advises.
The details of the use case - I am writing java web apps to be deployed to JEE web containers. - I want the logging config to be external to the wars, so that I can use the same wars in different environments, but configure logging differently. - I want to be able to deploy several different wars to the same web container and have them use different logging configurations.
Here is a simple recipe:
1) Package logback jars in each web-app separately. 2) In web-app A, place a logback.xml file which references an external file for inclusion. Example:
# --- logback.xml shipping in web-app A -------
<configuration debug="true"> <include file="/opt/logback-A.xml"> </configuration>
# ----------------------------------------------
/opt/logback-A.xml would contains the config specific for web-app A.
3) Do the same for your other web-apps.
4) Declare victory.
As Bjorn mention, you can parameterize the reference to the included file via JNDI (or any of the other variable types supported in logback).
HTH,
-- Ceki http://tinyurl.com/proLogback _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
-- View this message in context: http://old.nabble.com/Externalized-Logback-configuration-for-web-application... Sent from the Logback User mailing list archive at Nabble.com.

File inclusion is not supported by Gaffer (logback's GroovyConfigurator) out-of-the-box. On 24.09.2012 22:55, Larry J wrote:
Thank you for the simple recipe using logback.xml. Is there an equally simple way using the groovy configuration?
If I try embedding these lines into logback.groovy: shell = new GroovyShell(binding) shell.evaluate(new File("/etc/logback-override.groovy").text) I get missing property exceptions when I try to use the 'root' and 'logger' methods from the external file.
Any help appreciated!
Ceki Gulcu wrote:
On 17.08.2012 06:23, Perryn Fowler wrote:
Hello,
I have a logging use case that I would think was relatively common, but that does not seem to be supported particularly well by logback. I wanted to see what the logback community thinks/advises.
The details of the use case - I am writing java web apps to be deployed to JEE web containers. - I want the logging config to be external to the wars, so that I can use the same wars in different environments, but configure logging differently. - I want to be able to deploy several different wars to the same web container and have them use different logging configurations.
Here is a simple recipe:
1) Package logback jars in each web-app separately. 2) In web-app A, place a logback.xml file which references an external file for inclusion. Example:
# --- logback.xml shipping in web-app A -------
<configuration debug="true"> <include file="/opt/logback-A.xml"> </configuration>
# ----------------------------------------------
/opt/logback-A.xml would contains the config specific for web-app A.
3) Do the same for your other web-apps.
4) Declare victory.
As Bjorn mention, you can parameterize the reference to the included file via JNDI (or any of the other variable types supported in logback).
HTH,
-- Ceki http://tinyurl.com/proLogback _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
-- Ceki http://twitter.com/ceki

Is it possible to grab the value of the filename from the context of the web application itself, e.g. a <context-param> inside the web.xml? Idea here is that this can be overridden by a context.xml at deployment time, to externalize the filename without disturbing the web application archive, and without needing JNDI. e.g. so: <include file="/opt/logback-A.xml"> becomes: <include file="${some-value-from-deployment-descriptor}"> Thanks! Best regards, Richard -- View this message in context: http://logback.10977.n7.nabble.com/Externalized-Logback-configuration-for-we... Sent from the Users mailing list archive at Nabble.com.

Just for the record, the servlet specification (or is it the Java EE) does not allow you to write to the file system, making your application subtly vendor dependent. We therefore use the jdk14 bridge instead of logback to use the container logging instead. /Thorbjørn -----Original Message----- From: logback-user-bounces@qos.ch [mailto:logback-user-bounces@qos.ch] On Behalf Of Richard Sand Sent: 31. januar 2013 20:40 To: logback-user@qos.ch Subject: Re: [logback-user] Externalized Logback configuration for web applications Is it possible to grab the value of the filename from the context of the web application itself, e.g. a <context-param> inside the web.xml? Idea here is that this can be overridden by a context.xml at deployment time, to externalize the filename without disturbing the web application archive, and without needing JNDI. e.g. so: <include file="/opt/logback-A.xml"> becomes: <include file="${some-value-from-deployment-descriptor}"> Thanks! Best regards, Richard -- View this message in context: http://logback.10977.n7.nabble.com/Externalized-Logback-configuration-for-we b-applications-tp3629p11387.html Sent from the Users mailing list archive at Nabble.com. _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

Sure. Values for variables can be fetched from JNDI. See [1]. Values defined in <context-param> inside the web.xml are available via JNDI. As far as I know, all web containers export values defined in <context-param> inside the web.xml via JNDI. Please try the method outlined in [1] and kindly report back on this mailing list. [1] http://logback.qos.ch/manual/configuration.html#insertFromJNDI On 31.01.2013 20:40, Richard Sand wrote:
Is it possible to grab the value of the filename from the context of the web application itself, e.g. a <context-param> inside the web.xml? Idea here is that this can be overridden by a context.xml at deployment time, to externalize the filename without disturbing the web application archive, and without needing JNDI.
e.g. so:
<include file="/opt/logback-A.xml">
becomes:
<include file="${some-value-from-deployment-descriptor}">
Thanks!
Best regards,
Richard
-- Ceki 65% of statistics are made up on the spot

Hi Ceki - I guess I was looking for another mechanism than JNDI to expose those, simply to avoid needing the extra context listeners and servlet filter needed for performance. Is there some magic we could do when logging is initialized to see if it's being loaded by a servlet classloader and finding the base servlet and grabbing the servletcontext? Or am I grasping at straws here? Thanks! Richard Sand | Managing Director PO Box 91824 | Austin | Texas 78709-1824 | USA Office: +1 888 612 8820 ext 02 | Fax: +1 866 304 3754 Mobile: +1 267 984 3651 -----Original Message----- From: logback-user-bounces@qos.ch [mailto:logback-user-bounces@qos.ch] On Behalf Of ceki Sent: Friday, February 01, 2013 1:12 PM To: logback users list Subject: Re: [logback-user] Externalized Logback configuration for web applications Sure. Values for variables can be fetched from JNDI. See [1]. Values defined in <context-param> inside the web.xml are available via JNDI. As far as I know, all web containers export values defined in <context-param> inside the web.xml via JNDI. Please try the method outlined in [1] and kindly report back on this mailing list. [1] http://logback.qos.ch/manual/configuration.html#insertFromJNDI On 31.01.2013 20:40, Richard Sand wrote:
Is it possible to grab the value of the filename from the context of the web application itself, e.g. a <context-param> inside the web.xml? Idea here is that this can be overridden by a context.xml at deployment time, to externalize the filename without disturbing the web application archive, and without needing JNDI.
e.g. so:
<include file="/opt/logback-A.xml">
becomes:
<include file="${some-value-from-deployment-descriptor}">
Thanks!
Best regards,
Richard
-- Ceki 65% of statistics are made up on the spot _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

Hello Richard, In principle, you don't have to do anything to get this working, no context listeners or filters. There are no steps involved other than defining a value in web.xml and fetching the defined value as explained in [1]. Please give it a try. [1] http://logback.qos.ch/manual/configuration.html#insertFromJNDI -- Ceki 65% of statistics are made up on the spot On 01.02.2013 19:21, Richard Sand wrote:
Hi Ceki - I guess I was looking for another mechanism than JNDI to expose those, simply to avoid needing the extra context listeners and servlet filter needed for performance.
Is there some magic we could do when logging is initialized to see if it's being loaded by a servlet classloader and finding the base servlet and grabbing the servletcontext? Or am I grasping at straws here?
Thanks!
Richard Sand | Managing Director PO Box 91824 | Austin | Texas 78709-1824 | USA Office: +1 888 612 8820 ext 02 | Fax: +1 866 304 3754 Mobile: +1 267 984 3651
-----Original Message----- From: logback-user-bounces@qos.ch [mailto:logback-user-bounces@qos.ch] On Behalf Of ceki Sent: Friday, February 01, 2013 1:12 PM To: logback users list Subject: Re: [logback-user] Externalized Logback configuration for web applications
Sure. Values for variables can be fetched from JNDI. See [1]. Values defined in <context-param> inside the web.xml are available via JNDI. As far as I know, all web containers export values defined in <context-param> inside the web.xml via JNDI.
Please try the method outlined in [1] and kindly report back on this mailing list.
[1] http://logback.qos.ch/manual/configuration.html#insertFromJNDI
On 31.01.2013 20:40, Richard Sand wrote:
Is it possible to grab the value of the filename from the context of the web application itself, e.g. a <context-param> inside the web.xml? Idea here is that this can be overridden by a context.xml at deployment time, to externalize the filename without disturbing the web application archive, and without needing JNDI.
e.g. so:
<include file="/opt/logback-A.xml">
becomes:
<include file="${some-value-from-deployment-descriptor}">
Thanks!
Best regards,
Richard
participants (6)
-
bjorn rohde jensen
-
ceki
-
Larry J
-
Perryn Fowler
-
Richard Sand
-
Thorbjørn Ravn Andersen