第一种:在restTemplate配置代码中加入
Logger logger = (Logger) LoggerFactory.getLogger("ROOT");
logger.setLevel(Level.INFO);
public class restTemplateConfig {
public static RestTemplate template(){
Logger logger = (Logger) LoggerFactory.getLogger("ROOT");
logger.setLevel(Level.INFO);
RestTemplate restTemplate = new RestTemplate();
return restTemplate;
}
}
第二种:去除springboot中对应的引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
第三种:主动日志处理
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
public class RestTemplateExample {
private static final Logger logger = LoggerFactory.getLogger(RestTemplateExample.class);
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
String url = "http://example.com";
try {
String response = restTemplate.getForObject(url, String.class);
logger.info("Response received: {}", response);
} catch (Exception e) {
logger.error("Error occurred: ", e);
}
}
}