'Logback cannot find a custom configuration file (FileNotFoundException)

I can't force logback to use my custom configuration file because it doesn't see it.

Here is my project structure:

src
--main
----java
----resources
------logback_pattern.xml

If I call my file as "logback.xml" it can download it but I need to have a different file name because when application starts the file miss some properties (i.e. filename and root level is appended after application is started).

Here is a method which I use to reset logback with the correct file:

private static void configureLogger(String logDir, String logLevel, String logbackConf){
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    try {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(context);
        context.reset();
        context.putProperty("LOG_DIR", logDir);
        context.putProperty("LOG_LEVEL", logLevel);
        configurator.doConfigure(logbackConf);
    } catch (JoranException je) {
        LOG.warn("Can not configure logger. Continue to execute the command.", je);
    }
    StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}

Here is a samples which I tried to use:
1)

configureLogger(logDir, isDebug()?"INFO":"WARN", "logback_pattern.xml");

2)

ClassLoader classLoader = Application.class.getClassLoader();
configureLogger(logDir, isDebug()?"INFO":"WARN", classLoader.getResource("logback_pattern.xml").getFile());

3)

configureLogger(logDir, isDebug()?"INFO":"WARN", "classpath:logback_pattern.xml");

4)

configureLogger(logDir, isDebug()?"INFO":"WARN", "src/main/resources/logback_pattern.xml");

I always get the same error:

13:39:49,267 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:39:49,267 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:39:49,267 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
13:39:49,271 |-INFO in ch.qos.logback.classic.BasicConfigurator@67117f44 - Setting up default configuration.
13:39:49,458 |-ERROR in ch.qos.logback.classic.joran.JoranConfigurator@5d3411d - Could not open [logback_pattern.xml]. java.io.FileNotFoundException: logback_pattern.xml (No such file or directory)
        at java.io.FileNotFoundException: logback_pattern.xml (No such file or directory)
        at      at java.io.FileInputStream.open0(Native Method)
        at      at java.io.FileInputStream.open(FileInputStream.java:195)
        at      at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at      at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:79)
        at      at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:72)
        at      at com.jblur.acme_client.Application.configureLogger(Application.java:38)
        at      at com.jblur.acme_client.Application.main(Application.java:92)
13:39:49,464 |-WARN in Logger[com.jblur.acme_client.Application] - No appenders present in context [default] for logger [com.jblur.acme_client.Application].

I am using gradle to build jar. Here is my build.gradle file:

buildscript {
    repositories {
        mavenCentral()
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'application'

mainClassName = "com.jblur.acme_client.Application"

jar {
    baseName = 'acme_client'
    version =  '0.1.0'
    manifest {
        attributes "Main-Class": "$mainClassName"
    }

    doFirst {
        from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    }
    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7'
    compile group: 'org.shredzone.acme4j', name: 'acme4j-client', version: '0.8'
    compile group: 'org.shredzone.acme4j', name: 'acme4j-utils', version: '0.8'
    compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.55'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
    compile group: 'com.beust', name: 'jcommander', version: '1.48'
}

Am I missing something?



Solution 1:[1]

It works if I load a configuration file (resource) as a stream. So, here is a method for logback configuration:

private static void configureLogger(String logDir, String logLevel, String logbackConfigResource){
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    try {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(context);
        context.reset();
        context.putProperty("LOG_DIR", logDir);
        context.putProperty("LOG_LEVEL", logLevel);
        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        InputStream is = classloader.getResourceAsStream(logbackConfigResource);
        configurator.doConfigure(is);
    } catch (JoranException je) {
        LOG.warn("Can not configure logger. Continue to execute the command.", je);
    }
    StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}

logbackConfigResource - is the resource file name like myLogbackConfig.xml

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1