Publish Application Logs to the Oracle Cloud Infrastructure Logging Service

This guide describes how to use the Graal Development Kit for Micronaut (GDK) to create an application that publishes logs to the Oracle Cloud Infrastructure Logging service. Typically, application logs are written to stdout and to files, but the Micronaut® Logging module supports multiple logging frameworks such as Logback, and logging to various other appenders, to email, a database, or other destinations.

The Oracle Cloud Infrastructure Logging service is a highly scalable and fully managed single pane of glass for all the logs in your tenancy. Logging provides access to logs from Oracle Cloud Infrastructure resources. These logs include critical diagnostic information that describes how resources are performing and being accessed.

Prerequisites #

Follow the steps below to create the application from scratch. However, you can also download the completed example:

A note regarding your development environment

Consider using Visual Studio Code, which provides native support for developing applications with the Graal Development Kit extension.

If you use IntelliJ IDEA, enable annotation processing.

Windows platform: The GDK guides are compatible with Gradle only. Maven support is coming soon.

1. Create the Application #

Create an application using the GDK Launcher.

  1. Open the GDK Launcher in advanced mode.

  2. Create a new project using the following selections.
    • Project Type: Application (Default)
    • Project Name: oci-logging-demo
    • Base Package: com.example (Default)
    • Clouds: OCI
    • Language: Java (Default)
    • Build Tool: Gradle (Groovy) or Maven
    • Test Framework: JUnit (Default)
    • Java Version: 17 (Default)
    • Micronaut Version: (Default)
    • Cloud Services: Logging
    • Features: GraalVM Native Image (Default)
    • Sample Code: Yes (Default)
  3. Click Generate Project, then click Download Zip. The GDK Launcher creates an application with the default package com.example in a directory named oci-logging-demo. The application ZIP file will be downloaded to your default downloads directory. Unzip it, open it in your code editor, and proceed to the next steps.

Alternatively, use the GDK CLI as follows:

gcn create-app com.example.oci-logging-demo \
    --clouds=oci \
    --services=logging \
    --features=graalvm \
    --build=gradle \
    --jdk=17 \
    --lang=java
gcn create-app com.example.oci-logging-demo \
    --clouds=oci \
    --services=logging \
    --features=graalvm \
    --build=maven \
    --jdk=17 \
    --lang=java

The GDK Launcher creates a multimodule project with two subprojects: oci for Oracle Cloud Infrastructure, and lib. You develop the application logic in the oci subproject. If your application is to be deployed to multiple cloud providers, use the lib subproject to create classes that can be shared between the providers. This enables you to separate the code that is different between cloud providers, while keeping most of the implementation in the common lib subproject.

The Micronaut Logging service that you selected at the project generation step bundles Logback, Jackson Databind, Oracle Cloud Infrastructure Logging, and other necessary dependencies. The Logback appender publishes logs to Oracle Cloud Infrastructure.

1.1. Controller Class #

The example code includes a controller in a file named oci/src/main/java/com/example/LogController.java, which enables you to send POST requests to publish a message to a log:

package com.example;

import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
class LogController {

    private static final Logger LOG = LoggerFactory.getLogger(LogController.class);

    @Post("/log")
    void log(@Body String message) {
        LOG.info(message);
    }
}

2. Get Started with the Oracle Cloud Infrastructure Logging Service #

To use the Oracle Cloud Infrastructure Logging service, first create a Log Group and then a Custom Log.

  1. Follow the steps described in Creating a Log Group to create a Log Group. Use “GDK_Log_Group” as its name and description.

  2. Follow the steps described in Creating a Log to create a Custom Log in the Log group you created in step 1.. Use “GDK_Custom_Log” as the name of the Log. When prompted to create agent configuration, select Add configuration later then click Create agent config.

  3. Click the name of the new custom log to view its details, and from the Log Information tab, copy its OCID (click Copy).

3. Configure Appender #

The GDK Launcher provided a file named oci/src/main/resources/logback.xml containing the configuration for an appender that publishes log statements to the Oracle Cloud Logging service. Update the file to include the OCI of the custom Log you copied earlier. Replace this line:

<logId><!-- TODO set the value of the Oracle Cloud Infrastructure log OCID here --></logId>

with this (the OCID of the Log):

<logId>ocid1.log.oc1.iad.ama...</logId>

Note: You can un-comment the STDOUT appender as the ‘emergency’ appender. See the file for details.

Having configured the appender, you can proceed to run the application, publishing the logs.

4. Run the Application, Publish and View Logs #

  1. Run the application using the following command (it will start the application on port 8080):

    ./gradlew :oci:run
    ./mvnw install -pl lib -am
    ./mvnw mn:run -pl oci
  2. Send some curl requests to test logging:
    curl -id '{"message":"your message here"}' \
       -H "Content-Type: application/json" \
       -X POST http://localhost:8080/log
    
  3. In the Oracle Cloud console, open the navigation menu, go to Observability & Management. Under Logging, click Logs, then click the name of the new custom log to view the logs published by your application.

5. Generate a Native Executable Using GraalVM #

The GDK supports compiling a Java application ahead-of-time into a native executable using GraalVM Native Image. You can use the Gradle plugin for GraalVM Native Image building/Maven plugin for GraalVM Native Image building. Packaged as a native executable, it significantly reduces application startup time and memory footprint.

To generate a native executable, run the following command:

./gradlew :oci:nativeCompile

The native executable is created in the oci/build/native/nativeCompile/ directory and can be run with the following command:

oci/build/native/nativeCompile/oci
./mvnw install -pl lib -am
./mvnw package -pl oci -Dpackaging=native-image

The native executable is created in the oci/target/ directory and can be run with the following command:

oci/target/oci

6. Run and Test the Native Executable #

Run the native executable, and then perform the same request as in step 4.

Summary #

This guide demonstrated how to use the GDK to create an application that publishes logs to the Oracle Cloud Infrastructure Logging service. Then, using the Oracle Cloud Infrastructure Logging service, 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 Oracle Cloud Infrastructure Logging.