Help Center/ FunctionGraph/ Best Practices/ Functional Application Practices/ Using a Java Function and Log4j2 to Print Logs
Updated on 2025-07-10 GMT+08:00

Using a Java Function and Log4j2 to Print Logs

Introduction

FunctionGraph supports Log4j2 for Java functions. This section describes how to use functions and Log4j2 to print logs.

Step 1: Download a Package

In this example, Java is used to implement log printing. You can directly download the sample code Log_demo.jar without any modification.

The key sample code is as follows.

package org.example;

import com.huawei.services.runtime.Context;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.util.LoaderUtil;

import java.net.URISyntaxException;
import java.util.Objects;

@Slf4j
public class LogTest {

    public void init(Context context) {
        try {
            Configurator.reconfigure(Objects.requireNonNull(LoaderUtil.getThreadContextClassLoader().getResource("log4j2-custom.xml")).toURI());
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public void handler(String event, Context context) {
        log.debug("debug log");
        log.info("info log");
        log.warn("warn log");
        log.error("info log");
    }
}
The following code is added to the initializer:
Configurator.reconfigure(Objects.requireNonNull(LoaderUtil.getThreadContextClassLoader().getResource("log4j2-custom.xml")).toURI());

Step 2: Creating a Function

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane. In the upper right corner, click Create Function.
  2. Click Create from scratch and configure the function information.

    • Function Type: Select Event Function.
    • Region: Select a region based on site requirements.
    • Function Name: Enter a custom name.
    • Runtime: Select Java 8.

    Retain the default values for other parameters and click Create Function.

  3. Upload the function code.

    After the function is created, go to the function details page, click the Code tab, choose Upload > Local ZIP, and add the ZIP file downloaded in Step 1: Download a Package.

  4. Enable class isolation.

    After the code package is successfully deployed, choose Configuration > Advanced Settings, enable Class Isolation, and click Save as shown in Figure 1.
    Figure 1 Enabling class isolation

  5. Set the function handler.

    As shown in Figure 2, choose Configuration > Basic Settings, set Handler to org.example.LogTest.handler, and click Save.

    Figure 2 Setting the handler

  6. Set the function initializer.

    As shown in Figure 3, choose Configuration > Lifecycle, enable Initialization. On the displayed page, set Function Initializer to org.example.LogTest.init, and click Save.

    Figure 3 Setting the function initializer

Step 3: Testing the Function

  1. After all parameters are configured, as shown in Figure 4, click the Code tab and click Configure Test Event, select Blank Template, and click Create.

    Figure 4 Configuring a test event

  2. Select the created test event and click Test. The test result is displayed in Execution Result tab on the right.