Overview

In a previous article, I showed how to implement Logging in a Java Spring Boot Application. In this video, I will show how to log to Azure Application Insights. Application Insights is a scalable Azure data store and service that is ideal for storing monitoring information of all kinds.

NOTE: The code for the previous article can be found here. If you did not work through the samples in that article, you can use this code as a starting point for this article.

Azure Application Insights

The first step is to create an Application Insights service.

This article walks you through the steps of creating an Application Insights service.

After Azure creates the service, navigate to its "Overview" blade, as shown in Fig. 1.

Application Insights Overview Blade
Fig. 1

On the "Overview" blade, copy the "Instrumentation Key" (Fig. 2). You will need this later.

Instrumentation Key
Fig. 2

Add Dependencies

To access Application Insights, you will need to register dependencies in the pom.xml file. Open this file and add the following inside the element.


    com.microsoft.azure
    applicationinsights-spring-boot-starter
    2.6.4


    com.microsoft.azure
    applicationinsights-logging-logback
    [2.0,)

Add App Insights Logging Configuration

Next, you need to add additional logging configuration in the logback-spring.xml file. Open this file and add the following inside the element:



The section above defines how to log to Application Insights. To tell your application to use this when logging, we must add an element in our logging section, as shown below:


    
    

When finished, the entire logback-spring.xml file should look like the following:



    
    
    
        
            
                %black(%d{ISO8601}) %highlight(%-5level) %cyan(%logger{36}) - %msg%n
            
        
    
    
        
        
    

Set App Insights Key in Application Properties

Finally, we need to provide the Instrumentation Key to our logging library. A simple way to do this is to add a file named "application.properties" anywhere in our application path (I like to save this in the "resources" folder).

Add the following line to this file:

azure.application-insights.instrumentation-key=aaaaaaaaaaaaaaaaaaaa

where aaaaaaaaaaaaaaaaaaaa is the Application Insights Key you copied above.

Testing It

We can run this code locally and call the greetPerson method with the URL: http://localhost:8080/greetings/greet/David

Logging to Application Insights is not instantaneous. App Insights logging scales very well because it is done asynchronously. But, if you wait a few minutes, you will be able to view the logged information in Application Insights.

A few minutes after running, we should see log statements in Application Insights. To see these log statements, navigate the

Note that, as described in the earlier article, we will only output log statements at or above the level set in logback-spring.xml.

Conclusion

In this article, I showed you how to log to Azure Application Insights in an Azure Spring Boot application. You can find the sample code here.