All Products
Search
Document Center

Function Compute (2.0):Logging

Last Updated:Mar 20, 2024

You can print and view logs in Node.js runtimes. The logs can be used to quickly locate issues or analyzed to obtain function execution information, such as the function execution process and duration. This improves system reliability and stability.

Print logs

The log content that is printed by a function to the standard output (stdout) is stored in the specified Logstore in Simple Log Service when you create a service. You can use one of the following methods to print logs to stdout. The following sample code shows how to print logs:

ECMAScript modules

Note

This example supports only Node.js 18 or later.

export const handler = async (event, context) => {
  process.stdout.write('hi,fc\n');
  console.log('hello,world');
  context.logger.info('hello,fc');
  return "Hello World!";
};

CommonJS modules

'usestrict';

exports.handler = (event, context, callback) => {
  process.stdout.write('hi,fc\n');
  console.log('hello,world');
  context.logger.info('hello,fc');
  callback(null, 'hello,world');
};

Use process.stdout.write to print logs

If you use this method to print logs, the log content is printed as is. The following logs are displayed:

hi,fc

Use console.log 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 logs are displayed:

2023-04-01T10:04:19.024Z 19b394a3-4fff-480c-9b5c-cbdfd6952f4e [silly] hello,world

Use context.logger to print logs

If you set instance concurrency of the function to a value greater than 1, one function instance concurrently processes multiple requests. In this case, we recommend that you use context.logger to print logs to distinguish logs of each concurrent request by request ID. The following logs are displayed:

2023-04-01T10:04:19.024Z 19b394a3-4fff-480c-9b5c-cbdfd6952f4e [info] hello,fc

View logs

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