热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

项目存在多个logback配置

以下两个是我在使用slf4j+logback时候日志提示的问题,问题不大,都是WARN,并不真正影响运行,但是结果可能不是你希望的结果。SLF4J:Foundbindingi

以下两个是我在使用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-apiartifactId>
<groupId>org.slf4jgroupId>
exclusion>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>log4j-over-slf4jartifactId>
exclusion>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>

 

 

针对情况<1>,一般是在classpath下出现了多个logback可以静态绑定的日志实现,slf4j会默认选择其中的一种实现来绑定,如若出现其他错误再仔细排查。

在这里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,在控制台输出日志 

 

 针对情况<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包中,你需要修改指定的文件并从新打包,上线。这样的优势在于你可以方便的修改一些线上的配置,并且实时被加载生效。

我将跟进一些源码,粗略的看下都发生了写什么。

入口: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);
}
}
}


View Code

 

 

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


View Code

 

 但是这个时候,如果有两个配置文件,以哪个文件为主呢?下篇解释。

 

参考:

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

 


推荐阅读
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
author-avatar
摩擦摩擦魔鬼的步伐
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有