All Products
Search
Document Center

Function Compute:Log management

Last Updated:Feb 02, 2024

This topic describes how to print and view logs in the Java runtime environment.

Print logs

Logs that are printed to standard output (stdout) or standard error (stderr) by a function are collected to the Simple Log Service Logstore that you specified when you created the service. You can use the context.getLogger method provided by the fc-java-core library to print logs. You can also use common logging libraries such as logback to print logs.

Note

We recommend that you use the context.getLogger method to print logs. Logs that are printed by using this method contain request IDs, and you can use the request IDs to query logs and troubleshoot errors.

Use the context.getLogger method to print logs

If you use this method to print logs, each log contains information such as the time, request ID, and log level. The following code provides an example:

package example;

import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.StreamRequestHandler;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class HelloFC implements StreamRequestHandler {

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        context.getLogger().info("hello world");
        outputStream.write(new String("hello world").getBytes());
    }
}

After you run the preceding sample code, the following content is printed:

message:2017-07-05T05:13:35.920Z a72df088-f738-cee3-e0fe-323ad891**** [INFO] hello world        

You can use the following code to print logs of different levels:

  • context.getLogger().trace: prints logs of the TRACE level.

  • context.getLogger().debug: prints logs of the DEBUG level.

  • context.getLogger().info: prints logs of the INFO level.

  • context.getLogger().warn: prints logs of the WARN level.

  • context.getLogger().error: prints logs of the ERROR level.

  • context.getLogger().fatal: prints logs of the FATAL level.

Function Compute records the FC Invoke Start and FC Invoke End lines for each invocation and the execution summary. The following table describes the parameters in the execution summary.

Parameter

Description

Request ID

The request ID of the invocation.

Code Check Value

The verification code of the code package that is used to invoke the function.

Function Execution Time

The time that the function handler actually takes to run.

Function Billing Time

The amount of time in which you are charged for the function invocation.

Function Memory

The memory that is allocated to the function.

Actual Used Memory

The maximum size of memory that is used by the function.

View logs

After a function is executed, you can view the logs of the function on the Logs tab of the Function Details page. For more information, see View function invocation logs.