Ask Your Question
3

What is the process of setting up Spring Boot to display request logs related to Connection refused errors?

asked 2023-06-04 16:26:04 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-04 16:37:02 +0000

pufferfish gravatar image

To set up Spring Boot to display request logs related to Connection refused errors, you can follow these steps:

  1. Add the following dependencies to your 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>
  1. Create a 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.

  1. Restart your Spring Boot application.

  2. 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...).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-04 16:26:04 +0000

Seen: 13 times

Last updated: Jun 04 '23