Cloudwatch - console.log vs console.dir (nodejs lambda)

4

Hello, i have recently used console.dir in our nodejs lambda. I was quite surprised to see that output in CW is different than those of console.log. When i run the same test in nodejs REPL the result is the same.

Lambda test code

const data = {traceId:'de719dc830dd30b74983f14e861ebxxx',parentId:'ae0efdedc04c4xxx',name:'getAllBusinessObjectEventsSpan',id:'85263fd6abb5bxxx',kind:0,timestamp:655413722127740,duration: 4140227,attributes: {},status: { code: 0 },events:[],links: []};

console.log(data);
console.dir(data, { depth: 3});

Result in CW:

  • console.log - there is prefix with timestamp, correlation id and log level (2022-06-29T06:46:05.747Z 9e516xxx-axxx-4xxx-8xxx-xxxxxxxxxxxx INFO) and whole json is written as a single log record (this is expected)
  • console.dir - no prefix is present and each property in json is printed in its own log record (even opening and closing parenthesis)

I thought behaviour should be identical as nodejs REPL yields same results and. Is logging by console.dir not supported in lambdas? Thanks a lot for any ideas

1개 답변
1

Hello,

The Lambda services provided NodeJS runtime will include the timestamp, request ID, and log level when you use console.log although it’s a bit hidden in our documentation: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-logging.html#node-logging-output

It adds a timestamp, request ID, and log level to each entry logged by the function. 

Is the key information. It doesn’t appear we include the same information for console.dir however but you can use the context object to extract the request id, and include timestamps if you want that included in your own console.dir logging.

As far as the differences in the CloudWatch log entries it appears as if it’s more of a NodeJS implementation item instead of anything the Lambdas provided NodeJS runtime is doing; I used three different JSON and it turns out the third one below was written to a single line when viewed in CloudWatch logs:

const data = {traceId:'de719dc830dd30b74983f14e861ebxxx',parentId:'ae0efdedc04c4xxx',name:'getAllBusinessObjectEventsSpan',id:'85263fd6abb5bxxx',kind:0,timestamp:655413722127740,duration: 4140227,attributes: {},status: { code: 0 },events:[],links: []};

const data = {kind:0,timestamp:655413722127740,duration: 4140227,attributes: {},status: { code: 0 },events:[],links: []};

const data = {attributes: {},status: { code: 0 },events:[],links: []};

It looks like depending on the entries found within the JSON it’ll either do multiple entries in a single CloudWatch log entry or a single line. As far as NodeJS REPL I’m not familiar with its underlying implementation myself.

The NodeJS documentation may have some further information on the implementations between the two:

https://nodejs.org/docs/latest/api/console.html#consolelogdata-args

https://nodejs.org/docs/latest/api/console.html#consoledirobj-options

And I think the key item may be console.dir uses util.inspect while I can’t see that console.log does. For a deeper answer that’s more definitive, I’d recommend reaching out to NodeJS folks on their GitHub page (the documentation linked me there): https://github.com/nodejs/node

AWS
지원 엔지니어
Tim_P
답변함 2년 전
profile picture
전문가
검토됨 한 달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠