Home > Mobile >  How to disable logging for Spring RestTemplate
How to disable logging for Spring RestTemplate

Time:02-02

I am trying to reduce the logging of RestTemplate.

HttpHeaders headers = new HttpHeaders();
            RestTemplate restTemplate = new RestTemplate();
            headers.setContentType(MediaType.TEXT_PLAIN);
            HttpEntity<String> entity = new HttpEntity<>(dataToBeSenttoVM, headers);

            log.info("something");

            ResponseEntity<String> response = restTemplate.exchange(url,HttpMethod.POST, entity, String.class);

When the statement is executed it prints a Response. I am doing a lot of API calls using this, and this is creating a lot of logging noise in my console.

Is there any way I can reduce or disable the logging for this? I only want to avoid logging in this part of the code not the whole application.

Using Log4j's LoggerManager with following:

logging.pattern.console=%d{yyyy-MM-dd} %d{HH:mm:ss.SSS} [%thread] %5p %-70.70(%logger:%L){70}  %m%n
logging.level.org.springframework.web.client.RestTemplate=ERROR
logging.level.org.springframework.web: DEBUG

CodePudding user response:

Increase the minimum logging level of RestTemplate in your application.properties file, e.g., to ERROR:

logging.level.org.springframework.web.client.RestTemplate=ERROR

Or, if you're using an application.yml file:

logging:
  level:
    org.springframework.web.client.RestTemplate: ERROR

CodePudding user response:

You can disable the logging for the RestTemplate by setting the log level to OFF.

To do this, add the following line of code to your application:

Logger logger = LoggerFactory.getLogger(RestTemplate.class);
logger.setLevel(Level.OFF);
  • Related