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

Dev: Fix Netlify Functions logging #722

Merged
merged 1 commit into from
Feb 21, 2020
Merged

Conversation

RaeesBhatti
Copy link
Contributor

- Summary
Fixes console.logs not working in latest version when running Netlify Functions with netlify dev.
Fixes: #716

- Test plan

  • Add console.log to your Netlify Function and run it through netlify dev

- Description for the changelog

Use winston to provide custom logger for lambda-local

- A picture of a cute animal (not mandatory but encouraged)
🐥

Copy link
Contributor

@erquhart erquhart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions:

  • do we know what regression caused logging to stop working?
  • can we use the getLogger() method of lambda-local to access the provided winston instance rather than adding our own?

@RaeesBhatti
Copy link
Contributor Author

  1. Yes, we switched from using our custom logic to invoke Netlify Function on netlify dev to using lambda-local, so that, we can support .env files and because lambda-local is more featureful. But the default logger in lambda-local logs input and output each Netlify Function event, which clutters the terminal. The only way to disable that was to set verboseLevel parameter to 0. But setting verboseLevel to a number less than 3 makes console.log statements from Netlify Functions not show up in console because of how the default logger logic is implemented.
  2. The only way to fix that without changing lambda-local codebase is to provide our own logger. Using getLogger is not useful in this context because that logger already has some custom logic that is causing the problem. Which is why we provide our own.

@erquhart
Copy link
Contributor

erquhart commented Feb 21, 2020

Thanks, looks good to me! I’m cool with merging without tests so we can get this released, but a PR with tests should come next - in this case just a test to ensure logs from a function work.

Sent with GitHawk

@erquhart erquhart merged commit 6c7ff78 into master Feb 21, 2020
@erquhart erquhart deleted the raees/functions-logging-fix branch February 21, 2020 01:16
@unclejustin
Copy link

@RaeesBhatti hello! Thanks for your effort on this, but it doesn't actually fix the problem for me. I'm on 2.36 which includes this change, but console.log still does nothing.

@RaeesBhatti
Copy link
Contributor Author

@unclejustin Can you please post a sample function to reproduce this issue!

@unclejustin
Copy link

Certainly. Also note that I can put the console.logs anywhere and none of them ever work.

const { retry, respondSuccess, respondError } = require('./utils')
const stripe = require('stripe')(process.env.STRIPE_SK)

const baseUrl = process.env.BASE_URL

exports.handler = (event, context, callback) => {
  console.log('Please work bby')
  
  switch (event.httpMethod) {
    // Code...
  }
}

@RaeesBhatti
Copy link
Contributor Author

The tried the following code and it's working as expected in the latest version:

const baseUrl = process.env.BASE_URL

exports.handler = (event, context, callback) => {
  console.log('Please work bby')
  
  switch (event.httpMethod) {
    // Code...
  }
}

Can you please make sure that you do a netlify version in the same directory to make sure you're running the latest version. Also, maybe try global uninstall and then install again. It could also be that if you're running with yarn or npx they're using different locally installed version than the latest one.
Screen Shot 2020-02-27 at 9 22 52 PM

@unclejustin
Copy link

It was yarn! @RaeesBhatti 🙇

Ran netlify version as you said and confirmed 2.35 ran which and found that I had installed initially with npm global. But "upgraded" with yarn 🤦‍♂

I'm all good, thanks for straightening me out 💪

@RaeesBhatti
Copy link
Contributor Author

No problem. Glad we could figure this out! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Console.log no longer working in netlify-cli/2.35.0
3 participants