2016-12-01 107 views
0

我無法強制logback使用我的自定義配置文件,因爲它沒有看到它。Logback無法找到自定義配置文件(FileNotFoundException)

這裏是我的項目結構:

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

如果我把我的文件「logback.xml」可以下載它,但我需要有一個不同的文件名,因爲應用程序啓動時的文件會錯過一些屬性(即在啓動應用程序後添加文件名和根級別)。

這裏是我使用與正確的文件復位的logback的方法:

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 [email protected] - Setting up default configuration. 
13:39:49,458 |-ERROR in [email protected] - 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這個建罐子。 這是我的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' 
} 

我錯過了什麼嗎?

+0

那對我表示你的資源目錄沒有被拾起你的編譯系統(可能是由於配置錯誤)。這不是解決方案,但是嘗試使用'/ main/resources/logback_pattern.xml'作爲路徑,假設'Application'位於'src /'的根目錄。 – tony19

+0

我正在使用gradle構建jar。我更新了我的問題並提供了一個build.gradle文件。我用「/main/resources/logback_pattern.xml」獲得的錯誤... – Alexandr

回答

0

它工作,如果我加載配置文件作爲流。所以,這裏是的logback配置的方法:

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); 
     ClassLoader classloader = Thread.currentThread().getContextClassLoader(); 
     InputStream is = classloader.getResourceAsStream(LOGBACK_CONF); 
     configurator.doConfigure(is); 
    } catch (JoranException je) { 
     LOG.warn("Can not configure logger. Continue to execute the command.", je); 
    } 
    StatusPrinter.printInCaseOfErrorsOrWarnings(context); 
} 
相關問題