Logging using RunTimeLogger

Creating a function that writes logs

To output logs from your function code, you can use methods on java.lang.System, or any logging module that writes to stdout or stderr.

Note

As all FunctionGraph instances write to the same log, it can be difficult to distinguish logs of different requests. Writing to stdout or stderr will need to log the requestId of the request obtained from the Context. The provided RunTimeLogger will add the requestId to the log output.

For logging using Log4J framework and requestId see: Logging using Log4j.

The opentelekomcloud-functiongraph-java library provides a logger class named RuntimeLogger that can be accessed from the context object.

This class provides following methods:

RuntimeLogger methods

Method name

Description

void log(String message);

Log message independent of Log Level

void debug(String message);

Log message as debug message

void info(String message);

Log message as info message

void warn(String message);

Log message as warn message

void error(String message);

Log message as error message

void setLevel(String level);

set Log Level, valid values are:
  • DEBUG

  • INFO

  • WARN

  • ERROR

The following example uses the RuntimeLogger logger provided by the context object.

Example: FGLogSample1.java
/*
 * Copyright (c) 2025 T-Systems International GmbH.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.opentelekomcloud.samples;

import com.google.gson.JsonObject;
import com.google.gson.GsonBuilder;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.opentelekomcloud.services.runtime.Context;
import com.opentelekomcloud.services.runtime.RuntimeLogger;

/**
 * FGLogSample1 is a sample function that logs the event data received.
 * It uses Gson to pretty print the JSON event data.
 */
public class FGLogSample1 {
  final Gson gsonPrettyPrint = new GsonBuilder().setPrettyPrinting().create();

  public String handleRequest(final JsonObject event, final Context context) {
    // get RuntimeLogger from context
    RuntimeLogger log = context.getLogger();

    JsonElement jsonElement = JsonParser.parseString(event.toString());

    // log event
    log.log("Event:" + gsonPrettyPrint.toJson(jsonElement));

    return "Success";
  }
}
Log Output
 2025-02-18T11:16:19Z Start invoke request 'a8f4c82d-a506-4ba3-9fc6-fb60471683d5', version: latest
 2025-02-18T11:16:19Z a8f4c82d-a506-4ba3-9fc6-fb60471683d5 INFO Event: {
 "key": "value"
 }
 2025-02-18T11:16:19Z Finish invoke request 'a8f4c82d-a506-4ba3-9fc6-fb60471683d5', duration: 1.634ms, billing duration: 2ms, memory used: 106.969MB, billing memory: 512MB

The Java runtime logs the Start invoke request and Finish invoke request lines for each invocation.

The Start invoke request line provides the following details:

  • RequestId - The unique request Id for the invocation

  • version - Version of the FunctionGraph

The Finish invoke request line provides the following details:

  • RequestId - The unique request Id for the invocation

  • duration - The amount of time that your function’s handler method spent processing the event.

  • billing duration - The amount of time billed for the invocation.

  • memory used - The amount of memory used for the invocation.

  • billing memory - The amount of memory billed for the invocation.

Getting current LogLevel

The Api implementation of Context, currently does not provide a method to get the current log level the RuntimeLogger is using.

Following sample can be used as a workaround:

Example: FGLogSample2.java
/*
 * Copyright (c) 2025 T-Systems International GmbH.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.opentelekomcloud.samples;

import com.google.gson.JsonObject;
import com.opentelekomcloud.services.runtime.Context;
import com.opentelekomcloud.services.runtime.RuntimeLogger;
import com.opentelekomcloud.services.functiongraph.runtime.core.ContextHelper;

/**
 * FGLogSample2 is a sample function that demonstrates how to log messages
 * at different log levels using RuntimeLogger.
 * It retrieves the current log level and sets it to "INFO".
 */
public class FGLogSample2 {

  public String handleRequest(final JsonObject event, final Context context) {
    // get RuntimeLogger from context
    RuntimeLogger log = context.getLogger();

    // get current LogLevel from context
    ContextHelper ctxHelper = new ContextHelper(context);
    
    String logLevel = ctxHelper.getLogLevel();

    log.log("Current LogLevel:" + logLevel);

    // set Log Level to "INFO"
    log.setLevel("INFO");

    log.debug("Debug"); // will not be logged
    log.info("Info"); // will be logged
    log.warn("Warning"); // will be logged
    log.error("Error"); // will be logged

    return "Success";
  }
}