Reputation: 3454
I´m using commons-httpclient
3.1 in an integration test suite. The default logging for HttpClient
is extremely noisy and I can't seem to turn it off. I've tried following the instructions here but none of them make any difference.
Mostly I just need to make the org.apache.http.wire logger shut up. Part of the problem is that I don't know what type of logger HttpClient
is trying to use. I've never used this library before. I tried creating a log4j.properties file and dropping it in my test/resources folder, modifying the master logging.properties file in jre/lib
, and sending in the various logging options to Maven as specified on the logging page, and none of them make any difference.
UPDATE: A correction: it appears the output in question is actually originating through jwebunit's usage of HttpClient
, not my own. Either way, it's not desirable.
UPDATE: Thanks for the attempts so far. I've tried everything suggested below but still no luck. I have a file commons-logging.properties
in my src/test/resources
folder with the following contents
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.Log4jFactory
log4j.configuration=log4j.properties
and a file log4j.properties in the same folder with the following contents
log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
#This is the line that should make httpclient shut up
log4j.logger.org.apache.http=ERROR
However, when I run my tests I still get a bunch of output like this:
21:57:41.413 [main] DEBUG org.apache.http.wire - << " [\r][\n]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << "[\r][\n]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << " [\r][\n]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << " </ul>[\n]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << " [\n]"
21:57:41.424 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << "[\r][\n]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << "[\r][\n]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << " </div>[\r][\n]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << " </li>[\r][\n]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << " [\r][\n]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << " [\r][\n]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << " </ul>[\n]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "</div>[\n]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "</div>[\n]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "<div class="details">[\n]"
21:57:41.442 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "<div class="details-body details-precis ">[\n]
"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "<div class="details-state">[\n]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "</div>[\n]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "</div>[\n]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "</div>[\n]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "</div>[\n]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "</div>[\n]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[\n]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[\r][\n]"
Destroying 1 processes21:57:41.465 [main] DEBUG org.apache.http.wire - << "[\r][\n]"
This output for everything that comes across the wire is making this library unusable for me...that is until I can figure out how to turn it off. Is there anything special I need to do to get this log configuration read in?
Upvotes: 170
Views: 151423
Reputation: 9
Please add log4j2test.properties file into your resources directory of project to suppress default logging of httpclient.
log4j2test.properties file -
# Root logger level set to INFO
rootLogger.level = INFO
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = Console
# Define the Console appender
appender.console.type = Console
appender.console.name = Console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
# Suppress HttpClient logs
logger.httpclient.name = org.apache.http
logger.httpclient.level = WARN
logger.httpclient.additivity = false
logger.httpwire.name = org.apache.http.wire
logger.httpwire.level = WARN
logger.httpwire.additivity = false
# Other specific logger settings can be added here as needed
Explanation
Root Logger: The root logger is set to INFO, meaning only INFO, WARN, and ERROR level logs will be shown.
Console Appender: A console appender is defined to print logs to the console in a specific format.
Suppress HttpClient Logs: org.apache.http is set to WARN to suppress debug or info logs generated by the HttpClient.
org.apache.http.wire is also set to WARN to suppress wire-level logs (typically very verbose and used for debugging HTTP connections).
Paste this log4j2test.properties file in project's src/test/resources directory. When you run JUnit tests, this configuration will be automatically suppress unwanted logging from the Apache HttpClient.
Upvotes: 0
Reputation: 1631
There are many responses yet none appears to be complete. I had to cobble up answers and solutions from answers here and other locations, as well as my trial and errors. Finally, I am working with Log4j2.
First, create log4j2.xml
in the resources
directory. This directory is in the src/main/java
. My logger is very simple:
DEBUG
level.DEBUG
from very org.apache.http
package.<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%p %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.http" level="warn" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Second, set up your project pom.xml
file. It is critical to include not only the dependencies to enable Log4j2 but also the bridge JARs. The bridge JARs will tell other dependencies to explicitly use Log4J2.
...
<!-- Logging using Log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j-version}</version>
</dependency>
<!-- Bridges to use Log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j-version}</version>
</dependency>
...
Finally, in your code you set up the logger object:
private final Logger log = LogManager.getLogger(YourClass.class);
and use it as follows:
log.info("Your helpful log message.");
Hope it helps.
Upvotes: 0
Reputation: 25
Create a new file logback.xml
into /src/main/resources
and copy paste the following
<?xml version="1.0" encoding="UTF-8"?>
<!--<configuration>-->
<!-- <logger name="org.apache" level="INFO"/>-->
<!-- <logger name="httpclient" level="WARN"/>-->
<!--</configuration>-->
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Upvotes: 0
Reputation: 2463
Create a logback.xml file with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<logger name="org.apache" level="WARN"/>
<logger name="httpclient" level="WARN"/>
</configuration>
My project is set up with folder structure src->main->java and src->main->resources. Put this file in the resources folder.
Upvotes: 0
Reputation: 236
It may be caused by a dependency bringing in a logging config (which they really should not, unless it's a framework supposed to do so).
I've been plagued by the same issue for quite some time now and finally decided to look into this.
It turned out the issue is that my project had a dependency on http-builder-0.5.2.jar
which bundled a log4j.xml
file within itself. And sure enough, the log level for org.apache.http.wire was DEBUG! The way I found it was just to go through all the jar files in my dependencies and do "jar tvf" and grepping for log4j.
While this discovery led to the eventual solution of upping the version of my http-builder
dependency to 0.6, it still baffles me what must have gone through the developer's mind when bundling the log4j.xml
file into the jar file. Anyway, that's probably not relevant to this thread for now. But I figured it's useful to mention this solution I found given that when I was searching for a solution before now, mine never came up. Hopefully someone will find this useful.
Upvotes: 6
Reputation: 19
I was led to this post when searching for solution for similar problem. Tim's answer was very helpful. like Matt Baker, I just want to shut off httpClient log without too much configuration.
As we were not sure which logging implementation underneath common-logging was used, My solution was to force it using log4j by throwing log4j jar file in the class path. Default setting of log4j configuration shuts off common-httpclient debug output. Of course, to make it more robust, you may create common-logging.properties and log4j.properties files to further define your logging configurations.
Upvotes: 1
Reputation: 294
Try put
org.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog
in your commons-logging.properties
Upvotes: 1
Reputation: 2071
For Apache HttpClient 4.5.3, if you want to move the level for all Apache logging to WARN, use:
log4j.logger.org.apache=WARN
Upvotes: 1
Reputation: 46904
Spring may load the logging config from logback-spring.xml
in order to support Spring "extensions" (currently only profile-based logging levels).
I spent a couple hours experimenting before finding that logback-spring.xml
will cause HttpClient logging not to pick up the level.
Renaming to logback.xml
finally worked.
(I think this is a different cause worth it's own answer.)
Upvotes: 0
Reputation: 6519
Note: Some of this answer might repeat things you already know (or think you know), but there is a bit of mis-information floating around on this question, so I'm going to start at the beginning and spell it all out
Commons HttpClient uses Commons-Logging for all its logging needs.
Commons-Logging is not a full logging framework, but rather, is a wrapper around several existing logging frameworks
That means that when you want to control the logging output, you (mostly) end up configuring a library other than Commons-Logging, but because Commons-Logging wraps around several other libraries, it's hard for us to guess which one to configure without knowing your exactly setup.
Commons-Logging can log to log4j, but it can also log to java.util.logging
(JDK1.4 logging)
Commons-Logging tries to be smart and guess which logging framework you are already using, and send its logs to that.
If you don't already have a logging framework, and are running on a JRE that's 1.4 or above (which you really should be) then it will probably be sending its log messages to the JDK logging (java.util.logging
)
Relying on Commons-Logging's autodiscovery mechanism is prone to error. Simply adding log4j.jar
onto the classpath would cause it to switch which logging mechanism it uses, which probably isn't what you want
It is preferable for you to explicitly tell Commons-Logging which logging library to use
You can do this by creating a commons-logging.properties
file as per these instructions
The steps you want to follow to configure the commons-httpclient logging are
Decide which underlying logging framework you want to use. Historically, the common choices used to be log4j
or java.util.logging
. In 2022, the common choice is LogBack (also the Spring Framework's default).
Set-up the commons-logging properties file to point to the correct Log
implementation. e.g.:
To use log4j
, put this into the properties file:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
To use JDK logging set:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
.
To use Slf4J:
org.apache.commons.logging.Log=org.apache.logging.slf4j.SLF4JLogger
These can also be set as system properties (e.g. using -D
on the command line).
That's a lot of steps, but that's what it takes. The developers at Apache-commons tend to assume you'll already have a logging framework configured, and they can work out which one it is by auto-discovery.
If that's not true for you, then it tends to be a bit more work to get things running.
Upvotes: 35
Reputation: 1
Simply check the pom.xml file first and update accordingly if needed. And when you see the "[main] DEBUG org.apache.http.wire" multiple times on the screen, just let that happen completely and take time with doing nothing. Just look at your screen for a few... It will start running your suite soon.
Thanks
Upvotes: 0
Reputation: 19223
Since Google also leads here even when searching for a log4j2
solution, here is what you can use. Add the following piece of XML
code into your log4j2.xml
within the Loggers
tags:
<logger name="org.apache.http" level="WARN"/>
Working for org.apache.httpcomponents:httpclient:4.5.13
and org.apache.logging.log4j:log4-core:2.14.1
.
Upvotes: 0
Reputation: 709
For me, the below lines in the log4j.properties
file cleaned up all the mess that came from HttpClient logging... Hurray!!! :)
log4j.logger.org.apache.http.headers=ERROR
log4j.logger.org.apache.http.wire=ERROR
log4j.logger.org.apache.http.impl.conn.PoolingHttpClientConnectionManager=ERROR
log4j.logger.org.apache.http.impl.conn.DefaultManagedHttpClientConnection=ERROR
log4j.logger.org.apache.http.conn.ssl.SSLConnectionSocketFactory=ERROR
log4j.logger.org.springframework.web.client.RestTemplate=ERROR
log4j.logger.org.apache.http.client.protocol.RequestAddCookies=ERROR
log4j.logger.org.apache.http.client.protocol.RequestAuthCache=ERROR
log4j.logger.org.apache.http.impl.execchain.MainClientExec=ERROR
log4j.logger.org.apache.http.impl.conn.DefaultHttpClientConnectionOperator=ERROR
Upvotes: 3
Reputation: 67
This worked for me.
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "error");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "error");
System.setProperty("log4j.logger.org.apache.http", "error");
System.setProperty("log4j.logger.org.apache.http.wire", "error");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "error");
Upvotes: 1
Reputation: 41
it's work for me with add "logback.xml" in class root path and below setting.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<logger name="org.apache" level="WARN"/>
<logger name="httpclient" level="WARN"/>
</configuration>
Upvotes: 4
Reputation: 177
For me it was very simple solution :
I had to add log4j dependancies in my POM.xml and that resolved unnecessary loggings .
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.1</version>
</dependency>
Upvotes: 0
Reputation: 707
In my case I use xml configuration, and I append this to the configuration file
<logger name="org.apache.http">
<level value="warn"/>
</logger>
Upvotes: -1
Reputation: 3733
I was also having the same problem. The entire console was filled with [main] DEBUG org.apache.http.wire
while running the tests.
The solution which worked for me was creating a logback-test.xml src/test/resources/logback-test.xml as in https://github.com/bonigarcia/webdrivermanager-examples/blob/master/src/test/resources/logback-test.xml (ref - https://github.com/bonigarcia/webdrivermanager/issues/203)
To view my logging infos, I replaced logger name="io.github.bonigarcia" with my package name
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.mypackage" level="DEBUG" />
<logger name="org" level="INFO" />
<logger name="com" level="INFO" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Upvotes: 4
Reputation: 431
With:
One can add:
logger.httpclient.name=org.apache.http
logger.httpclient.level=info
With 'httpclient' in the above example being a logical name you choose.
(Tested on Java 11 OpenFX application.)
Upvotes: -1
Reputation: 3350
I tried all above solutions to no avail. The one soution that came the closest for me was the one suggesting creating a logback.xml. That worked, however nothing got logged. After playing around with the logback.xml, this is what I ended up with
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Now All levels below DEBUG gets logged correctly.
Upvotes: -1
Reputation: 391
I had this issue while using RestAssured with JUnit. For me this programmatic approach worked:
@BeforeClass
public static void setUpClass() {
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger("org.apache.http");
root.setLevel(ch.qos.logback.classic.Level.INFO);
//...
}
Upvotes: 19
Reputation: 17
Simply add these two dependencies in the pom file: I have tried and succeed after trying the discussion before.
<!--Using logback-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
Commons-Logging -> Logback and default Info while Debug will not be present; You can use:
private static Logger log = LoggerFactory.getLogger(HuaweiAPI.class);
to define the information you want to log:like Final Result like this. Only the information I want to log will be present.
Upvotes: -1
Reputation: 528
I experienced such problem after setting HttpComponentsClientHttpRequestFactory for my rest template.
Setting OkHttpClientHttpRequestFactory should solve problem with trash logging.
Upvotes: 0
Reputation: 1006
Add the below lines in the log4j property file and it will shut the http logs :- log4j.logger.org.apache.http=OFF
Upvotes: 2
Reputation: 1069
Update log4j.properties
to include:
log4j.logger.httpclient.wire.header=WARN
log4j.logger.httpclient.wire.content=WARN
Note that if Log4j library is not installed, HttpClient (and therefore JWebUnit) will use logback. In this situation, create or edit logback.xml
to include:
<configuration>
<logger name="org.apache" level="WARN" />
<logger name="httpclient" level="WARN" />
</configuration>
Setting the log level to WARN
with Log4j using the package name org.apache.commons.httpclient
in log4j.properties
will not work as expected:
log4j.logger.org.apache.commons.httpclient=WARN
This is because the source for HttpClient (v3.1) uses the following log names:
public static Wire HEADER_WIRE = new Wire(LogFactory.getLog("httpclient.wire.header"));
public static Wire CONTENT_WIRE = new Wire(LogFactory.getLog("httpclient.wire.content"));
Upvotes: 106
Reputation: 3856
For log4j, add the following to log4j.properties
(in the application's source
directory):
log4j.logger.org.apache=WARN
log4j.logger.httpclient=WARN
For logback, the following logback.xml
will kill the noise:
<configuration>
<logger name="org.apache" level="WARN" />
<logger name="httpclient" level="WARN" />
</configuration>
Upvotes: 24
Reputation: 9
The best solution I found was to use the maven enforcer plugin in order to prevent commons-logging from being used altogether. Then I added the slf4j dependency for logging instead. So add the following to your pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>[your version here]</version>
</dependency>
and also add the maven-enforcer plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>[your version here]</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<DependencyConvergence />
<bannedDependencies>
<excludes>
<exclude>commons-logging:commons-logging</exclude>
</excludes>
</bannedDependencies>
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
Upvotes: -1
Reputation: 1087
The following 2 lines solved my problem completely:
Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.ERROR);
Logger.getLogger("httpclient").setLevel(Level.ERROR);
Upvotes: 3
Reputation: 9674
This worked for my tests;
java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST);
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "ERROR");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "ERROR");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "ERROR");
Upvotes: 21