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

Best practices for scoped logging #929

Open
zeke opened this issue May 3, 2019 · 10 comments

Comments

@zeke
Copy link
Member

commented May 3, 2019

Hi Probot friends!

I'm working on a bot that reacts to pull request events. For debugging purposes, I'd like to be able to tail Probot's logs and grep for log lines that pertain to just that PR:

heroku logs -t | grep pr-123

At present, I'm thinking of creating a custom logging function in the scope of the event handler:

async function handler (context) {
  function log (args) {
    const prefix = `pr-${context.payload.pull_request.number}`
    context.log(prefix, ...args)
  }

  log('some', objects, and, stuff)
})

Is there a better built-in way to includ grep-friendly context when logging?

cc @sarahs

@issue-label-bot

This comment has been minimized.

Copy link

commented May 3, 2019

Issue-Label Bot is automatically applying the label question ❓ to this issue, with a confidence of 0.83. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@JasonEtco

This comment has been minimized.

Copy link
Member

commented May 3, 2019

Hiya @zeke 👋 You could also use log.child (docs):

const logger = context.log.child({ pr: context.payload.pull_request.number })
logger.info('hello!')
// -> Something like:
// INFO http: POST / 200 - 111.46 ms (pr=1)
@zeke

This comment has been minimized.

Copy link
Member Author

commented May 4, 2019

Oh that looks perfect, @JasonEtco. Thanks!

@zeke zeke closed this May 4, 2019

@zeke

This comment has been minimized.

Copy link
Member Author

commented May 4, 2019

Oops spoke too soon.

TypeError: context.log.child is not a function

@zeke zeke reopened this May 4, 2019

@JasonEtco

This comment has been minimized.

Copy link
Member

commented May 4, 2019

Thaaaat's weird. I know for a 1000% fact that, at least on Probot 7.x.x, this works:

const { logger } = require('probot/lib/logger')
const myLogger = logger.child({ foo: true })
@zeke

This comment has been minimized.

Copy link
Member Author

commented May 4, 2019

Looks like it was implemented in #355 and landed in v6.0.0: https://github.com/probot/probot/releases/tag/v6.0.0

But yeah, it doesn't appear to work in probot 7 or 9 for me. I will give probot/lib/logger a try.

GitHub
🤖 A framework for building GitHub Apps to automate and improve your workflow - probot/probot
@stale

This comment has been minimized.

Copy link

commented Jul 3, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 3, 2019

@zeke

This comment has been minimized.

Copy link
Member Author

commented Jul 3, 2019

🤷‍♂ I never got scoped logging working.

@stale stale bot removed the wontfix label Jul 3, 2019

@lswith

This comment has been minimized.

Copy link

commented Jul 12, 2019

I'd like scoped logging as well

@stale

This comment has been minimized.

Copy link

commented Sep 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Sep 10, 2019

@gr2m gr2m added the pinned label Sep 10, 2019

@stale stale bot removed the wontfix label Sep 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.