3. Configure Appender and Application Configuration
3.1. Logback Appender
The GDK Launcher generated a file named azure/src/main/resources/logback.xml containing the configuration for an appender that publishes log events to Azure Monitor Logs:
<configuration debug='false'>
<!--
You can un-comment the STDOUT appender and <appender-ref ref='STDOUT'/> in
the cloud appender to log to STDOUT as the 'emergency' appender.
-->
<!--
<appender name='STDOUT' class='ch.qos.logback.core.ConsoleAppender'>
<encoder>
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern>
</encoder>
</appender>
-->
<appender name='AZURE' class='io.micronaut.azure.logging.AzureAppender'>
<!-- <appender-ref ref='STDOUT'/> -->
<encoder class='ch.qos.logback.core.encoder.LayoutWrappingEncoder'>
<layout class='ch.qos.logback.contrib.json.classic.JsonLayout'>
<jsonFormatter class='io.micronaut.azure.logging.AzureJsonFormatter'/>
</layout>
</encoder>
</appender>
<root level='INFO'>
<appender-ref ref='AZURE'/>
</root>
</configuration>
Note: You can un-comment the STDOUT appender as the 'emergency' appender. See the file for details.
3.2. Set Application Configuration Properties
Update the Azure application.properties as follows:
(1)
azure.logging.data-collection-endpoint=<endpoint>
(2)
azure.logging.rule-id=<ruleid>
(3)
azure.logging.stream-name=Custom-GDKTable
1 Replace <endpoint> with the logs ingestion endpoint URL that you saved earlier
2 Replace <ruleid> with the rule ID that you saved earlier
3 Set the value of the azure.logging.stream-name property with the name of the workspace table you created, "Custom-GDKTable"
Having configured the appender and the application configuration, you can proceed to run the application, publishing the logs.
4. Run the Application, Publish and View Logs
To run the application, use the following command, which starts the application on port 8080.
Send some curl
requests to test logging:
MESSAGE="Hello World"
POST_RES=$(curl -s -id '{"message":"'"$MESSAGE"'"}' \
-H "Content-Type: application/json" \
-X POST http://localhost:8080/log)
echo $POST_RES
Run the az monitor log-analytics query command to retrieve log events that were pushed while running your application:
LOG_RES=$(az monitor log-analytics query \
--workspace $GDK_WORK_SPACE_ID \
--analytics-query "search in ($TABLE_NAME) \"$MESSAGE\" | where TimeGenerated > ago(1h) | take 1" )
echo $LOG_RES
Note that it can take a few minutes for log entries be available.
5. Clean up Cloud Resources
Once you are done with this guide, you can delete the Azure resources created to avoid incurring unnecessary charges.
Delete the resource group and all of its resources with:
Alternatively, run these commands to delete resources individually:
Summary
This guide demonstrated how to use the GDK to create an application that publishes logs to Azure Monitor Logs. Then, using Azure Monitor Logs, you viewed the logs produced by the application. Finally, you saw how to build a native executable for this application with GraalVM Native Image, and ran it to test publishing logs to Azure Monitor Logs.