Function Compute collects all output written to stdout or stderr into the Simple Log Service (SLS) Logstore you specified when creating the service. This topic explains how to print structured logs from a Java function, read the execution summary that Function Compute appends to every invocation, and view logs in the console.
Print logs
Two options are available for printing logs from a Java function:
| Method | When to use |
|---|---|
context.getLogger — provided by the fc-java-core library | Use this when you need to correlate logs with a specific invocation. Each log entry includes a timestamp, request ID, and log level, so you can filter the Logstore for all output from a single function call. |
| Third-party logging libraries (for example, logback) | Use these when your application already has an established logging setup. |
We recommend that you use the context.getLogger method. Logs printed by this method contain request IDs, which you can use to query logs and troubleshoot errors.Use context.getLogger
The following example writes an INFO-level log entry and returns a response:
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());
}
}Running this handler produces the following output in your Logstore:
message:2017-07-05T05:13:35.920Z a72df088-f738-cee3-e0fe-323ad891**** [INFO] hello worldEach log entry contains the time, request ID, and log level followed by your message.
Log levels
context.getLogger supports six log levels. Choose the level that matches the intent of the message:
| Log level | Method | Standard usage |
|---|---|---|
| TRACE | context.getLogger().trace | Fine-grained execution path details; most verbose |
| DEBUG | context.getLogger().debug | Detailed information for diagnosing system behavior |
| INFO | context.getLogger().info | Normal operation messages |
| WARN | context.getLogger().warn | Potential issues that may cause unexpected behavior if unaddressed |
| ERROR | context.getLogger().error | Failures that prevent the code from running as expected |
| FATAL | context.getLogger().fatal | Critical errors that stop the application; least verbose |
Understand the execution summary
For every invocation, Function Compute records FC Invoke Start and FC Invoke End marker lines and appends an execution summary to the log. The summary contains the following fields:
| Field | Description |
|---|---|
| Request ID | The request ID of the invocation. |
| Code Check Value | The verification code of the function code package used for this invocation. |
| Function Execution Time | The time the function handler actually took to run. |
| Function Billing Time | The amount of time you are charged for. |
| Function Memory | The memory allocated to the function. |
| Actual Used Memory | The maximum memory used during the invocation. |
View logs
After a function runs, view its logs on the Logs tab of the Function Details page in the Function Compute console. For step-by-step instructions, see View function invocation logs.