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?
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: