question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Console transport does not log to AWS Lambda output

See original GitHub issue

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: v8.10
  • Operating System? Linux (AWS Lambda)
  • Language? TypeScript ^3.0.0 -> webpack -> ES5

What is the problem?

Deploying a NodeJS Lambda function that uses Winston Console transport for logging fails silently to deliver log commands to the Lambda output. No errors, just no logs. Winston Console transport prefers console._stdout and console._stderr to console.log() and console.error(), but Lambda does not support console._stdout and console._stderr.

From console.js:

    if (console._stdout) {
      // Node.js maps `process.stdout` to `console._stdout`.
      console._stdout.write(`${info[MESSAGE]}${this.eol}`);
    } else {
      // console.log adds a newline.
      console.log(info[MESSAGE]);
    }

The problem I see is that console._stdout and console._stderr exist in the Lambda runtime environment so the checks in Winston do not fall back to console.log(), etc.

What do you expect to happen instead?

I would expect Winston’s conditional check to account for the presence of console._stdout and console._stderr AND either a configuration or an environment detail that allows it to skip these methods of logging in AWS Lambda environments as they are unsupported.

Other information

I’m able to work around this in my lambda functions by including this hack in my handler file:

delete console['_stdout'];
delete console['_stderr'];

I’d love to be able to keep from modifying the prototype of console, but this works for now.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
vadivelselvarajcommented, Jan 15, 2020

After 1.5 days of troubleshooting, found that all I’d to do make the logs appear in aws lambda is to pass the logger level in the right character case. I was setting it to INFO instead of info and winston had no alert statements for this misconfig.

3reactions
ptimsoncommented, May 14, 2019

@davemkirk I managed to get this working using https://github.com/winstonjs/winston/issues/1305#issuecomment-387634897

import winston from 'winston'

const LEVEL = Symbol.for('level')
const MESSAGE = Symbol.for('message')

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new winston.transports.Console({
      log(info, callback) {
        setImmediate(() => this.emit('logged', info))

        if (this.stderrLevels[info[LEVEL]]) {
          console.error(info[MESSAGE])
          if (callback) callback()
          return
        }

        console.log(info[MESSAGE])
        if (callback) callback()
      },
    }),
  ],
})

export default logger
Read more comments on GitHub >

github_iconTop Results From Across the Web

Winston CloudWatch Transport not Creating Logs When ...
When I deploy this app to the lambda, the console logs for the lambda cloudwatch log show up (i.e. /aws/lambda/lambda-name), but it doesn't...
Read more >
AWS Lambda function logging in Node.js
To output logs from your function code, you can use methods on the console object , or any logging library that writes to...
Read more >
Using AWS Lambda extensions to send logs to custom ...
The Lambda service stores logs before sending to CloudWatch Logs and any subscribed extensions. If Lambda cannot deliver logs to the extension, ...
Read more >
AWS Lambda function logging in Python
You can use the Lambda console to view log output after you invoke a Lambda function. For more information, see Accessing Amazon CloudWatch...
Read more >
AWS Lambda – FAQs
Amazon CloudWatch Logs rates will apply. Q: How do I scale an AWS Lambda function? You do not have to scale your Lambda...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found