11.12.2. TypeScript 上下文对象引用

上下文 对象具有多个属性,可供函数开发人员访问。访问这些属性可提供有关传入 HTTP 请求的信息,并将输出写入集群日志。

11.12.2.1. log

提供一个日志记录对象,可用于将输出写入集群日志。日志遵循 Pino 日志记录 API

日志示例

export function handle(context: Context): string {
    // log the incoming request body's 'hello' parameter
    if (context.body) {
      context.log.info((context.body as Record<string, string>).hello);
    } else {
      context.log.info('No data received');
    }
    return 'OK';
}

您可以使用 kn func invoke 命令访问功能:

示例命令

$ kn func invoke --target 'http://example.function.com'

输出示例

{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}

您可以将日志级别更改为 fatalerrorwarninfodebugtracesilent 之一。为此,请使用 config 命令将其中的一个值分配给环境变量 FUNC_LOG_LEVEL,以更改 logLevel 的值。