Skip to content

chore: better logging #2221

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

Closed
wants to merge 12 commits into from
Closed

chore: better logging #2221

wants to merge 12 commits into from

Conversation

MarcL
Copy link
Contributor

@MarcL MarcL commented Jul 19, 2023

Description

This PR adds the winston logger to enable us to have clearer logging in the runtime. It will help us to add additional logging to help with debugging but avoid additional clutter in the logs. This is due to the default log level of info being set.

This is a suggestion to see if the team think it'll be useful. It will allow us to set a LOG_LEVEL environment variable to enable logging levels below the current default. With this, we can instrument the code and allow customers to enable a lower log level if we're struggling to debug issues.

I've added the no-console eslint rule to avoid accidental console.* statements but have not enabled it for tests. See the inline comments for more details.

I considered using pino too as it's faster. However, by default, it generates structured JSON logs which can't currently be ingested by Netlify's build process. You can use the pino-pretty package to display them nicely but it adds an additional timestamp. winston gave a simpler 1-line transport which has parity with our console logging so in the short term we can keep this.

Documentation

N/A

Tests

I've updated tests which spy or mock console.* to use the logger.* instead.
There is 1 failing snapshot test that we need to look at before merging.

Relevant links (GitHub issues, etc.) or a picture of cute animal

image

#2220

@netlify
Copy link

netlify bot commented Jul 19, 2023

Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

Name Link
🔨 Latest commit 451b689
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/64be7a7c21e32d000815f24a
😎 Deploy Preview https://deploy-preview-2221--netlify-plugin-nextjs-static-root-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 19, 2023

Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

Name Link
🔨 Latest commit 451b689
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/64be7a7c5e275500081d4951
😎 Deploy Preview https://deploy-preview-2221--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 19, 2023

Deploy Preview for next-plugin-edge-middleware ready!

Name Link
🔨 Latest commit 451b689
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/64be7a7c6cb471000839fd77
😎 Deploy Preview https://deploy-preview-2221--next-plugin-edge-middleware.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 19, 2023

Deploy Preview for netlify-plugin-nextjs-export-demo ready!

Name Link
🔨 Latest commit 451b689
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/64be7a7cf7e6320008b3979d
😎 Deploy Preview https://deploy-preview-2221--netlify-plugin-nextjs-export-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 19, 2023

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit 451b689
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/64be7a7cb2b35f000848c8b6
😎 Deploy Preview https://deploy-preview-2221--next-i18next-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added the type: chore work needed to keep the product and development running smoothly label Jul 19, 2023
@netlify
Copy link

netlify bot commented Jul 19, 2023

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit 451b689
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/64be7a7c56443d0007b3d3d6
😎 Deploy Preview https://deploy-preview-2221--next-plugin-canary.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 19, 2023

Deploy Preview for netlify-plugin-nextjs-demo ready!

Name Link
🔨 Latest commit 451b689
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/64be7a7c5e275500081d495b
😎 Deploy Preview https://deploy-preview-2221--netlify-plugin-nextjs-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 19, 2023

Deploy Preview for nextjs-plugin-custom-routes-demo ready!

Name Link
🔨 Latest commit 451b689
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/64be7a7c9b411c0008ff1374
😎 Deploy Preview https://deploy-preview-2221--nextjs-plugin-custom-routes-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 19, 2023

Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!

Name Link
🔨 Latest commit 451b689
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/64be7a7cc821280008f57b19
😎 Deploy Preview https://deploy-preview-2221--netlify-plugin-nextjs-next-auth-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -0,0 +1,3 @@
// Winston supports JSON by default but this is using a custom formatter
// It only gives the message and not the timestamp or log level
export { logger as default } from './winston'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had this logger exposed via an index file as I was trying both winston and pino. Pino is meant to be faster but by default supports JSON logging. Even with their pretty formatter, it would make our logs a little strange with multiple timestamps.

We can move this to a single logger module that only includes winston if needed.

import winston from 'winston'

// Define log level from environment variable so we can change it if needed
const logLevel = process.env.LOG_LEVEL ?? 'info'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will allow us to only display logger.info and higher levels (.warn, .error) into the user-facing logs.

We can use the environment variable with customers or support so we can use logger.debug or similar in the code.

orinokai
orinokai previously approved these changes Jul 20, 2023
Copy link
Contributor

@orinokai orinokai left a comment

Choose a reason for hiding this comment

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

Thanks Marc! Seems like a no-brainer as it gives us extra flexibility and observability, plus we can avoid leaving accidental console.log in the code. Unless the performance difference is huge, I'd advocate for using Winston instead of developing a custom transport for Pino.

@@ -30,6 +30,7 @@ module.exports = {
'unicorn/no-array-push-push': 0,
'unicorn/numeric-separators-style': 0,
'max-lines': 0,
'no-console': 2,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ESLint now errors for all console.* statements.

The following routes use "revalidate" values of under ${MINIMUM_REVALIDATE_SECONDS} seconds, which is not supported.
They will use a revalidate time of ${MINIMUM_REVALIDATE_SECONDS} seconds instead.
`)

// Disabling lint rule as the logger doesn't have a table formatter
// eslint-disable-next-line no-console
console.table(shortRevalidateRoutes)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure what we wanted to do here. We'd have to write some code to format as a table so I've left console.table as it was.

Choose a reason for hiding this comment

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

This could always be a logger.info(JSON.stringify(shortRevalidateRoutes, null, 2)). Or we could create a logger.table that wraps my suggestion.

Copy link

@nickytonline nickytonline Jul 20, 2023

Choose a reason for hiding this comment

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

Actuallym, nvm. That would behave more like console.dir, not console.table.

@@ -1,3 +1,6 @@
// This is a runtime function so assuming we don't want to add
// an additional dependency on a logger (Winston) for runtime code
/* eslint-disable no-console */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

My assumption for all of our runtime code is that we don't want to add the extra dependency of winston to avoid bundle bloat.

Choose a reason for hiding this comment

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

Yeah, the new logger should be just for build time, no runtime code for the reason you mention. I do wonder if we can use the system log in handlers we generate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's look at that for the next PR. Sounds like a good idea.

@@ -1,3 +1,5 @@
// Do we want logging in the tests?
/* eslint-disable no-console */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if we care about console.* statements in the tests.

Personally, I'm not sure we should be including additional logging in the tests as the tests should provide the information we need.

Thoughts?

Copy link

@nickytonline nickytonline Jul 20, 2023

Choose a reason for hiding this comment

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

I don't think they're necessary in the tests either. Maybe do that cleanup in another PR?

@@ -810,9 +813,9 @@ describe('onPostBuild', () => {
},
])

const oldLog = console.log
const oldLoggerInfo = logger.info
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left this as was but we could probably just spy on the logger.info here to confirm it gave the message we expect.

@MarcL
Copy link
Contributor Author

MarcL commented Jul 20, 2023

I've updated the PR and added logging to all of the code.

@MarcL MarcL requested a review from nickytonline July 20, 2023 11:33
@MarcL MarcL force-pushed the marcl/better-logging branch from 158a3d1 to a223e71 Compare July 20, 2023 11:36
@MarcL
Copy link
Contributor Author

MarcL commented Jul 20, 2023

Some test snapshots fail so I'll need to work with @nickytonline or @orinokai to understand why. They don't look like they're related to any of the changes in this PR. 🤔

[EDIT]: I think these fail on main too.

Adding a fix for the failing tests here: #2224

@MarcL MarcL requested a review from orinokai July 20, 2023 11:43
@MarcL MarcL marked this pull request as ready for review July 20, 2023 11:43
@MarcL MarcL requested a review from a team as a code owner July 20, 2023 11:43
Copy link

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

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

This is looking great @MarcL! Left some questions/comments.

@@ -210,7 +213,7 @@ export const warnForProblematicUserRewrites = ({

export const warnForRootRedirects = ({ appDir }: { appDir: string }) => {
if (existsSync(join(appDir, '_redirects'))) {
console.log(
logger.info(
yellowBright(

Choose a reason for hiding this comment

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

do we need the colours still, or does winston provide different colours for different log levels?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. It does allow you to colour them but we'd have to add an additional config.

https://github.com/winstonjs/winston#colorizing-standard-logging-levels

Should we do this and remove our use of chalk or do we want to leave it as is so it matches the current behaviour?

Choose a reason for hiding this comment

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

Maybe in a small follow up PR once the new logger is up and running. If it's not a big lift, go for it now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I took a look and we'd have to ensure we used the correct log levels. In some cases, we're changing colours but still using logger.info. Might be worth shipping this and then updating the log levels if we're considering some of them as different messages.

@@ -1,3 +1,6 @@
// This is a runtime function so assuming we don't want to add

Choose a reason for hiding this comment

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

double check, but I think you might be able to exclude the whole packages/runtime/src/templates/ folder from this eslint rule.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great. I'll check this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you do this for a single rule @nickytonline?
I can see how you exclude linting a set of files but not for a single rule.

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! 👍🏻

@MarcL MarcL force-pushed the marcl/better-logging branch from 5c22126 to 6a68d3e Compare July 21, 2023 15:01
@MarcL
Copy link
Contributor Author

MarcL commented Oct 17, 2023

Going to close this as we're moving forward with other work.

@MarcL MarcL closed this Oct 17, 2023
@MarcL MarcL deleted the marcl/better-logging branch October 17, 2023 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: chore work needed to keep the product and development running smoothly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants