You can print and view logs in Node.js runtimes. The printed 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
Logs that are printed by a function to the standard output (stdout) are stored in the specified Logstore in Simple Log Service. You can use one of the following methods to print logs to stdout. The following sample code provides an example:
ECMAScript modules
This example supports only Node.js 18 or later.
The sample code supports one-click deployment. You can deploy the sample code in Function Compute with one click. nodejs-fc-log-es
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
'use strict';
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. Sample log:
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. Sample log:
2024-03-04 07:01:16.927 1-65e571bc-158a59e8-b63f98cd471c [info] hello,world
Use context.logger to print logs
If you configure the instance concurrency of the function to a value that is greater than 1, 1 function instance concurrently processes multiple requests. In this case, we recommend that you use context.logger to print logs to distinguish the logs of each concurrent request by request ID. Sample log:
2024-03-04 07:01:16.927 1-65e571bc-158a59e8-b63f98cd471c [[object Object]] hello,fc
Custom log severity levels
Custom log severity levels are supported only in Nodejs18 and Nodejs20 runtimes.
You can customize log severity levels by using the ALIYUN_FC_LOG_LEVEL environment variable. The custom log levels, from lowest to highest severity, are: debug, info, warn, and error, with debug being the lowest and error the highest.
Only logs whose severity levels are higher than or equal to ALIYUN_FC_LOG_LEVEL are printed.
For example, if you configure ALIYUN_FC_LOG_LEVEL=warn
, only the logs whose log levels are higher than or equal to warn
are printed. By default, the level of logs printed by console.log
is info, which is lower than the warn level. In this case, logs of info level are not printed. Logs that are printed by using process.stdout.write()
are not affected by this environment variable.
Procedure
Create a code directory for testing, for example,
mycode
.Create the
index.js
code file in thecode
directory and save the following sample code toindex.js
.'use strict'; exports.handler = (event, context, callback) => { process.stdout.write('stdout log\n'); console.log('console log'); context.logger.debug('context log info'); context.logger.info('context log info'); context.logger.warn('context log warn'); context.logger.error('context log error'); callback(null, 'hello,world'); }
Log on to the Function Compute console. Create a function by selecting Event Function. Select Node.js 18 or Node.js 20 as the runtime and Upload Folder as the code upload method. For more information, see Create an event function.
On the function details page, click the Configurations tab. In the left-side navigation pane, click Environment Variables. In the Environment Variables section, click Modify.
The following logs are printed after you click Test Function if you configure
ALIYUN_FC_LOG_LEVEL=warn
:
stdout log
2024-06-19 08:48:51.672 1--xxx-xxx-xxx [warn] context log warn
2024-06-19 08:48:51.672 1--xxx-xxx-xxx [error] context log error
View logs
After the function is executed, you can view the logs on the Logs tab. For more information, see View function invocation logs.