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

[b]现象[/b]
项目里使用的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


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

看commons-httpclient的[url=http://hc.apache.org/httpclient-3.x/logging.html]官方说明[/url],用的是commons-logging组件。
[quote]
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)

[/quote]
而commons-logging,如果有其它的日志组件,会自动的适配到其它的日志组件,比如log4j\slf4j等。
到底是哪一个呢?
commons-logging[url=http://commons.apache.org/proper/commons-logging/troubleshooting.html#When_To_Use_Diagnostic_Logging]官方文档[/url]说,可以打印commons-logging的诊断信息。[b]眼泪哗哗的,一个项目作的优秀不优秀,细节上就能体现出来。[/b]
在执行代码的第一行,加入以下属性。

System.setProperty("org.apache.commons.logging.diagnostics.dest", "JCL.log");

或者配置到eclipse里,

[img]http://dl2.iteye.com/upload/attachment/0103/3193/582b5f54-7986-330b-9eea-5a9041f37f30.jpg[/img]
会在项目根目录下生成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的配置文件,
用的到底是哪一个呢?
[url=http://stackoverflow.com/questions/5402953/log4j-properties-not-picked-up-from-jar]stackoverflow[/url]上的大神说,加一个启动参数
[quote]
java -Dlog4j.debug
[/quote]
对我们来说,加到eclipse里,如图

[img]http://dl2.iteye.com/upload/attachment/0103/3201/7542c826-d4c1-38b1-837e-a4ddf6296137.jpg[/img]
再次运行测试用例,多了下面的内容
[code="java"]
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 .
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 .
[/code]
看到了吧,我们用的哪个jar包的log4j的配置文件

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

[b]解决办法[/b]
找那个jar包的提供者,或者动态修改log4j的属性。
[url]http://blog.csdn.net/attilax/article/details/21157659[/url]
[quote]
项目里面也许有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");
[/quote]


[b]方便你加参数[/b]

-Dlog4j.debug
-Dorg.apache.commons.logging.diagnostics.dest=JCL.log
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值