Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logs not showing correctly when using sam #15

Open
code-is-art opened this issue Dec 14, 2020 · 18 comments
Open

logs not showing correctly when using sam #15

code-is-art opened this issue Dec 14, 2020 · 18 comments
Labels
bug Something isn't working

Comments

@code-is-art
Copy link

code-is-art commented Dec 14, 2020

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

@code-is-art code-is-art changed the title sam logs not showing correctly logs not showing correctly when using sam Dec 14, 2020
@chasemaier
Copy link

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:

  • It is clearly not a valid JSON object being output
  • The "closing" braces appear EARLIER on the same line of a wrapped url string, when in reality they should be lines later in the output.
  • The prefix "EVENT" doesn't even make it into the final output.

image

@bitconym
Copy link

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

@linjungz
Copy link

linjungz commented Mar 3, 2021

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
Copy link

es50678 commented Mar 8, 2021

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

Envek pushed a commit to Envek/aws-sam-typescript-layers-example that referenced this issue Apr 21, 2021
Current version of the container aws-lambda-runtime-interface-emulator has a bug which corrupts the output of sam local debug statements. This implements the workaround suggested in aws/aws-lambda-runtime-interface-emulator#15 (comment)
@tw-Dan
Copy link

tw-Dan commented Apr 27, 2021

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
Copy link

cross posting sorry aws/aws-sam-cli#1359 (comment)

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
Copy link

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

@habdulla-Onovative
Copy link

@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
Copy link

tw-Dan commented May 20, 2021

@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
Copy link

vh-dao commented Dec 3, 2021

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
Copy link

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

process.stdout.write(JSON.stringify(event, null, "  ") + "\n");

@ghost ghost added the bug Something isn't working label Aug 17, 2022
@dmngu9
Copy link

dmngu9 commented Sep 28, 2022

process.stdout still doesnt work

@johndanek
Copy link

johndanek commented Feb 22, 2023

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
Copy link

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
Copy link

mkessy commented Sep 26, 2023

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
Copy link

tyrnut commented Dec 15, 2023

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
Copy link

juppdes commented Jul 4, 2024

In Windows, still not working :/

@2yan
Copy link

2yan commented Jul 31, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests