51

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?

Community
  • 1
  • 1
shabby
  • 3,002
  • 3
  • 39
  • 59

5 Answers5

62
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();
Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • I would upvote this as the best answer if you would add the reset() call. – JimN May 16 '15 at 02:44
  • 1
    Is this the only way to pass a logback setting file configuration ? I alsways found the _ye olde_ log4j BasicConfigurator relatively intuiitve. Pass a file name and let it happen. – will Jul 10 '16 at 13:18
  • 2
    JoranConfigurator inherits from http://logback.qos.ch/apidocs/ch/qos/logback/core/joran/GenericConfigurator.html and there are 5 overrides for `doConfigure()` – Luca Basso Ricci Jul 10 '16 at 16:02
  • @michid you have a better solution ? – Anand Rockzz May 23 '18 at 18:25
  • 1
    If you use Spring Boot than the `org.springframework.boot.logging.logback.SpringBootJoranConfigurator` should be used which is a subclass of the `JoranConfigurator`. This class adds support for the Spring Boot specific parts like the `` (https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html). – Gebezs Aug 09 '19 at 16:58
52

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • 3
    If you want to override any default configuration, this method does not work. You need to reset the configuration using JoranConfiguration – isian8814 Sep 13 '16 at 19:32
  • Is it a relative path like 'application/test/conf/logback.xml' or a full path 'C:/application/test/conf/logback.xml'? – Pwnstar Jun 13 '18 at 09:31
  • Depends on your project configuration - but if you are using maven for example, a relative path within the jar should work. So if you've put it in src/main/resources, just the file name should work. – assylias Jun 13 '18 at 11:48
  • if you deploy this to tomcat, app is not gonna load after tomcat restart – syd Apr 10 '19 at 13:04
  • @syd I don't know Tomcat but, generally speaking, in an application server environment you should rely on the container logging system and not use your own. – assylias Apr 10 '19 at 14:12
  • this doesn't work for me. It goes with the default configuration instead of my logback.xml file – hidden_machine Sep 05 '22 at 08:52
11

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

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

Amin Abbaspour
  • 1,111
  • 11
  • 10
3

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.

sschrass
  • 7,014
  • 6
  • 43
  • 62
0

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