Open code-is-art opened 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).
This one statement is supposed to be logging a JSON event object containing multiple layers of data. As seen in the screenshot:
Any update on this? Quite annoying to see logs printed like this.
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
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
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.
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
Looking for a workaround for windows as well. tr isn't a natively available command in windows shell/powershell.
@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 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!
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
Possible workaround from codebase side is just replace console.log
by process.stdout
:
process.stdout.write(JSON.stringify(event, null, " ") + "\n");
process.stdout
still doesnt work
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));
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.
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.
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
In Windows, still not working :/
So it turns out.. the good old print statement works just fine.
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
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
Additional environment details os: OSX 10.14.6 sam cli versions tested: 1.3.0, 1.12.0, 1.13.2