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

fix: log json in production #1598

Merged
merged 9 commits into from Dec 8, 2021
Merged

Conversation

rethab
Copy link
Contributor

@rethab rethab commented Sep 29, 2021

in production mode -- according to the docs -- we should be logging json.

however with the most recent refactoring, that seems to got lost and probot now always logs in the "pretty" format.

this PR reinstates that: if the NODE_ENV is set to production, json logging is used unless specified otherwise. Else, it defaults to pretty.

fixes #1573

@rethab rethab requested a review from a team as a code owner September 29, 2021 18:00
@rethab rethab changed the title fix: log json in production fix: log json in production, fixes #1573 Sep 29, 2021
@rethab
Copy link
Contributor Author

rethab commented Sep 29, 2021

The tests fail, but hopefully you can still take a look, @gr2m just to make sure the general idea is sound.

re test failure: I guess it depends on the terminal whether colors are printed. Perhaps it would be enough to just make sure no { is part of the log output? (in other words, just remove the specific expects that are failing now). Please let me know how elaborate you want these tests to be

@rethab
Copy link
Contributor Author

rethab commented Oct 5, 2021

@gr2m friendly ping :)

@gr2m
Copy link
Contributor

gr2m commented Oct 5, 2021

sorry it will probably take some time before I find the time to look into this :( maybe some other @probot/maintainers can help out

@rethab
Copy link
Contributor Author

rethab commented Oct 8, 2021

Removed the color expectation to make the tests green. Hopefully this convinces a maintainer to take a look :)

@rethab
Copy link
Contributor Author

rethab commented Oct 18, 2021

Hi @gr2m, who would be the other maintainers? Or is there something I could do to make it easier for you to review this change?

@rethab
Copy link
Contributor Author

rethab commented Oct 28, 2021

@gr2m friendly ping 😬 😢

@gr2m
Copy link
Contributor

gr2m commented Oct 29, 2021

No need to keep pinging us. I hope to have some time to look into it soon, but cannot make promises, sorry. I just don't have free time right now

Copy link
Contributor

@tcbyrd tcbyrd left a comment

Choose a reason for hiding this comment

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

Great work and thanks for the PR! Found a blocker that I think we'll have to address before merging to fully fix this.

Comment on lines 29 to 31
if (isProd() && !getTransformStreamOptions.logFormat) {
getTransformStreamOptions.logFormat = "json";
}
Copy link
Contributor

@tcbyrd tcbyrd Dec 3, 2021

Choose a reason for hiding this comment

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

This part isn't working when run from from the CLI because logFormat defaults to "pretty" when not otherwise defined:

process.env.LOG_FORMAT || "pretty"

So logFormat is always defined, which means we never hit this block that sets it to json. We could either ignore LOG_FORMAT if isProd() or do something like:

Suggested change
if (isProd() && !getTransformStreamOptions.logFormat) {
getTransformStreamOptions.logFormat = "json";
}
const logPretty = !isProd() && getTransformStreamOptions.logFormat == undefined
getTransformStreamOptions.logFormat = logPretty ? "pretty" : "json";

The latter option would mean removing the CLI option that defaults to "pretty", and should work just fine if you explicitly set LOG_FORMAT since it wouldn't be undefined when we get here. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your feedback, @tcbyrd. Two things to consider here:

  • Should json and pretty be the only options? With your suggestion, we'd limit that.
  • Should we always use json in production?

I'm curious what your thoughts are on this. Is there a guideline we should follow from probot's perspective?

My thinking was: If the user specifies something, we take it. If the user doesn't specify anything and we're in production, then we use json. This has the consequence that, when running from the CLI, the user would have to explicitly specify json if they want to log json.

Depending on what you think about the two points above, we could also change the default for the cli to something like process.env.LOG_FORMAT || (isProd() ? "json" : "pretty").

Copy link
Contributor

Choose a reason for hiding this comment

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

Should json and pretty be the only options

json and pretty are the only formats built into pino. If you want another format, you'd have to use a custom pino logger.

process.env.LOG_FORMAT || (isProd() ? "json" : "pretty").

Looks like the right approach to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

src/helpers/get-log.ts Outdated Show resolved Hide resolved
src/helpers/is-prod.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@tcbyrd tcbyrd left a comment

Choose a reason for hiding this comment

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

LGTM! 🌮

@gr2m gr2m changed the title fix: log json in production, fixes #1573 fix: log json in production Dec 8, 2021
@gr2m gr2m merged commit bb068d9 into probot:master Dec 8, 2021
@github-actions
Copy link

github-actions bot commented Dec 8, 2021

🎉 This PR is included in version 12.1.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@rethab rethab deleted the fix/prod-json-logging branch December 9, 2021 08:05
@rethab
Copy link
Contributor Author

rethab commented Dec 9, 2021

Thanks @gr2m and @tcbyrd 🎉 ❤️

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

Successfully merging this pull request may close these issues.

Logging format based on NODE_ENV not working as documented
3 participants