Logback无法找到自定义配置文件(FileNotFoundException)

时间:2016-12-01 11:50:07

标签: java classpath logback filenotfoundexception

我无法强制退回使用我的自定义配置文件,因为它看不到它。

这是我的项目结构:

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

如果我将我的文件称为“logback.xml”,它可以下载它,但我需要有一个不同的文件名,因为当应用程序启动时,文件会遗漏一些属性(即在启动应用程序后附加文件名和根级别)。

这是一种用于使用正确的文件重置回溯的方法:

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);
}

以下是我尝试使用的样本:
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");

我总是得到同样的错误:

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].

我正在使用gradle来构建jar。 这是我的build.gradle文件:

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'
}

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

如果我将配置文件作为流加载,它会起作用。所以,这是一个用于logback配置的方法:

bar