-2

我正在使用 VS Code Cloud Code 插件编写 Apigee JavaScript Policy Resources,但找不到print语句输出 - 它在运行 Apigee (Docker) 模拟器时是否可用?

TIA。

4

1 回答 1

0

print输出在 Apigee 本地开发模拟器中不可用,它只能在实际 Apigee 产品中使用 Debug 或 Trace 提供。

为了解决这个问题,我编写了几个辅助函数,它们记录context到 Response 标头,然后将日志转储到 Response 标头:

function logMsg(msg) {
   var index = context.getVariable("logger.index");
   if (index == null) {
       index = 1;
   }
   context.setVariable("logger.logMsg-" + index, msg);
   index = index + 1;
   context.setVariable("logger.index", index);
}

// Call this function during Response processing
function logsToResponseHeader() {
    var index = context.getVariable("logger.index");
    if (index == null) {
        context.proxyResponse.headers['Z-Log-0'] = 'logger.index == null';
        return
    }

    context.proxyResponse.headers['Z-Log-0'] = 'logger.index == ' + index + ' type: ' + typeof (index);
    for (var i = 1; i < index; i++) {
        var msg = context.getVariable("logger.logMsg-" + i);
        if (msg == null) {
            context.proxyResponse.headers['Z-Log-' + i] = 'Null msg';
        } else {
            context.proxyResponse.headers['Z-Log-' + i] = msg;
        }
    }
}

示例输出curl -v

< Z-Log-0: logger.index == 11 type: number
< Z-Log-1: Trace-Context-Request: enter
< Z-Log-2: Trace-Context-Request: exit
< Z-Log-3: Trace-Context-Response: enter
< Z-Log-4: WARN: null value for TraceEndTimestamp: 
< Z-Log-5: RequestParentID: 
< Z-Log-6: ParentID: 769db5b4
< Z-Log-7: TraceID: 9b5c3406fdd0d275
< Z-Log-8: TraceFlags: 01
< Z-Log-9: parentStartTime: 1640441735345
< Z-Log-10: Trace-Context-Response: exit
于 2021-12-25T15:30:43.400 回答