我工程里配的log4j.xml文件,但是由于想动态改变日志级别,所以引了logback-classic包,结果一直打印debug日志。
LoggerContext loggerContext= (LoggerContext) LoggerFactory.getILoggerFactory();
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
后来又加了这个依赖,放在logback-classic上面
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
2018-08-13 20:43:48.478:INFO::No Transaction manager found - if your webapp requires one, please configure one.
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2018-08-13 20:43:48.922:INFO:/:Initializing Spring root WebApplicationContext
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/m2space/repository/org/slf4j/slf4j-log4j12/1.7.21/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/m2space/repository/ch/qos/logback/logback-classic/1.2.0/logback-classic-1.2.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
然后又换了一下,把logback-classic放在上面,结果如下:
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2018-08-13 20:48:32.894:INFO:/:Initializing Spring root WebApplicationContext
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/m2space/repository/ch/qos/logback/logback-classic/1.2.0/logback-classic-1.2.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/m2space/repository/org/slf4j/slf4j-log4j12/1.7.21/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
再看logback-classic 默认level是debug,怪不得用这包不配logback.xml全打debug呢
public LoggerContext()
{
this.loggerCache = new ConcurrentHashMap();
this.loggerContextRemoteView = new LoggerContextVO(this);
this.root = new Logger("ROOT", null, this);
this.root.setLevel(Level.DEBUG);
this.loggerCache.put("ROOT", this.root);
initEvaluatorMap();
this.size = 1;
this.frameworkPackages = new ArrayList();
}
那为什么那个是info呢,因为slf4j-log4j12的
StaticLoggerBinder无参构造方法中初始化loggerFactory为new Log4jLoggerFactory(),Log4jLoggerFactory的LogManager是org.apache.log4j.LogManager,默认是找log4j.xml
尝试把两个依赖都去掉,会打印如下:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
也就是slf4j-api的LoggerFactory的bind方法
private static final void bind()
{
try
{
Set<URL> staticLoggerBinderPathSet = null;
if (!isAndroid())
{
staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();
reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);
}
StaticLoggerBinder.getSingleton();
INITIALIZATION_STATE = 3;
reportActualBinding(staticLoggerBinderPathSet);
fixSubstituteLoggers();
replayEvents();
SUBST_FACTORY.clear();
}
catch (NoClassDefFoundError ncde)
{
String msg = ncde.getMessage();
if (messageContainsOrgSlf4jImplStaticLoggerBinder(msg))
{
INITIALIZATION_STATE = 4;
Util.report("Failed to load class \"org.slf4j.impl.StaticLoggerBinder\".");
Util.report("Defaulting to no-operation (NOP) logger implementation");
Util.report("See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.");
}
else
{
failedBinding(ncde);
throw ncde;
}
}
catch (NoSuchMethodError nsme)
{
String msg = nsme.getMessage();
if ((msg != null) && (msg.contains("org.slf4j.impl.StaticLoggerBinder.getSingleton()")))
{
INITIALIZATION_STATE = 2;
Util.report("slf4j-api 1.6.x (or later) is incompatible with this binding.");
Util.report("Your binding is version 1.5.5 or earlier.");
Util.report("Upgrade your binding to version 1.6.x.");
}
throw nsme;
}
catch (Exception e)
{
failedBinding(e);
throw new IllegalStateException("Unexpected initialization failure", e);
}
}
那,这时候调用接口,返回成功了,中间明明有logger.info打印语句,可是控制台就没有显示出来,以后要用什么可以心里有点逼数了。
网友评论