日志消息不会写入日志文件中

时间:2012-01-16 12:09:26

标签: java junit log4j

我已经使用Log4j配置了一个项目。我已使用log4.properties文件配置了log4j。 在测试运行时,它会创建myLog.log文件,但不会将任何内容写入日志文件。 我们使用的Appender为RollingFileAppenderlog4j.rootLogger=DEBUG

任何人都可以提出可能导致此问题的原因吗?

这是项目的log4j.properties。

log4j.rootLogger= DEBUG, ConsoleAppender, Rolling 
log4j.appender.ConsoleAppender=org.apache.log4j.ConsoleAppender 
log4j.appender.ConsoleAppender.layout=org.apache.log4j.PatternLayout 

log4j.appender.ConsoleAppender.layout.ConversionPattern=%d %-2p %c.%M(): %m%n 
log4j.appender.Rolling=org.apache.log4j.RollingFileAppender 
log4j.appender.Rolling.MaxFileSize=5MB 
log4j.appender.Rolling.MaxBackupIndex=2 
log4j.appender.Rolling.File=D:\Logs\myProj.log 
log4j.appender.Rolling.layout = org.apache.log4j.PatternLayout 
log4j.appender.Rolling.layout.ConversionPattern=%d %-4p %c.%M(): %m%n 
## Following two lines have been removed now.
log4j.logger.com=WARN 
log4j.logger.org=WARN

收到登录日志文件:

2012-01-16 22:02:08,534 DEBUG org.apache.http.headers.receiveResponseHeader(): << content-type: application/json; charset=UTF-8
2012-01-16 22:02:08,534 DEBUG org.apache.http.headers.receiveResponseHeader(): << connection: close
2012-01-16 22:02:08,534 DEBUG org.apache.http.headers.receiveResponseHeader(): << content-length: 236
2012-01-16 22:02:08,550 DEBUG org.apache.http.headers.receiveResponseHeader(): << server: httpd.js
2012-01-16 22:02:08,550 DEBUG org.apache.http.headers.receiveResponseHeader(): << date: Tue, 17 Jan 2012 03:02:06 GMT
2012-01-16 22:02:08,566 DEBUG org.apache.http.wire.wire(): << "{"name":"clickElement","sessionId":"2fad55cf-670e-44f3-ab60-7d2f76f641e6","status":0,"value":"This action is final and cannot be undone?"}"
2012-01-16 22:02:08,566 DEBUG org.apache.http.impl.conn.DefaultClientConnection.shutdown(): Connection shut down
2012-01-16 22:02:08,566 DEBUG org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.releaseConnection(): Released connection is not reusable.
2012-01-16 22:02:08,566 DEBUG org.apache.http.impl.conn.tsccm.ConnPoolByRoute.freeEntry(): Releasing connection [HttpRoute[{}->http://127.0.0.1:7055]][null]
2012-01-16 22:02:08,566 DEBUG org.apache.http.impl.conn.tsccm.ConnPoolByRoute.notifyWaitingThread(): Notifying no-one, there are no waiting threads
2012-01-16 22:02:10,566 DEBUG org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.getConnection(): Get connection: HttpRoute[{}->http://127.0.0.1:7055], timeout = 120000
2012-01-16 22:02:10,566 DEBUG org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(): [HttpRoute[{}->http://127.0.0.1:7055]] total kept alive: 0, total issued: 0, total allocated: 0 out of 2000
2012-01-16 22:02:10,566 DEBUG org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getFreeEntry(): No free connections [HttpRoute[{}->http://127.0.0.1:7055]][null]
2012-01-16 22:02:10,566 DEBUG org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(): Available capacity: 2000 out of 2000 [HttpRoute[{}->http://127.0.0.1:7055]][null]
2012-01-16 22:02:10,581 DEBUG org.apache.http.impl.conn.tsccm.ConnPoolByRoute.createEntry(): Creating new connection [HttpRoute[{}->http://127.0.0.1:7055]]
2012-01-16 22:02:10,597 DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(): Connecting to 127.0.0.1:7055
2012-01-16 22:02:10,597 DEBUG org.apache.http.client.protocol.RequestAddCookies.process(): CookieSpec selected: best-match
2012-01-16 22:02:10,597 DEBUG org.apache.http.client.protocol.RequestAuthCache.process(): Auth cache not set in the context
2012-01-16 22:02:10,597 DEBUG org.apache.http.impl.client.DefaultHttpClient.tryExecute(): Attempt 1 to execute request
2012-01-16 22:02:10,597 DEBUG org.apache.http.impl.conn.DefaultClientConnection.sendRequestHeader(): Sending request: GET /hub/session/2fad55cf-670e-44f3-ab60-7d2f76f641e6/alert_text HTTP/1.1
2012-01-16 22:02:10,597 DEBUG org.apache.http.wire.wire(): >> "GET /hub/session/2fad55cf-670e-44f3-ab60-7d2f76f641e6/alert_text HTTP/1.1[\r][\n]"
2012-01-16 22:02:10,597 DEBUG org.apache.http.wire.wire(): >> "Accept: application/json, image/png[\r][\n]"
2012-01-16 22:02:10,597 DEBUG org.apache.http.wire.wire(): >> "Host: 127.0.0.1:7055[\r][\n]"
2012-01-16 22:02:10,597 DEBUG org.apache.http.wire.wire(): >> "Connection: Keep-Alive[\r][\n]"
2012-01-16 22:02:10,597 DEBUG org.apache.http.wire.wire(): >> "[\r][\n]"
2012-01-16 22:02:10,597 DEBUG org.apache.http.headers.sendRequestHeader(): >> GET /hub/session/2fad55cf-670e-44f3-ab60-7d2f76f641e6/alert_text HTTP/1.1
2012-01-16 22:02:10,597 DEBUG org.apache.http.headers.sendRequestHeader(): >> Accept: application/json, image/png
2012-01-16 22:02:10,597 DEBUG org.apache.http.headers.sendRequestHeader(): >> Host: 127.0.0.1:7055
2012-01-16 22:02:10,597 DEBUG org.apache.http.headers.sendRequestHeader(): >> Connection: Keep-Alive
2012-01-16 22:02:10,612 DEBUG org.apache.http.wire.wire(): << "HTTP/1.1 200 OK[\r][\n]"
2012-01-16 22:02:10,612 DEBUG org.apache.http.wire.wire(): << "content-type: application/json; charset=UTF-8[\r][\n]"
2012-01-16 22:02:10,612 DEBUG org.apache.http.wire.wire(): << "connection: close[\r][\n]"
2012-01-16 22:02:10,612 DEBUG org.apache.http.wire.wire(): << "content-length: 236[\r][\n]"
2012-01-16 22:02:10,612 DEBUG org.apache.http.wire.wire(): << "server: httpd.js[\r][\n]"
2012-01-16 22:02:10,612 DEBUG org.apache.http.wire.wire(): << "date: Tue, 17 Jan 2012 03:02:10 GMT[\r][\n]"
2012-01-16 22:02:10,612 DEBUG org.apache.http.wire.wire(): << "[\r][\n]"
2012-01-16 22:02:10,612 DEBUG org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(): Receiving response: HTTP/1.1 200 OK

2 个答案:

答案 0 :(得分:2)

首先,确保在类路径中加载了log4j.properties。我假设如果它是一个webapp,也可以在WEB-INF/classes目录中找到该文件。但是因为你说文件正在创建,我认为这不是问题。

所以,试试这个

log4j.rootLogger= DEBUG, Console, Rolling 
log4j.appender.Console=org.apache.log4j.ConsoleAppender 
log4j.appender.Console.layout=org.apache.log4j.PatternLayout 

log4j.appender.Console.layout.ConversionPattern=%d %-2p %c.%M(): %m%n 
log4j.appender.Rolling=org.apache.log4j.RollingFileAppender 
log4j.appender.Rolling.MaxFileSize=5MB 
log4j.appender.Rolling.MaxBackupIndex=2 
log4j.appender.Rolling.File=D:\Logs\myProj.log 
log4j.appender.Rolling.layout = org.apache.log4j.PatternLayout 
log4j.appender.Rolling.layout.ConversionPattern=%d %-4p %c.%M(): %m%n 

请注意,我已删除了最后两行。

基本上,这些行将日志级别设置为WARN到以comorg开头的所有包。因此,除非您记录了任何WARN条消息,否则您将无法在日志文件中看到任何内容。

您可以通过执行类似

的操作来体验关卡
log4j.logger.com.foo.bar=DEBUG
log4j.logger.com.foo.bar.MyClass=TRACE

以上内容将在com.foo.bar中打印所有DEBUG日志,但会在com.foo.bar.MyClass级别打印TRACE个日志。

答案 1 :(得分:1)

或更详细:来自com或org-packages的日志记录请求没有日志级别&lt; WARN将被启用。所以它不会被转发到你的rootLogger。

引用:

  

记录器中级别为p的日志请求(已分配或​​已继承,以较大者为准)   如果p> = q,则启用适当的级别q。

  

给定记录器的每个已启用的日志记录请求都将转发到其中的所有appender   该记录器以及层次结构中较高的appender。

请参阅:http://logging.apache.org/log4j/1.2/manual.html