Quantcast
Channel: All – akquinet – Blog
Viewing all articles
Browse latest Browse all 133

Logging with Azure Application Insights – Part 1: Spring-Boot

$
0
0

When developing and running apps inside Microsoft Azure you have to deal with the topics like monitoring and logging. Azure provides a central solution for that question which is Application Insights. AppInsights (for short) is the central hub to get metrics and log data from our applications and let you access these data within the azure portal in an easy and convenient way. While the metric aspect is well documented, how to connect your favorite application logger to AppInsights it is not.

In this blog post we will show you how to enhance your typical Spring-Boot application to have all the logging data send to Azure AppInsights automatically. In a followup post we will show the same for a typical nodejs based application.

To enhance Spring-Boot applications to have all logging data automatically pushed to AppInsights the following steps have to be done:

  1. Add the AppInsights subsystem to your app
  2. Configure AppInsights inside your app
  3. Add and configure the AppInsights log-appender to your logging configuration

To add and activate AppInsights you have only had to add the corresponding AppInsights starter module to your dependencies in your pom.xml:

...
<dependencies&gt;
        <dependency&gt;
            <groupId&gt;com.microsoft.azure</groupId&gt;
            <artifactId&gt;applicationinsights-spring-boot-starter</artifactId&gt;
            <version&gt;1.1.2</version&gt;
        </dependency&gt;
...

The next step is to configure the Azure AppInsights instrumentation key. This key you can find in your azure portal on the overview page of your application insights resource:

AppInsights Instrumentation Key

By default the AppInsights module in Spring-Boot will look for an environment variable, which can be set with normal Spring-Boot property mechanisms. One approach is to have that key configured in your standard application.properties file:

azure.application-insights.instrumentation-key=<YOUR APPINSIGHTS INSTRUMENTATION KEY&gt;

Having this done, AppInsights gets now several metric data right away from our application. To have our logger output send as well we have to configure the Spring-Boot logging system.

Spring-Boot is using logback as the default logger. Azure does provide its own log-appender which has to be configured in our Spring-Boot application. First you have to add the AppInsights log-appender as a new dependency:

...
        <dependency&gt;
            <groupId&gt;com.microsoft.azure</groupId&gt;
            <artifactId&gt;applicationinsights-logging-logback</artifactId&gt;
            <version&gt;2.3.1</version&gt;
        </dependency&gt;
...

To configure the newly added log-appender you have to add a new configuration File /src/main/resources/logback-spring.xml to your app

<?xml version="1.0" encoding="UTF-8"?&gt;
<configuration&gt;
    <include resource="org/springframework/boot/logging/logback/base.xml"/&gt;
    <logger name="com.example.xxx" level="DEBUG"/&gt;
    <appender name="aiAppender"
              class="com.microsoft.applicationinsights.logback.ApplicationInsightsAppender"&gt;
    </appender&gt;
    <root level="info"&gt;
        <appender-ref ref="aiAppender" /&gt;
    </root&gt;
</configuration&gt;

Finally all your logging output is now published automatically to your AppInsights instance.

While doing the Spring-Boot integration is quite hassle free, having a nodejs based application doing the same is quite more challenging and not well documented. So stay tuned for the next blog post about that topic.


Viewing all articles
Browse latest Browse all 133

Trending Articles