[logback-user] How to use different xml settings file for logback depending on run.mode

ceki ceki at qos.ch
Thu Sep 20 22:39:28 CEST 2012


Hi Olek,

I can think of three techniques at your disposal. Moreover, they may be 
combined if necessary.

1) conditional processing of config files

See http://logback.qos.ch/manual/configuration.html#conditional

This option requires the Janino library. You would have a single config 
file parts of which would be activated depending on the value of the
run.mode property.

2) file inclusion

See http://logback.qos.ch/manual/configuration.html#fileInclusion

The included file would be a system property, say "includedFile". You 
would be responsible for setting "includedFile". Here is a sample 
logback.xml file:

configuration>
   <include file="${includedFile:-/path/to/default/logback.xml}"/>

   other configuration directives common to various modes go here...

</configuration>

The above uses default values for variables.
See http://tinyurl.com/bmxcj5w


3) Specifying the location of the default configuration file as a system 
property

   http://logback.qos.ch/manual/configuration.html#configFileProperty

This third option is probably the simplest. You programmatically set the 
  "logback.configurationFile" system property to point to the 
appropriate config file.


BYW, you can reset any existing configuration by invoking:

   LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
   lc.reset();

Let us know how it goes.
-- 
Ceki

On 20.09.2012 20:58, Olek Swirski wrote:
> Hi,
>
> I'm using logback in a runnable jar containing a lift webapp. logback reads
> its settings from logback.xml file at the root of the jar.
> This runnable jar can be run in development mode by just using:
> java -jar myexecutable.jar
> but also in production mode:
> java -Drun.mode=production -jar myexecutable.jar
> I would like to be able to use different file for logback configuration
> depending on
> the run.mode. For example in default (development) mode this could be
> default.logback.xml (or just logback.xml as it is by default)
> and in production
> production.default.logback.xml
> or in test
> test.default.logback.xml
>
> How do I do that? In my app, there is a runnable object Start, with main
> method,
> where I probably could set some init options for logback depending on
> the run mode,
> which can be detected with System.getProperty("run.mode").
>
> Here is the topic on lift mailing list, about this executable jar, and
> some last few
> messages are about trying to figure this out
> https://groups.google.com/forum/?fromgroups=#!topic/liftweb/uwwIA8FwmU8
>




More information about the Logback-user mailing list