How to disable log4j logging in Http Client 4.1 to log to FileAppender

喜夏-厌秋 提交于 2019-12-10 15:23:40

问题


Here is my log4j configuration for a HTTP client:

log4j.appender.HTTPCLIENT_APPDR=com.xxx.log.FileAppender
log4j.appender.HTTPCLIENT_APPDR.File=${user.dir}/log/access.log
log4j.appender.HTTPCLIENT_APPDR.layout=org.apache.log4j.PatternLayout
log4j.appender.HTTPCLIENT_APPDR_APPDR.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss SSS}ms %-5p [%t] - %m%n
log4j.appender.HTTPCLIENT_APPDR.MaxFileSize=20000KB
log4j.appender.HTTPCLIENT_APPDR.MaxBackupIndex=30
log4j.logger.org.apache.http=DEBUG,HTTPCLIENT_APPDR

I would like it to turn off the httpclient logging from the CODE depending on the environment I am in (I know how to disable it from the log4j.properties).

I tried inserting these lines:

+    System.setProperty("log4j.logger.org.apache.http", "ERROR"); 

or

+    Logger.getLogger("log4j.logger.org.apache.http").setLevel(Level.off)

in the start of my application but it does not work.

  1. Can I access the log4j properties from the System class?
  2. When I look at the Logger.getLogger("log4j.logger.org.apache.http") the level is null? Should it not be DEBUG?

What worked finally,

Logger.getLogger("org.apache.http").setLevel(org.apache.log4j.Level.OFF);

I was not using the right key.

Regards,


回答1:


What worked finally, Logger.getLogger("org.apache.http").setLevel(org.apache.log4j.Level.OFF); I was not using the right key.




回答2:


If you have a log4j.properties file, add this line

log4j.logger.org.apache.http=WARN

This is only tested in Apache HttpClient 4



来源:https://stackoverflow.com/questions/12935923/how-to-disable-log4j-logging-in-http-client-4-1-to-log-to-fileappender

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!