Can't turn off HttpClient Wire debug log messages
So what did finally work for my project was creating a configuration for sl4j
/ logback -- I couldn't make it work for log4j etc.
So under main/java/resources/logback.xml
<xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss} %thread %-5level %logger{36} %msg%n</pattern>
</encoder>
</appender>
<logger name="org.apache" level="ERROR"/>
<logger name="httpclient" level="ERROR"/>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
in maven I simply included dependency for slf4j
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
and its all working now.
Disable request logging via HttpClient for single url
Your logging filter should be depend on how we make use of HttpClient
. For example, using like this
using var weatherHttpClient = new HttpClient()
// make using here
AFAIK, this case would be impossible to separate logging from HttpClient
.
If we using HttpClient
via DI, we could make a filter like this.
// Register with DI. I'm just a fan of named HttpClient, use your register as wished
services.AddHttpClient(nameof(WeatherService), cfg =>
{
cfg.Timeout = TimeSpan.FromSeconds(10);
cfg.BaseAddress = new Uri("https://api.openweathermap.org");
});
// Using WeatherService class:
private readonly HttpClient _httpClient;
public WeatherService(IHttpClientFactory httpClientFactory)
{
_httpClient = httpClientFactory.CreateClient(nameof(WeatherService));
}
// We can create a logging Filter on Program.cs like
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.AddFilter((_, category, _) => !category.StartsWith($"System.Net.Http.HttpClient.{nameof(WeatherService)}"));
})
Register as services.AddHttpClient<WeatherService>
would result the same category name to filter.
But I still feel it some way cumbersome... of not using Serilog
, could you share the reason why say no to that ?
Disable logging in org.apache.httpcomponents
You can try 2 things. Either put this into your application.yml to only log warnings and errors for the mentioned packages
logging:
level:
org.apache.http: WARN
org.springframework.web: WARN
or define your own resources/logback.xml
like with the loggers for the packages set:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="CONSOLE_LOG_PATTERN" value="[%date{YYYY-MM-dd HH:mm:ss.SSS}] %-5level [%.15thread] %logger{1} %msg%n"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<logger name="org.apache.http" level="WARN" />
<logger name="org.springframework.web" level="WARN" />
<root level="${GLOBAL_LOGGING_LEVEL:-INFO}">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
How to disable log4j logging in Http Client 4.1 to log to FileAppender
What worked finally,
Logger.getLogger("org.apache.http").setLevel(org.apache.log4j.Level.OFF);
I was not using the right key.
Related Topics
Why Can Array Constants Only Be Used in Initializers
In Java, How to Call a Base Class's Method from the Overriding Method in a Derived Class
How to Log Spring 5 Webclient Call
Why Is Each Public Class in a Separate File
How to Get a Index Value from Foreach Loop in Jstl
Differences Between Just in Time Compilation and on Stack Replacement
Get All Variable Names in a Class
Calendar.Month Gives Wrong Output
Multiple Inheritance for an Anonymous Class
Jaxb Creating Context and Marshallers Cost
The Easiest Way to Transform Collection to Array
How to Rotate Jpeg Images Based on the Orientation Metadata
How Java Do the String Concatenation Using "+"
Oracle Jdbc Intermittent Connection Issue
Modifying Existing File Content in Java