Amazon Cloudwatch JSON format not correct - aws-lambda

I am logging Amazon skill requests and replies in the Lambda function of my Alexa skill. These are JSON objects that I am logging as follows:
logger.debug('Incoming request:\n' + JSON.stringify(event, null, 2));
logger.debug('Final response:\n' + JSON.stringify(alexaResponse, null, 2) + '\n\n');
When viewing the logs in Cloudwatch with Expand all = Row I see this:
Cloudwatch Log with Expand all set to Row
When I set Expand all to Text, the result is slightly better, but the leading spaces are trimmed, causing the indentation structure of the JSON document to be lost.
Cloudwatch Log with Expand all set to Text
I got this code from a tutorial. It is possible that changes in Amazon Cloudwatch have made code that worked in the tutorial fail now.
In the tutorial, the output looks like this:
Tutorial screen shot
That's the log output I want. How do I get it?

My cloudwatch logs automatically display JSON strings as you want them without using the optional 2nd and 3rd arguments of JSON.stringify()
So try removing the null and 2 from your log statement's JSON.stringify.
logger.debug('Incoming request: \n' + JSON.stringify(event));
logger.debug('Final response: \n '+ JSON.stringify(alexaResponse));
I'm not at my computer to test, but I think that new line (\n) should give you the desired effect in cloudwatch, placing the json in its own line that you can expand. And its the extra whitespace that is making cloudwatch set new rows in the JSON.

With the limited experience i have, all the new lines are converted into a separate log entry.
IMHO it is behaving as line logger, instead of entry logger.
So what you can do is, remove the new lines from log entries, when sending to cloudwatch.
It should automatically show the log json in formatted structure.

Related

multiValueQueryStringParameters missing from lambda call

I made a call to an API Gateway connected to a Lambda function. I was expecting to see multiValueQueryStringParameters as a key in the event input to my handler.
https://aws.amazon.com/blogs/compute/support-for-multi-value-parameters-in-amazon-api-gateway/
Instead, I was seeing a comma delimited list in queryStringParameters
So for example, this call:
https://12324234234234.execute-api.us-east-2.amazonaws.com/dois_to_pmids?a=1&a=2&a=3
generates the following event entry:
"queryStringParameters": {"a": "1,2,3"}
I tried making everything a proxy integration, first be doing:
GET /{proxy+}
and then by doing:
ANY /{proxy+}
with no luck.
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html
https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
What's going on?
So after a bit of digging I discovered an option when setting up the integration to select a payload version (either 1 or 2). You can currently read about the payload version formats (sort of, details are a bit light) here:
https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
The main takeaway from that link is that version 2 doesn't have multiValueQueryStringParameters, instead repeated parameters are concatenated together in a comma separated list (as observed).
Haven't tested how this works with values that have commas :/

Ignore formatting in Slack App when reading messages

I'm building a Slack App which is only interested in actual user input in message, regardless if it's bold, italic, or a web link.
What would be the best way to remove all the formatting characters, such as _, *, etc, and leave actual text only?
The best solution is actually to ignore the text param entirely, as it is inherently lossy, and to instead parse what you want from the blocks param, which gives an exact representation of what the user saw in their slack client message field when they posted their message.
To demonstrate this, here's a simple example
what you type
WYS
text
blocks[0]["elements"]["elements"]
parsing text from blocks
[a][space][*][b][*]
​a b
a *b*
[{"type":"text","text":"a "},{"type":"text","text":"b","style":{"bold":true}}]
a b
[a][*][b][*][←][←][←][space]
a *b*
a *b*
[{"type":"text","text":"a *b*"}]
a *b*
Unfortunately for now, the Slack API does not give you the blocks param in command events, only message events. Hopefully they will fix this, but until then you may wish to use regular messages instead of slash commands.
If you are using node.js there is a module for this:
https://openbase.io/js/remove-markdown/documentation
Note that this is for markdown and not Slack's variant mrkdwn so it might not work.
Managed to get the unformatted message in python by looping through the array.
def semakitutu(message, say):
user = message['user']
# print(message)
hasira = message['blocks']
hasira2 = hasira[0]['elements'][0]['elements']
urefu = len(hasira2)
swali = ''
for x in range(urefu):
swali=swali+hasira2[x]['text']
print(swali)
say(f"Hello <#{user}>! I am running your request \n {swali}")```

Parse email subject office 365 flows

I am trying to get some data parsed out of a subject line in Office 365 Flows. I have an email that has a consistent format:
Help Desk [Ticket #12345]
I want to get the number '12345' for use in later steps in the flow. So far, I've attempted to use the substring expression in a compose connector:
substring(triggerBody()?['Subject'], 20, 5)
But I get an error about the string being null.
Besides the index being incorrect (to retrieve '12345' from Help Desk [Ticket #12345] you need to use substring(value, 0, 5) as the index is 0-based), the expression looks correct. But you can take a step-by-step approach to see what is wrong.
To start, take a look at the flow run to see exactly what the trigger outputs are:
If you see the Subject field (as I do in my case), create a variable containing that value only to make sure that you don't have any typo:
If it works correctly, then you should see in the flow run the subject:
If everything is still good at that point, create a new variable with the substring that you want:
And again, check the value.
If you got to this point, then you should be able to retrieve the ticket id.

CloudWatch filter pattern for ruby logger

With AWS CloudWatch Logs, i'd like apply a filter, separating the fields in a message generated by a ruby logger.
I, [26/Oct/2015:04:35:12 +0000#11] INFO -- : (0.000934s) BEGIN
This is how the format is specified according to the Ruby docs:
SeverityID, [DateTime #pid] SeverityLabel -- ProgName: message
How do i extract the labels using a CloudWatch filter? Namely, how can i extract values between certain characters like the pID and parts of the actual message?
the short answer is that you cannot do that.
If logging in json format is an option you could potentially do something around extracting fields/values.
Docs are here: http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/FilterAndPatternSyntax.html

Parse.com Cloud Code log only returns Input and Result

I am trying to debug some code on parse.com. I am using console.log to print to console various things but when I try to read the log file all I get is the Input and the Result. This is what I have tried so far
console.log("whatever") //nothing
console.warn("whatever") //nothing
In the terminal
parse log -l INFO //This gives me just Input and Result
Anyone knows how to do this?
Thanks in advance.
You need to pass a JSON string, not a regular string. For example: console.log({"hello":"world!"});
I can't find the reference, and even they use normal string in their example code, but it works fine with JSON strings.

Resources