5

I am using the default HelloWorld example My code is:

let response;
exports.lambdaHandler = async (event, context) => {
    console.log(event);
    try {
        response = {
            'statusCode': 200,
            'body': JSON.stringify({
                message: 'hello world'
            })
        }
    } catch (err) {
        console.log(err);
        return err;
    }

    return response
};

Everything works as expected. I am starting it with sam local start-api or sam local start-api --debug

My questions, Where do I see the output of console.log(event);
Currently I can only see a {.
I if put there a string, like 'AAAAAAAA' I would see that in the terminal where I typed sam local start-api (as expected).

what am I missing so I can see full local logs?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278

1 Answers1

7

In your code, put in JSON.stringify to convert the object to a string from logging,

console.log(JSON.stringify(event));

You can then see the logging in the terminal/command-prompt via

sam local start-api

Or have it log to a file with:

sam local start-api --log-file logfile.txt

petey
  • 16,914
  • 6
  • 65
  • 97
  • Piggy-backing to add: You can use `--log-file /dev/stdout` to just log in the same window as your sam command. This will make logs a little jumbled though. – JonTroncoso Aug 25 '23 at 22:56