aws / aws-lambda-runtime-interface-emulator

Apache License 2.0
914 stars 95 forks source link

logs not showing correctly when using sam #15

Open code-is-art opened 3 years ago

code-is-art commented 3 years ago

Description When using sam invoke, the output of console.log is not displayed correctly as described in this issue.

Steps to reproduce console.log('line one\rline two\rline three\nline four\nline five')

will output

START RequestId: ac1ef293-d504-16a9-6b21-20a04c7f98e6 Version: $LATEST line fiveeT14:12:51.527Z ac1ef293-d504-16a9-6b21-20a04c7f98e6 INFO line one END RequestId: ac1ef293-d504-16a9-6b21-20a04c7f98e6

and console.log(json.stringify({one: 'line one', two: 'line two', three: 'line three', four: 'line four'}, null, 2)) is just as problematic. One can only read the first and last lines, usually { and}

According to @mhart

This is due to some unfortunate internal changes in the runtimes where console.log is overwritten by a function that transforms all newlines to \r.

Additional environment details os: OSX 10.14.6 sam cli versions tested: 1.3.0, 1.12.0, 1.13.2

chasemaier commented 3 years ago

I'm also seeing this, but NOT related to use of SAM. Just using a docker image (extended from FROM public.ecr.aws/lambda/nodejs:latest) and manually using curl to test as described here.

The JSON object being logged (in my case the event) seems to be fine, it's just missing several lines of the multi-line log message (and seems to be overwriting lines that have already been written previously), so it seems to only be an output problem (everything functional is fine).

image

This one statement is supposed to be logging a JSON event object containing multiple layers of data. As seen in the screenshot:

image

bitconym commented 3 years ago

Any update on this? Quite annoying to see logs printed like this.

linjungz commented 3 years ago

I'm also seeing this issue under this environment:

SAM CLI, version 1.19.1 OSX 10.14.6

It would be great if this issue could be fixed asap. Thanks

es50678 commented 3 years ago

also seeing this -

SAM CLI, version 1.20.0
OSX 11.2.2

As a workaround: sam local [invoke | start-api] 2>&1 | tr "\r" "\n" fixes the issue for me. Found the solution here: https://github.com/aws/aws-sam-cli/issues/1359#issuecomment-744469252

Thanks to @code-is-art for the workaround

tw-Dan commented 3 years ago

also seeing this -

SAM CLI, version 1.20.0
OSX 11.2.2

As a workaround: sam local [invoke | start-api] 2>&1 | tr "\r" "\n" fixes the issue for me. Found the solution here: aws/aws-sam-cli#1359 (comment)

Thanks to @code-is-art for the workaround

Anyone know if it's possible to use this workaround in windows? When I run it in CMD it does not start and I get no output.

rawpixel-vincent commented 3 years ago

cross posting sorry https://github.com/aws/aws-sam-cli/issues/1359#issuecomment-831837585

Hi there, I fixed the issue for us by adding "test": "sam local invoke ... 2>&1 | tr \"\r\" \"\n\"" in our package.json script section.

However, this issue has been opened for two years and it seems like it should be fixed, can you either give a reason not fix the issue and add a gentle note to the sam cli documentation (with that workaround), or fix it?

If not planning to fix, adding a note to the docs will save time to figure it out -- many people like me could waste a lot of time thinking they are doing something wrong.

thanks

habdulla-Onovative commented 3 years ago

Looking for a workaround for windows as well. tr isn't a natively available command in windows shell/powershell.

habdulla-Onovative commented 3 years ago

@tw-Dan I figured out a workaround for windows PowerShell:

sam local [invoke | start-api] 2>&1 | % {$_.ToString().replace("`r`n","`n")}

Here is my full setup, wanting to output to the screen/console as well as to a log file since the --log-file argument on the sam cli will prevent output to the screen:

sam local [invoke | start-api] 2>&1 | % {$_.ToString().replace("`r`n","`n")} | Tee-Object -Variable out; Out-File -InputObject $out sam.log -Encoding utf8

tw-Dan commented 3 years ago

@tw-Dan I figured out a workaround for windows PowerShell:

sam local [invoke | start-api] 2>&1 | % {$_.ToString().replace("`r`n","`n")}

Here is my full setup, wanting to output to the screen/console as well as to a log file since the --log-file argument on the sam cli will prevent output to the screen:

sam local [invoke | start-api] 2>&1 | % {$_.ToString().replace("`r`n","`n")} | Tee-Object -Variable out; Out-File -InputObject $out sam.log -Encoding utf8

Thanks for taking the time to share this with me. I have been trying to fix this intermittently over the past few weeks!

vh-dao commented 2 years ago

sam local [invoke | start-api] 2>&1 | % {$_.ToString().replace("`r`n","`n")} If I put into package.json, it does not work:

  "dev": "sam local start-api 2>&1 | % {$_.ToString().replace('`r`n','`n')}"

npm run dev --------> Error

zelibobla commented 2 years ago

Possible workaround from codebase side is just replace console.log by process.stdout:

process.stdout.write(JSON.stringify(event, null, "  ") + "\n");
dmngu9 commented 1 year ago

process.stdout still doesnt work

johndanek commented 1 year ago

This is still an issue in Feb 2023 with macOS Ventura 13.1 and SAM version 1.73.0

If you're just trying to print an object to the console for debugging, wrapping the object in JSON.stringify seems to work better.

i.e. changing console.debug(event); to console.debug(JSON.stringify(event));

arpadgabor commented 1 year ago

also seeing this -

SAM CLI, version 1.20.0
OSX 11.2.2

As a workaround: sam local [invoke | start-api] 2>&1 | tr "\r" "\n" fixes the issue for me. Found the solution here: aws/aws-sam-cli#1359 (comment)

Thanks to @code-is-art for the workaround

This is still the best solution as of today.

The problem with stringify is that non-serializable values are stripped out, and if you try to log an object on multiple lines without the solution above, you only see the first line. This really needs a fix.

mkessy commented 11 months ago

Just commenting that this still seems to be an issue. Not using SAM just running it through a regular docker container it appears that console.log get's cut off at one line. It does seem that process.stdout.write is a work around but of course less than convenient.

tyrnut commented 8 months ago

This is still an open issue. Hoping we can get a fix?

edit: I just noticed https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/main/src/LogPatch.js#L86C23-L86C32, which seems like a likely culprit...so maybe not the RIE at all?

edit: AH HA! This environment variable will fix things (if you don't mind JSON logs)

AWS_LAMBDA_LOG_FORMAT=JSON

juppdes commented 1 month ago

In Windows, still not working :/

2yan commented 3 weeks ago

So it turns out.. the good old print statement works just fine.