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

Print logs

The content printed by functions to standard output (stdout) or standard error (stderr) is collected to the Logstore that you specify when you create a service. You can use the context.getLogger method provided by the fc-java-core library to print logs, or use common logging libraries such as logback to print logs.

Note We recommend that you use the context.getLogger method to print logs. The logs printed by this method contain the request IDs, which help you troubleshoot errors.

Use context.getLogger 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 sample 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 log 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 at the TRACE level.
  • context.getLogger().debug: prints logs at the DEBUG level.
  • context.getLogger().info: prints logs at 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 at 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 for the execution summary.

Parameter Description
Request ID The request ID of the invocation.
Code Check Value The verification code of the code package used for invoking 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 allocated for the function.
Actual Used Memory The maximum amount of memory that is actually used by the function.

View logs

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