How to embed a JSON response in a logging policy

Not applicable

When an error occurs, I want to log the content of the response (from wf.message.content, below). I've tried escaping the '{' and '}" characters, but the logging still does not occur properly. The beginning of the logging policy looks like this:

    <Message defaultVariableValue="">[{message.header.x-request-id:not applicable}] \{"status": {message.status.code}, "message_content": "{wf.message.content}", "method": "{request.verb}", ...

The failing result of the policy looks like (note the blank area):

May 04 14:31:03 54.210.253.110 logger:             ", "method": "GET", ...

So whatever is happening with the processing of wf.message.content is prevent everything up to the trailing quote from displaying properly. (Note that if remove wf.message.content then the other fields display correctly). I create wf.message.content in a JS callout like this:

var messageContent = context.getVariable('message.content').replace(/({|})/g,"\\$1");
context.setVariable('wf.message.content', messageContent);

and the text it processes results in this:

\{ "data": \{ \}, "errors": \{ "protocol": [ "must be HTTPS" ] \}, "meta": \{ "api_version": "1.1", "deprecation_information": \{ \} \} \}

That appears to not be sufficient. Any ideas?

1 5 955
5 REPLIES 5

George, can you try something like this:

<Message variablePrefix='%' variableSuffix='#'>  <![CDATA[{
"status" : %message.status.code#,
"method" : "%request.verb#",
"message_content" : %wf.message.content#
}
]]></Message>

The code within Apigee Edge that fills in Message templates, currently looks for open-curly braces by default as the thing that flags a variable to be replaced. If you want your message to be json, then... obviously that's a problem. So there's a way to replace the variable prefix and suffix with alternatives, as shown above.

In the future, we expect this won't be necessary , so that if you have a json payload, Apigee Edge will intelligently be able to determine what is a "real" open curly and what is a variable reference. But for now you should be able to this approach.

Unfortunately, that does not seem to work. I get no logging at all, even if I put other text outside the CDATA

Not applicable

Unfortunately, that does not seem to work. I get no logging at all, even if I put other text outside the CDATA.

@dino, I'm with the same problem, using extension "Google Stackdriver Logging", there is solution for this?

hi

The stackdriver logging extension is totally different than the MessageLogging policy.

So it seems it's not the same problem, but a related one.

Normally this is the point at which I say "Please ask a new question". But for some reason people are unable to ask new questions these days, something wrong with the site. I am unable to solve that problem, and I don't know the status.

Anyway,

Back to the Stackdriver Logging Extension. I would avoid it. Stackdriver, now called Google Cloud Logging, presents a RESTful API, and you can use it directly from a ServiceCallout or JavaScript.

Here is an example repo showing how it works.

It's true, it's not as simple as the Extension. But... if you encapsulate all the Cloud Logging stuff into a sharedflow, it can be pretty simple.