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:
- Add the AppInsights subsystem to your app
- Configure AppInsights inside your app
- 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> <dependency> <groupId>com.microsoft.azure</groupId> <artifactId>applicationinsights-spring-boot-starter</artifactId> <version>1.1.2</version> </dependency> ...
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:
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
azure.application-insights.instrumentation-key=<YOUR APPINSIGHTS INSTRUMENTATION KEY>
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> <groupId>com.microsoft.azure</groupId> <artifactId>applicationinsights-logging-logback</artifactId> <version>2.3.1</version> </dependency> ...
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"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <logger name="com.example.xxx" level="DEBUG"/> <appender name="aiAppender" class="com.microsoft.applicationinsights.logback.ApplicationInsightsAppender"> </appender> <root level="info"> <appender-ref ref="aiAppender" /> </root> </configuration>
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.