Search code examples
apigeegoogle-cloud-code

Apigee JavaScript resource output running Cloud Code emulator?


I'm writing Apigee JavaScript Policy Resources using the VS Code Cloud Code plugin and can't locate the print statement output- is it available while running the Apigee (Docker) emulator?

TIA.


Solution

  • print output is not available in the Apigee local development emulator, it is only available using Debug or Trace in the actual Apigee product.

    To work around this I've written a couple of helper functions that log to the context and then dump the logs to the Response header:

    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;
            }
        }
    }
    

    Sample output from 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