Setting logback.xml path programmatically

JavaLogback

Java Problem Overview


I know I can set the logback.xml path like this:

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

You may specify the location of the default configuration file with a system property named "logback.configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.

java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1

but how can I do it in code?

Java Solutions


Solution 1 - Java

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
JoranConfigurator configurator = new JoranConfigurator();
InputStream configStream = FileUtils.openInputStream(logbackPropertiesUserFile);
configurator.setContext(loggerContext);
configurator.doConfigure(configStream); // loads logback file
configStream.close();

Solution 2 - Java

You could use:

System.setProperty("logback.configurationFile", "/path/to/config.xml");

But it would have to happen before logback is loaded, i.e. something like:

class Main {
  static { System.setProperty("logback.configurationFile", "/path/to/config.xml");}
  private final Logger LOG = LoggerFactory.getLogger(Main.class);

  public void main (String[] args) { ... }
}

Note: I have not tested it but it should work.

Solution 3 - Java

modifying environment variables might not always be feasible. To do it properly see logback manual:

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

Solution 4 - Java

I just want to share what I did in the end with a Jersey-Spring app:

MyWebApplicationInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        servletContext.addListener(new LogbackConfigListener());
        try { LogbackConfigurer.initLogging(servletContext.getRealPath("/WEB-INF/logback.xml")); }
        catch (FileNotFoundException | JoranException e) { e.printStackTrace(); }
    }
}

I also have to add, that I had to move

<dependency>
    <groupId>org.logback-extensions</groupId>
    <artifactId>logback-ext-spring</artifactId>
    <version>0.1.4</version>
    <scope>compile</scope></dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.2.3</version>
    <scope>compile</scope></dependency>

from runtime (parent project in my case) to compile.

Solution 5 - Java

Include another logback xml to change path in logback-spring.xml

include resource = "/path/to/logback.xml"

add data within included tags in logback.xml

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshabbyView Question on Stackoverflow
Solution 1 - JavaLuca Basso RicciView Answer on Stackoverflow
Solution 2 - JavaassyliasView Answer on Stackoverflow
Solution 3 - JavaAmin AbbaspourView Answer on Stackoverflow
Solution 4 - JavasschrassView Answer on Stackoverflow
Solution 5 - JavaHimanshu SoniView Answer on Stackoverflow