來源:http://www.cnblogs.com/guozp/p/5949744.html
以下兩個是我在使用slf4j + logback時候日志提示的問題,問題不大,都是WARN,並不真正影響運行,但是結果可能不是你希望的結果。
<1>
SLF4J: Found binding in [jar:file:/servers/storm-0.9.0.1/lib/logback-classic-1.0.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/servers/test_index/index.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
Class path contains multiple SLF4J bindings.
<2>
Could NOT find resource [logback.groovy]
- Could NOT find resource [logback-test.xml]
- Found resource [logback.xml] at [file:/D:/IdeaCode/gitCode/index_file/index_file_service/target/classes/logback.xml]
- Resource [logback.xml] occurs multiple times on the classpath.
- Resource [logback.xml] occurs at [file:/D:/IdeaCode/index/service/target/classes/logback.xml]
- Resource [logback.xml] occurs at [file:/D:/IdeaCode/index/dao/target/classes/logback.xml]
簡單介紹下SLF4J:
SLF4J(Simple logging Facade for Java)不是一個真正的日志實現,而是一個抽象層,它允許你在后台使用任意一個日志類庫。如果是在編寫供內外部都可以使用的API或者通用類庫,那么你真不會希望使用你類庫的客戶端必須使用你選擇的日志類庫。
SLF4j采用了靜態綁定來確定具體日志庫。靜態綁定就是為每一個具體的日志庫寫一個包名和類名都相同類: org.slf4j.impl.StaticLoggerBinder,這個類的功能就是調用具體的日志庫。這個類會存放在Adaptation layer或者native implementation of slf4j-api的jar包中。SLF4j的使用者只要把具體日志庫對應的Adaptation layer或者native implementation of slf4j-api的jar包放入classpath中,SLF4j便會裝載(load)對應版本的org.slf4j.impl.StaticLoggerBinder,從而調用具體的日志庫。
如果項目中之前使用了Log4j,現在要使用SLF4J + LogBack,如果遇到沖突,先觀察以下jar包:
(slf4j-XXX-version.jar)
slf4j-log4j12.jar:slf4j提供 log 接口,其具體實現是根據放入程序的綁定器決定.
slf4j-log4j12.jar就是實現通過slf4j調度使用log4j。
(XXX-over-slf4j.jar)
所謂的橋接器即一個假的日志實現工具。
log4j-over-slf4j.jar:是一個橋接器,本來組件是通過log4j輸出日志的,通過該橋接器被轉到slf4j,slf4j在根據綁器把日志交給具體的日志實現工具。
如果log4j-over-slf4j.jar 和 slf4j-log4j12.jar共存話,就像兩個人互相推卸責任不干活,最終陷入死循環。
可以將對應的jar排除
<exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion>
針對情況<1>,一般是在classpath下出現了多個logback可以靜態綁定的日志實現,slf4j會默認選擇其中的一種實現來綁定,如若出現其他錯誤再仔細排查。
針對情況<2>,一般是在classpath下出現了多個logback配置文件,一般是在使用了maven不同的模塊中使用了不同的配置,其實可以將所有模塊公用一個配置。
正如stackoverflow上所述一樣,
As far as I'm concerned, you should never package a logging config file (logback.xml, log4j.properties, or what have you) inside a JAR file. The whole point of even having a logging config file is to make it easy for the end-user to adjust logging levels by editing the file. Burying it in an archive defeats the purpose of that, because to change the logging level your user has to expand the archive, edit the file, and then re-package the archive.
Here's my preferred layout for a deployed application. It's a little bit more work to set up, but IMO is worth the trouble because it gives you the flexibility and ease of configuration that an uberjar doesn't.
my-app/
bin/
run-app.sh
config/
logback.xml
lib/
my-lib.jar
my-app.jar
Your run-app.sh script would look something like:
BIN=`dirname "$0"`
BASE=$BIN/..
java -cp "$BASE/config:$BASE/lib/*" my-app.main
This has the advantage that, by putting the config directory at the front of the classpath, any logging config file found there should take precedence over anything that might be found in one of the JARs (e.g. included by a third-party library that you have no control over).
你最好不要將配置文件打包到jar包中,因為增加配置文件本質上就是為了方便用戶調整一下參數配置,如果將它打包到jar包中,你需要修改指定的文件並從新打包,上線。這樣的優勢在於你可以方便的修改一些線上的配置,並且實時被加載生效。
在這里logback啟動的時候,加載配置文件的順序:
1. 如果指定了 logback.configurationFile性,將使用這個屬性的地址,如啟動指定了: java -Dlogback.configurationFile=/path/to/config.xml
2.如果沒有配置上面的屬性, 將會在classpath中查找 logback.groovy ,如果沒有找到文件,依次查找 logback-test.xml, logback.xml 。
3.如果都沒有找到, jdk版本如果是 jdk6+,會調用ServiceLoader 查找 com.qos.logback.classic.spi.Configurator接口的第一個實現類
4.如果上面都沒有,將使用ch.qos.logback.classic.BasicConfigurator,在控制台輸出日志
我將跟進一些源碼,粗略的看下都發生了寫什么。
入口:org.slf4j.impl.StaticLoggerBinder
void init() { try { try {
//這里開始初始化配置 (new ContextInitializer(this.defaultLoggerContext)).autoConfig(); } catch (JoranException var2) { Util.report("Failed to auto configure default logger context", var2); } StatusPrinter.printInCaseOfErrorsOrWarnings(this.defaultLoggerContext); this.contextSelectorBinder.init(this.defaultLoggerContext, KEY); this.initialized = true; } catch (Throwable var3) { Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", var3); } }
ch.qos.logback.classic.util.ContextInitializer:
public void autoConfig() throws JoranException { StatusListenerConfigHelper.installIfAsked(this.loggerContext); //加載配置文件,logback.groovy(有加載,無繼續)---->logback-test.xml(有加載,無繼續)------>logback.xml URL url = this.findURLOfDefaultConfigurationFile(true); if(url != null) { //讀取設置配置信息 this.configureByResource(url); } else { //找不到配置文件,設置默認 BasicConfigurator.configure(this.loggerContext); } } //加載配置文件,logback.groovy(有加載,無繼續)---->logback-test.xml(有加載,無繼續)------>logback.xml //getResource()---->statusOnResourceSearch()----->multiplicityWarning()--->打印Resource [logback.xml] occurs multiple times on the classpath public URL findURLOfDefaultConfigurationFile(boolean updateStatus) { ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this); URL url = this.findConfigFileURLFromSystemProperties(myClassLoader, updateStatus); if(url != null) { return url; } else { url = this.getResource("logback.groovy", myClassLoader, updateStatus); if(url != null) { return url; } else { url = this.getResource("logback-test.xml", myClassLoader, updateStatus); return url != null?url:this.getResource("logback.xml", myClassLoader, updateStatus); } } } private URL getResource(String filename, ClassLoader myClassLoader, boolean updateStatus) { URL url = Loader.getResource(filename, myClassLoader); if(updateStatus) { this.statusOnResourceSearch(filename, myClassLoader, url); } return url; } private void statusOnResourceSearch(String resourceName, ClassLoader classLoader, URL url) { StatusManager sm = this.loggerContext.getStatusManager(); if(url == null) { sm.add(new InfoStatus("Could NOT find resource [" + resourceName + "]", this.loggerContext)); } else { sm.add(new InfoStatus("Found resource [" + resourceName + "] at [" + url.toString() + "]", this.loggerContext)); this.multiplicityWarning(resourceName, classLoader); } } private void multiplicityWarning(String resourceName, ClassLoader classLoader) { Set urlSet = null; StatusManager sm = this.loggerContext.getStatusManager(); try { urlSet = Loader.getResourceOccurenceCount(resourceName, classLoader); } catch (IOException var7) { sm.add(new ErrorStatus("Failed to get url list for resource [" + resourceName + "]", this.loggerContext, var7)); } if(urlSet != null && urlSet.size() > 1) { sm.add(new WarnStatus("Resource [" + resourceName + "] occurs multiple times on the classpath.", this.loggerContext)); Iterator i$ = urlSet.iterator(); while(i$.hasNext()) { URL url = (URL)i$.next(); sm.add(new WarnStatus("Resource [" + resourceName + "] occurs at [" + url.toString() + "]", this.loggerContext)); } } }
但是這個時候,如果有兩個配置文件,以哪個文件為主呢?詳見下篇
完整源代碼代碼:
ch.qos.logback.classic.util.ContextInitializer:

// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package ch.qos.logback.classic.util; import ch.qos.logback.classic.BasicConfigurator; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.gaffer.GafferUtil; import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.classic.util.EnvUtil; import ch.qos.logback.classic.util.StatusListenerConfigHelper; import ch.qos.logback.core.joran.spi.JoranException; import ch.qos.logback.core.status.ErrorStatus; import ch.qos.logback.core.status.InfoStatus; import ch.qos.logback.core.status.StatusManager; import ch.qos.logback.core.status.WarnStatus; import ch.qos.logback.core.util.Loader; import ch.qos.logback.core.util.OptionHelper; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.Set; public class ContextInitializer { public static final String GROOVY_AUTOCONFIG_FILE = "logback.groovy"; public static final String AUTOCONFIG_FILE = "logback.xml"; public static final String TEST_AUTOCONFIG_FILE = "logback-test.xml"; public static final String CONFIG_FILE_PROPERTY = "logback.configurationFile"; public static final String STATUS_LISTENER_CLASS = "logback.statusListenerClass"; public static final String SYSOUT = "SYSOUT"; final LoggerContext loggerContext; public ContextInitializer(LoggerContext loggerContext) { this.loggerContext = loggerContext; } public void configureByResource(URL url) throws JoranException { if(url == null) { throw new IllegalArgumentException("URL argument cannot be null"); } else { if(url.toString().endsWith("groovy")) { if(EnvUtil.isGroovyAvailable()) { GafferUtil.runGafferConfiguratorOn(this.loggerContext, this, url); } else { StatusManager configurator = this.loggerContext.getStatusManager(); configurator.add(new ErrorStatus("Groovy classes are not available on the class path. ABORTING INITIALIZATION.", this.loggerContext)); } } if(url.toString().endsWith("xml")) { JoranConfigurator configurator1 = new JoranConfigurator(); configurator1.setContext(this.loggerContext); configurator1.doConfigure(url); } } } void joranConfigureByResource(URL url) throws JoranException { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(this.loggerContext); configurator.doConfigure(url); } private URL findConfigFileURLFromSystemProperties(ClassLoader classLoader, boolean updateStatus) { String logbackConfigFile = OptionHelper.getSystemProperty("logback.configurationFile"); if(logbackConfigFile != null) { URL result = null; URL e1; try { result = new URL(logbackConfigFile); URL e = result; return e; } catch (MalformedURLException var13) { result = Loader.getResource(logbackConfigFile, classLoader); if(result != null) { URL f1 = result; return f1; } File f = new File(logbackConfigFile); if(!f.exists() || !f.isFile()) { return null; } try { result = f.toURI().toURL(); e1 = result; } catch (MalformedURLException var12) { return null; } } finally { if(updateStatus) { this.statusOnResourceSearch(logbackConfigFile, classLoader, result); } } return e1; } else { return null; } } public URL findURLOfDefaultConfigurationFile(boolean updateStatus) { ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this); URL url = this.findConfigFileURLFromSystemProperties(myClassLoader, updateStatus); if(url != null) { return url; } else { url = this.getResource("logback.groovy", myClassLoader, updateStatus); if(url != null) { return url; } else { url = this.getResource("logback-test.xml", myClassLoader, updateStatus); return url != null?url:this.getResource("logback.xml", myClassLoader, updateStatus); } } } private URL getResource(String filename, ClassLoader myClassLoader, boolean updateStatus) { URL url = Loader.getResource(filename, myClassLoader); if(updateStatus) { this.statusOnResourceSearch(filename, myClassLoader, url); } return url; } public void autoConfig() throws JoranException { StatusListenerConfigHelper.installIfAsked(this.loggerContext); URL url = this.findURLOfDefaultConfigurationFile(true); if(url != null) { this.configureByResource(url); } else { BasicConfigurator.configure(this.loggerContext); } } private void multiplicityWarning(String resourceName, ClassLoader classLoader) { Set urlSet = null; StatusManager sm = this.loggerContext.getStatusManager(); try { urlSet = Loader.getResourceOccurenceCount(resourceName, classLoader); } catch (IOException var7) { sm.add(new ErrorStatus("Failed to get url list for resource [" + resourceName + "]", this.loggerContext, var7)); } if(urlSet != null && urlSet.size() > 1) { sm.add(new WarnStatus("Resource [" + resourceName + "] occurs multiple times on the classpath.", this.loggerContext)); Iterator i$ = urlSet.iterator(); while(i$.hasNext()) { URL url = (URL)i$.next(); sm.add(new WarnStatus("Resource [" + resourceName + "] occurs at [" + url.toString() + "]", this.loggerContext)); } } } private void statusOnResourceSearch(String resourceName, ClassLoader classLoader, URL url) { StatusManager sm = this.loggerContext.getStatusManager(); if(url == null) { sm.add(new InfoStatus("Could NOT find resource [" + resourceName + "]", this.loggerContext)); } else { sm.add(new InfoStatus("Found resource [" + resourceName + "] at [" + url.toString() + "]", this.loggerContext)); this.multiplicityWarning(resourceName, classLoader); } } }
ch.qos.logback.core.util.Loader:

// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package ch.qos.logback.core.util; import ch.qos.logback.core.Context; import ch.qos.logback.core.util.OptionHelper; import java.io.IOException; import java.net.URL; import java.security.AccessControlException; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Enumeration; import java.util.HashSet; import java.util.Set; public class Loader { static final String TSTR = "Caught Exception while in Loader.getResource. This may be innocuous."; private static boolean ignoreTCL = false; public static final String IGNORE_TCL_PROPERTY_NAME = "logback.ignoreTCL"; private static boolean HAS_GET_CLASS_LOADER_PERMISSION = false; public Loader() { } public static Set<URL> getResourceOccurenceCount(String resource, ClassLoader classLoader) throws IOException { HashSet urlSet = new HashSet(); Enumeration urlEnum = classLoader.getResources(resource); while(urlEnum.hasMoreElements()) { URL url = (URL)urlEnum.nextElement(); urlSet.add(url); } return urlSet; } public static URL getResource(String resource, ClassLoader classLoader) { try { return classLoader.getResource(resource); } catch (Throwable var3) { return null; } } public static URL getResourceBySelfClassLoader(String resource) { return getResource(resource, getClassLoaderOfClass(Loader.class)); } public static ClassLoader getTCL() { return Thread.currentThread().getContextClassLoader(); } public static Class loadClass(String clazz, Context context) throws ClassNotFoundException { ClassLoader cl = getClassLoaderOfObject(context); return cl.loadClass(clazz); } public static ClassLoader getClassLoaderOfObject(Object o) { if(o == null) { throw new NullPointerException("Argument cannot be null"); } else { return getClassLoaderOfClass(o.getClass()); } } public static ClassLoader getClassLoaderAsPrivileged(final Class clazz) { return !HAS_GET_CLASS_LOADER_PERMISSION?null:(ClassLoader)AccessController.doPrivileged(new PrivilegedAction() { public ClassLoader run() { return clazz.getClassLoader(); } }); } public static ClassLoader getClassLoaderOfClass(Class clazz) { ClassLoader cl = clazz.getClassLoader(); return cl == null?ClassLoader.getSystemClassLoader():cl; } public static Class loadClass(String clazz) throws ClassNotFoundException { if(ignoreTCL) { return Class.forName(clazz); } else { try { return getTCL().loadClass(clazz); } catch (Throwable var2) { return Class.forName(clazz); } } } static { String ignoreTCLProp = OptionHelper.getSystemProperty("logback.ignoreTCL", (String)null); if(ignoreTCLProp != null) { ignoreTCL = OptionHelper.toBoolean(ignoreTCLProp, true); } HAS_GET_CLASS_LOADER_PERMISSION = ((Boolean)AccessController.doPrivileged(new PrivilegedAction() { public Boolean run() { try { AccessController.checkPermission(new RuntimePermission("getClassLoader")); return Boolean.valueOf(true); } catch (AccessControlException var2) { return Boolean.valueOf(false); } } })).booleanValue(); } }
參考:
http://stackoverflow.com/questions/18263139/how-do-i-suppress-a-inherited-projects-logback-xml-file-2-logback-xml-in-a-sing
http://stackoverflow.com/questions/3401051/suppress-all-logback-output-to-console
http://jira.qos.ch/browse/LOGBACK-337