Sending Logs from Micronaut Native Image Applications to Azure Monitor
Logico_jp details how to configure a Micronaut native image Java application to send logs and traces to Azure Monitor Application Insights, including setup steps, required libraries, and code examples.
Sending Logs from Micronaut Native Image Applications to Azure Monitor
This guide, authored by Logico_jp, walks through the process for sending logs generated by Java-based Micronaut native image applications to Azure Monitor using Application Insights. The article emphasizes cloud-native monitoring on Azure and provides actionable steps, CLI commands, and code to enable robust log management.
Log Destination Overview
Azure-managed hosting options (like App Service and Container Apps) support multiple log destinations:
- Default console logs: Captured automatically and redirected to
ContainerAppConsoleLogs_CL
or log analytics tables based on diagnostic settings. - Custom destination via DCE: Use Data Collection Endpoint to write logs to custom tables in Log Analytics Workspace.
- Application Insights (traces table): Recommended for structured tracing; requires Log Appender configuration.
Refer to Log storage and monitoring options in Azure Container Apps for more details.
Prerequisites
- Maven: 3.9.10
- Java: JDK 21
- Micronaut: 4.9.0 or later
- Supported Log Libraries: Log4j2, Logback (used in this guide), JBoss Logging, java.util.logging
Step 1: Azure Resource Setup
- Create a Resource Group
- Create and configure an Application Insights resource (detailed steps in Microsoft docs)
Step 2: Micronaut Project Archetype
You can scaffold a Micronaut app using the CLI or Micronaut Launch:
mn create-app \
--build=maven \
--jdk=21 \
--lang=java \
--test=junit \
--features=graalvm,azure-tracing,yaml \
dev.logicojp.micronaut.azuremonitor-log
Key features: graalvm, azure-tracing, yaml (for configuration)
Step 3: Add Dependencies and Plugins
Ensure the following dependencies and plugins are present in your pom.xml
:
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-logback-appender-1.0</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-logging-logback</artifactId>
</dependency>
<dependency>
<groupId>io.micronaut.tracing</groupId>
<artifactId>micronaut-tracing-opentelemetry-http</artifactId>
</dependency>
<!-- For GraalVM compatibility -->
<dependency>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>graalvm-reachability-metadata</artifactId>
<version>0.11.0</version>
<classifier>repository</classifier>
<type>zip</type>
</dependency>
GraalVM Maven plugin configuration should enable reachability metadata and quick build:
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
<metadataRepository>
<enabled>true</enabled>
</metadataRepository>
<buildArgs combine.children="append">
<buildArg>-Ob</buildArg>
</buildArgs>
<quickBuild>true</quickBuild>
</configuration>
</plugin>
Step 4: Configure Application
- Use
application.yml
for configuration - Configure both Application Insights and Azure tracing settings
- Example snippet:
applicationinsights:
connection:
string: ${AZURE_MONITOR_CONNECTION_STRING}
sampling:
percentage: 100
instrumentation:
logging:
level: "INFO"
preview:
captureLogbackMarker: true
captureControllerSpans: true
azure:
tracing:
connection-string: ${AZURE_MONITOR_CONNECTION_STRING}
You can set the connection string as an environment variable (APPLICATIONINSIGHTS_CONNECTION_STRING
) or within your configuration file.
Step 5: Logging and OpenTelemetry Setup
Application Insights Enablement
Explicitly create and configure an OpenTelemetry
object in your application:
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
AzureMonitorAutoConfigure.customize(sdkBuilder, "connectionString");
Log Appender Configuration (logback.xml)
Add an Appender for OpenTelemetry in logback.xml
:
<appender name="OpenTelemetry" class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender">
<captureExperimentalAttributes>true</captureExperimentalAttributes>
<captureCodeAttributes>true</captureCodeAttributes>
<captureMarkerAttribute>true</captureMarkerAttribute>
<captureKeyValuePairAttributes>true</captureKeyValuePairAttributes>
<captureMdcAttributes>*</captureMdcAttributes>
</appender>
Then, in your application code, associate the OpenTelemetry object with the appender:
OpenTelemetryAppender.install(openTelemetry);
Example Controller
Minimal controller using logging:
@Controller("/api/hello")
@ExecuteOn(TaskExecutors.IO)
public class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
public HelloController(OpenTelemetry openTelemetry){
OpenTelemetryAppender.install(openTelemetry);
logger.info("OpenTelemetry is configured and ready to use.");
}
@Get
@Produces(MediaType.APPLICATION_JSON)
public GreetingResponse hello(@QueryValue(value = "name", defaultValue = "World") String name) {
logger.info("Hello endpoint was called with query parameter: {}", name);
// ... business logic ...
logger.info("Processing complete, returning response");
return new GreetingResponse(/* ... */);
}
}
Step 6: Building and Testing
- Run as a Java app:
mvn clean package
- Confirm logs are visible in Application Insights under the
traces
table - Build native image:
mvn package -Dpackaging=native-image
- Collect GraalVM configuration and reachability metadata as described
- Place generated config files in
src/main/resources/META-INF/native-image/{groupId}/{artifactId}
Summary and Recommendations
- Sending logs from Micronaut native image apps to Azure Monitor is feasible and not overly complex, but does require explicit appender and dependency setup.
- Ensure proper configuration of Application Insights and GraalVM native image parameters for best results.
For further details, see the related Microsoft Community Hub post and the linked documentation.
This post appeared first on “Microsoft Tech Community”. Read the entire article here