To set up Spring Boot to display request logs related to Connection refused errors, you can follow these steps:
pom.xml
file:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.2</version>
</dependency>
log4j2.xml
file in your classpath (such as src/main/resources
), with the following content:<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
</Root>
<Logger name="org.springframework.web.client.RestTemplate" level="DEBUG"/>
</Loggers>
</Configuration>
Note that this configuration sets the logging level of org.springframework.web.client.RestTemplate
to DEBUG
, so that it will log connection refused errors.
Restart your Spring Boot application.
When you make a request that results in a connection refused error, you should see a log entry like this:
2021-09-01 15:12:56 [http-nio-8080-exec-1] ERROR o.s.w.c.RestTemplate - I/O error on GET request for "http://localhost:8081/api/customers/1/orders": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
This log entry shows the time, thread, log level, logger name (org.springframework.web.client.RestTemplate
), and the error message (I/O error on GET request...
).
Asked: 2023-06-04 16:26:04 +0000
Seen: 13 times
Last updated: Jun 04 '23