commons-logging和log4j多配置文件问题的定位

现象
项目里使用的commons-httclient组件,莫名其妙的打印debug信息,
[      0]  DEBUG - .commons.httpclient.HttpClient - Java version: 1.6.0_24 
[      0]  DEBUG - .commons.httpclient.HttpClient - Java vendor: Sun Microsystems Inc. 
[      1]  DEBUG - .commons.httpclient.HttpClient - Operating system name: Windows 7 
[      1]  DEBUG - .commons.httpclient.HttpClient - Operating system architecture: amd64 
[      1]  DEBUG - .commons.httpclient.HttpClient - Operating system version: 6.1 
[     42]  DEBUG - .commons.httpclient.HttpClient - SUN 1.6: SUN (DSA key/parameter generation; DSA signing; SHA-1, MD5 digests; SecureRandom; X.509 certificates; JKS keystore; PKIX CertPathValidator; PKIX CertPathBuilder; LDAP, Collection CertStores, JavaPolicy Policy; JavaLoginConfig Configuration) 
[     42]  DEBUG - .commons.httpclient.HttpClient - SunRsaSign 1.5: Sun RSA signature provider 
[     42]  DEBUG - .commons.httpclient.HttpClient - SunJSSE 1.6: Sun JSSE provider(PKCS12, SunX509 key/trust factories, SSLv3, TLSv1) 
[     42]  DEBUG - .commons.httpclient.HttpClient - SunJCE 1.6: SunJCE Provider (implements RSA, DES, Triple DES, AES, Blowfish, ARCFOUR, RC2, PBE, Diffie-Hellman, HMAC) 
[     42]  DEBUG - .commons.httpclient.HttpClient - SunJGSS 1.0: Sun (Kerberos v5, SPNEGO) 
[     42]  DEBUG - .commons.httpclient.HttpClient - SunSASL 1.5: Sun SASL provider(implements client mechanisms for: DIGEST-MD5, GSSAPI, EXTERNAL, PLAIN, CRAM-MD5; server mechanisms for: DIGEST-MD5, GSSAPI, CRAM-MD5) 
[     42]  DEBUG - .commons.httpclient.HttpClient - XMLDSig 1.0: XMLDSig (DOM XMLSignatureFactory; DOM KeyInfoFactory) 
[     42]  DEBUG - .commons.httpclient.HttpClient - SunPCSC 1.6: Sun PC/SC provider 
[     44]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.useragent = Jakarta Commons-HttpClient/3.1 
[     45]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.protocol.version = HTTP/1.1 
[     46]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.connection-manager.class = class org.apache.commons.httpclient.SimpleHttpConnectionManager 
[     46]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.protocol.cookie-policy = default 
[     46]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.protocol.element-charset = US-ASCII 
[     46]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.protocol.content-charset = ISO-8859-1 
[     47]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.method.retry-handler = org.apache.commons.httpclient.DefaultHttpMethodRetryHandler@3e018c74 
[     47]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.dateparser.patterns = [EEE, dd MMM yyyy HH:mm:ss zzz, EEEE, dd-MMM-yy HH:mm:ss zzz, EEE MMM d HH:mm:ss yyyy, EEE, dd-MMM-yyyy HH:mm:ss z, EEE, dd-MMM-yyyy HH-mm-ss z, EEE, dd MMM yy HH:mm:ss z, EEE dd-MMM-yyyy HH:mm:ss z, EEE dd MMM yyyy HH:mm:ss z, EEE dd-MMM-yyyy HH-mm-ss z, EEE dd-MMM-yy HH:mm:ss z, EEE dd MMM yy HH:mm:ss z, EEE,dd-MMM-yy HH:mm:ss z, EEE,dd-MMM-yyyy HH:mm:ss z, EEE, dd-MM-yyyy HH:mm:ss z] 
[     50]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.connection.timeout = 10000 
[     50]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.socket.timeout = 10000 
[     50]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.protocol.cookie-policy = compatibility 
[     50]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.protocol.single-cookie-header = true 
[     50]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.protocol.content-charset = UTF-8 
[     65]  DEBUG - lient.params.DefaultHttpParams - Set parameter http.method.response.buffer.warnlimit = 10485760 


问题定位
项目里配置了多个日志组件,log4j\slf4j\commons-logging
不知道到底是哪个组件打印的。

看commons-httpclient的 官方说明,用的是commons-logging组件。
引用

Being a library HttpClient is not to dictate which logging framework the user has to use. Therefore HttpClient utilizes the logging interface provided by the Commons Logging package. Commons Logging provides a simple and generalized log interface to various logging packages. By using Commons Logging, HttpClient can be configured for a variety of different logging behaviours. That means the user will have to make a choice which logging framework to use. By default Commons Logging supports the following logging frameworks:

    Log4J
    java.util.logging
    SimpleLog (internal to Commons Logging)


而commons-logging,如果有其它的日志组件,会自动的适配到其它的日志组件,比如log4j\slf4j等。
到底是哪一个呢?
commons-logging 官方文档说,可以打印commons-logging的诊断信息。 眼泪哗哗的,一个项目作的优秀不优秀,细节上就能体现出来。
在执行代码的第一行,加入以下属性。
		System.setProperty("org.apache.commons.logging.diagnostics.dest", "JCL.log");

或者配置到eclipse里,

commons-logging和log4j多配置文件问题的定位_第1张图片
会在项目根目录下生成JCL.log文件。
[LogFactory from sun.misc.Launcher$AppClassLoader@1435494339] Created object org.apache.commons.logging.impl.LogFactoryImpl@808460461 to manage classloader sun.misc.Launcher$AppClassLoader@1435494339
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] Discovering a Log implementation...
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] Trying to get configuration for item org.apache.commons.logging.Log.allowFlawedContext
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] No LogFactory attribute found for org.apache.commons.logging.Log.allowFlawedContext
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] No system property found for property org.apache.commons.logging.Log.allowFlawedContext
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] No configuration defined for item org.apache.commons.logging.Log.allowFlawedContext
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] Trying to get configuration for item org.apache.commons.logging.Log.allowFlawedDiscovery
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] No LogFactory attribute found for org.apache.commons.logging.Log.allowFlawedDiscovery
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] No system property found for property org.apache.commons.logging.Log.allowFlawedDiscovery
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] No configuration defined for item org.apache.commons.logging.Log.allowFlawedDiscovery
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] Trying to get configuration for item org.apache.commons.logging.Log.allowFlawedHierarchy
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] No LogFactory attribute found for org.apache.commons.logging.Log.allowFlawedHierarchy
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] No system property found for property org.apache.commons.logging.Log.allowFlawedHierarchy
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] [ENV] No configuration defined for item org.apache.commons.logging.Log.allowFlawedHierarchy
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] Trying to get log class from attribute 'org.apache.commons.logging.Log'
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] Attempting to load user-specified log class 'org.apache.commons.logging.impl.Log4JLogger'...
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] Attempting to instantiate 'org.apache.commons.logging.impl.Log4JLogger'
[LogFactoryImpl@808460461 from sun.misc.Launcher$AppClassLoader@1435494339] Trying to load 'org.apache.commons.logging.impl.Log4JLogger' from classloader sun.misc.Launcher$AppClassLoader@1435494339

看到了吧,使用的是log4j。
既然使用的是log4j,那问题还挺麻烦,项目里的多个jar包里,都有log4j的配置文件,
用的到底是哪一个呢?
stackoverflow上的大神说,加一个启动参数
引用

java -Dlog4j.debug

对我们来说,加到eclipse里,如图

commons-logging和log4j多配置文件问题的定位_第2张图片
再次运行测试用例,多了下面的内容
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@558fe7c3.
log4j: Using URL [jar:file:/xxxx1.0-SNAPSHOT.jar!/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Level value for root is  [].
log4j: root level set to DEBUG
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Setting property [encoding] to [UTF-8].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [[%7r] %6p - %30.30c - %m 
].
log4j: Adding appender named [CONSOLE] to category [root].
log4j: Class name: [org.apache.log4j.FileAppender]
log4j: Setting property [append] to [false].
log4j: Setting property [file] to [/xxx-debug.log].
log4j: Setting property [encoding] to [UTF-8].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d [%7r] %6p - %30.30c - %m 
].
log4j: setFile called: /xxx-debug.log, false
log4j: setFile ended
log4j: Adding appender named [FILE-DEBUG] to category [root].

看到了吧,我们用的哪个jar包的log4j的配置文件

log4j: Using URL [jar:file:/xxxx1.0-SNAPSHOT.jar!/log4j.xml] for automatic log4j configuration.

解决办法
找那个jar包的提供者,或者动态修改log4j的属性。
http://blog.csdn.net/attilax/article/details/21157659
引用

项目里面也许有log4j.xml,这个的优先级比property的高
删除或者reset cfg
//        core.logger.info("xxx");
//        org.apache.log4j.LogManager.resetConfiguration();
//        org.apache.log4j.PropertyConfigurator
//                .configure("D:\\workspace\\imServer\\src\\log4j.properties");
//        core.logger("xxxt");
//
//        // (2)重新设置属性
//
//        Properties log4jCfg = log4jloadAgain.getLog4jProperties();
//
//        PropertyConfigurator.configure(log4jCfg);
//
//        core.logger.info("xxxt3");
//        log.info("xx4");
        core.logger.info("xx");



方便你加参数
-Dlog4j.debug 
-Dorg.apache.commons.logging.diagnostics.dest=JCL.log

你可能感兴趣的:(java,log4j)