News and Events

News and Events

Mule 4 Logging to Azure Insights

Integrating Mule 4 logging with Microsoft Azure Insights is a simple task when you make use of custom Log4j2 ApplicationInsightsAppender appender. The only minor issue is Munit testing that seems to be using a different classloader that fails to find custom appender libraries and throws errors. No such classloader issues when running the application.

src/main/resources/log4j2.xml

<?xml version="1.0" encoding="utf-8"?>
<Configuration packages="com.microsoft.applicationinsights.log4j.v2">
    <Appenders>
        <RollingFile name="file" fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}my-sapi.log"
            filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}my-sapi-%i.log">
            <PatternLayout pattern="%-5p %d [%t] %X{correlationId}%c: %m%n" />
            <SizeBasedTriggeringPolicy size="10 MB" />
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        <ApplicationInsightsAppender name="aiAppender" instrumentationKey="${sys:instrumentationKey}" />
    </Appenders>
    <Loggers>
        <!-- Http Logger shows wire traffic on DEBUG. -->
        <AsyncLogger name="org.mule.service.http.impl.service.HttpMessageLogger" level="INFO" />
        <AsyncLogger name="org.mule.service.http" level="WARN"/>
        <AsyncLogger name="org.mule.extension.http" level="WARN"/>
        <!-- Mule classes -->
        <AsyncLogger name="org.mule" level="INFO"/>
        <AsyncLogger name="com.mulesoft" level="INFO"/>
        <AsyncLogger name="aiAppender" level="INFO"/>
        <AsyncRoot level="INFO">
            <AppenderRef ref="file" />
            <AppenderRef ref="aiAppender" />
        </AsyncRoot>
    </Loggers>
</Configuration>

pom.xml

<applicationinsights.version>2.6.4</applicationinsights.version>
<dependencies>
   <!-- Application Insights dependencies. -->
   <dependency>
     <groupId>com.microsoft.azure</groupId>
     <artifactId>applicationinsights-logging-log4j2</artifactId>
     <version>${applicationinsights.version}</version>
   </dependency>
   <dependency>
     <groupId>com.microsoft.azure</groupId>
     <artifactId>applicationinsights-web</artifactId>
     <version>${applicationinsights.version}</version>
   </dependency>
   <dependency>
   <groupId>com.microsoft.azure</groupId>
     <artifactId>applicationinsights-core</artifactId>
     <version>${applicationinsights.version}</version>
   </dependency>
</dependencies>