Implemented Azure Function App with Spring Cloud Functions. App Insights instrumentation key configured in App Settings "APPINSIGHTS_INSTRUMENTATIONKEY": . Im using lombok annotation @Slf4j for logging the messages, but i cant see them in App insights. App insights capturing only log which written using ExecutionContext log. Can any one give me A reference ho to log the messages in App Insights using slf4j.
-
Have you seen this - https://learn.microsoft.com/en-us/azure/developer/java/sdk/logging-overview – Oleg Zhurakousky May 28 '21 at 06:51
4 Answers
If you are using AppInsights 2.x, you can use applicationinsights-logging-logback
or applicationinsights-logging-log4j2
libraries to enable logging. Please read the documentation to learn more about enabling logs.
<!-- Logback -->
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-logging-logback</artifactId>
<version>[2.0,)</version>
</dependency>
</dependencies>
<!-- Log4J 2.0 -->
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-logging-log4j2</artifactId>
<version>[2.0,)</version>
</dependency>
</dependencies>
If you are using AppInsights 3.x, you can configure the agent which will automatically export logs to App Insights.
There's also a preview feature in Azure Functions to enable distributed tracing that can be enabled from the Azure portal.

- 2,914
- 3
- 21
- 35
Updated in 2022
Application Insights agent supports SLF4j logs (for log4j2, logback, and jul) out of the box. Quoting from https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-standalone-config#auto-collected-logging
Log4j, Logback, and java.util.logging are auto-instrumented, and logging performed via these logging frameworks is auto-collected. Logging is only captured if it first meets the level that is configured for the logging framework, and second, also meets the level that is configured for Application Insights.
It is now pretty straight-forward:
- Application Insights needs to be enabled for your azure function
- Add the following in azure function configurations. More details here:
ApplicationInsightsAgent_EXTENSION_VERSION=~3
- Just like any other project add the logback dependency
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
</dependency>
and a sample logback.xml
- Finally within your azure function, log using the SLF4j API:
LoggerFactory.getLogger(getClass()).info("====================SLF4j Logger=======================");
With the above done, you should be able to view the logs in App Insights
traces
| order by timestamp desc
| limit 3000
severity
column represents the log level. (3 = Error. 1 = Information)

- 31,116
- 15
- 98
- 163
Answering My own question. Tried a couple of above approaches, nothing worked. Finally passed the ExecutionContext from handler to remaining services through method call.

- 51
- 1
- 4
When using that logger, always pass the Throwable as part of the error so that app insights can collect it and show you the throwables metadata:
log.error(String.format("Message {}", message), e);
If you convert the e to a string, that wont happen. You'll just get a bare bones log message.

- 28,471
- 61
- 196
- 289