Logback 找不到自定义配置文件 (FileNotFoundException)
Logback cannot find a custom configuration file (FileNotFoundException)
我无法强制 logback 使用我的自定义配置文件,因为它看不到它。
这是我的项目结构:
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 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'
}
我是不是漏掉了什么?
如果我将配置文件(资源)作为流加载,它会起作用。所以,这里有一个logback配置的方法:
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
- 是资源文件名,如myLogbackConfig.xml
我无法强制 logback 使用我的自定义配置文件,因为它看不到它。
这是我的项目结构:
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 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'
}
我是不是漏掉了什么?
如果我将配置文件(资源)作为流加载,它会起作用。所以,这里有一个logback配置的方法:
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
- 是资源文件名,如myLogbackConfig.xml