日志 本文介绍如何在Node.js运行环境下打印和查看函数日志。 打印日志 当需要查看函数运行相关的自定义状态时,可以使用如下几种方式打印日志至标准输出stdout。往标准输出stdout打印的日志内容会被函数收集。 ES模块 export const handler async (event, context) > { process.stdout.write('hi,fcn'); console.log('hello,world'); context.logger.info('hello,fc'); return "Hello World!"; }; CommonJS模块 'use strict'; exports.handler (event, context, callback) > { process.stdout.write('hi,fcn'); console.log('hello,world'); context.logger.info('hello,fc'); callback(null, 'hello,world'); }; 以下分别介绍所使用的几种日志打印方法。 使用process.stdout.write打印日志 使用此方法打印日志会将内容原样输出到日志中。输出的日志内容如下所示。 hi,fc 使用console.log打印日志 使用此方法打印的每条日志中都会包含时间、RequestId、日志级别等信息。输出的日志内容如下所示。 20240304 07:01:16.927 165e571bc158a59e8b63f98cd471c [info] hello,world 直接使用context.logger打印日志 当您配置的函数实例并发度大于1时,一个函数实例会同时并发处理多个请求。此时强烈建议使用context.logger打印日志,以通过RequestId区分各并发请求的日志。输出的日志内容如下所示。 20240304 07:01:16.927 165e571bc158a59e8b63f98cd471c [[object Object]] hello,fc 查看日志 函数执行完成后,您可以在函数详情页的日志页签查看日志信息。