How to know spring boot application run stopped due to hard error or it just completed normally

180 Views Asked by At

I have a requirement like if Spring Boot application suddenly stopped due to error I need to send an alert in splunk. If it stopped normally, I have a logs in application so based on logs I'm configuring alert messages in splunk.

Example:

@SpringBootApplication  
public class SpringBootHelloWorldExampleApplication {  
    public static void main(String[] args) {  
        SpringApplication.run(SpringBootHelloWorldExampleApplication.class, args);  
        log.debug("successfully completed");
    }  
} 

I only get this above log when the application stopped normally. I mean successfully completing the application. But if I get any error in application, I won't get that log. If only I get this log then I'm sending splunk alert.

As I won't get log if any error occurs, then how I know if the application stopped due to hard error? Or how can I log something for hard errors?

3

There are 3 best solutions below

1
On

Configure Logging: Spring Boot uses the Spring Framework's built-in logging support. You need to configure your logging framework to log errors to a file or a specific log appender. The most commonly used logging frameworks are Logback or Log4j. Here, we'll use Logback for the example.

In your Spring Boot project, you can add a logback-spring.xml or logback.xml configuration file to control your log output. For example:

<configuration>
    <appender name="splunk" class="ch.qos.logback.classic.net.SyslogAppender">
        <syslogHost>splunk-server-ip</syslogHost>
        <port>514</port>
        <facility>LOCAL0</facility>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%date %level [%thread] </pattern>
        </layout>
    </appender>

    <logger name="org.springframework" level="ERROR"/>
    <root level="INFO">
        <appender-ref ref="splunk"/>
    </root>
</configuration>

This configuration sends logs with a level of ERROR from the Spring Framework to a Syslog Appender, which can be configured to send logs to Splunk.

Configure Splunk: Configure your Splunk instance to receive logs from the source you specified in your Logback configuration. You'll need to set up a data input in Splunk to receive the syslog messages.

Start Spring Boot Application: Start your Spring Boot application. Any errors logged at the ERROR level in your application will be forwarded to Splunk according to your Logback configuration.

Make sure to replace splunk-server-ip with the actual IP or hostname of your Splunk server.

Springboot code :

@SpringBootApplication
    public class SpringBootTestApplication {
        public static void main(String[] args) {
            try {
                SpringApplication.run(SpringBootTestApplication.class, args);
                log.debug("Successfully completed");
            } catch (Exception e) {
                **log.error("Error thrown: " + e.getMessage());**
            } 
        }
     }
3
On

One straightforward approach is to use a try { } catch { } block to make an attempt to run the Spring Boot application. If the operation is successful, it will proceed to log "successfully completed". Otherwise, if an exception is thrown during the execution, it will be caught by the catch block, and then it will proceed to log a suitable message of your preference, or you can combine it with the message of the exception. Something like that:

    @SpringBootApplication
    public class SpringBootHelloWorldExampleApplication {
        public static void main(String[] args) {
            try {
                SpringApplication.run(SpringBootHelloWorldExampleApplication.class, args);
                log.debug("Successfully completed");
            } catch (Exception e) {
                log.debug("Error thrown: " + e.getMessage());
            } //e.getMessage(): print the message of the Exception thrown 
        }
     }
2
On

It can be implemented on Java level, using something like Runtime#addShutdownHook(). The Runtime will spawn a new thread on the VM termination to do some cleaning. In this Thread you can do whatever you need.

However, I would encourage you to investigate the non-Java approach here. Oftentimes, in large systems, such an observability is achieved with external systems. It highly depends on where your Java app is deployed, how it is launched e.t.c.

For instance, if you just launch it via a script you can check an exit code of the Java process. If you are deployed in the K8S cluster somewhere either on-premise or in the cloud, then it is a bit harder, although K8S is capable of providing the status code of the failed process to API clients.