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.

Create Init function to override global logging

See original GitHub issue

Feature Request

Is your feature request related to a problem? Please describe.

This is related to https://github.com/probot/probot/issues/929 and https://github.com/probot/probot/issues/764. Right now it is impossible to consistently modify the logger that is used in context, or to fully replace the logger streams. https://github.com/icco/validator is where I discovered this, but the following is an example of the problem:

const { createStream } = require('bunyan-gke-stackdriver')

module.exports = async (app) => {
  app.log.target.addStream(createStream())

  const router = app.route('/')
  router.get('/healthz', (req, res) => {
    res.send('hi.')
  })

  app.on(['check_suite.requested', 'check_run.rerequested'], check)
  async function check (context) {
    const data = context.payload.check_suite
    context.log(data, "got a check")
  }
}

This code will log all http requests twice (which is expected, once for each stream). This code will only log “got a check” once though. It will only go on to the original stream, not the added stream. This happens because createStream() adds a new stream to stdout with a different format (one that google cloud understands).

Describe the solution you’d like

I would like there to be an init function that lets me completely replace the logging used by all subsystems. Something like

app.init(() => {
  app.setLogger(brandNewLoggerGenerator())
})

this new logger would be used for http requests, app.log calls, and context.log calls. This is important so I can remove the duplicate log lines and get consistent log messages from all places I can call log.

Describe alternatives you’ve considered

I’ve tried using addStream as recommended, but since I can’t remove the default stream, I get too many log lines. I’ve tried hacky ways to force replace the log in the app, but it doesn’t affect the context.

I’m suggesting the init idea because it seems reasonable that people would have other things they want to configure before the framework is fully in place. I’m not sure what, but seems like a common problem?

Teachability, Documentation, Adoption, Migration Strategy

Not sure what to put here, but glad to write docs or whatever.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gr2mcommented, Dec 8, 2020

We no longer use bunyan, but pino. But I think the relevant APIs are compatible, so you should be able to swap out the one with the other.

As part of the recent work on Probot, we tried to make it easier to decompose so that you can eject it if your requirements grow outside of the scope of Probot.

In your case, you could customize the way your app is started by using the Server class directly. Here is what probot run does today: https://github.com/probot/probot/blob/a028c3d7933ff7bcd2be3a04e97b8caf1d170a4f/src/run.ts - you’d basically implement a custom version of it where you pass in your logger as log option to the Server constructor.

Alternatively you can use the createNodeMiddleware API, which works nicely with express or most serverless deployments: https://probot.github.io/docs/serverless-deployment/#nodejsexpress-middleware

Your code would look something like this:

const { createMyMiddleware, Probot } = require('probot')
const myApp = require('./app')

module.exports = createMyMiddleware(myApp, {
  probot: new Probot({
    appId: process.env.APP_ID,
    privateKey: process.env.PRIVATE_KEY,
    secret: process.env.WEBHOOK_SECRET,
    log: myCustomLogger
  }),
});

Does that work for you?

<div> GitHub</div><div>probot/probot</div><div>🤖 A framework for building GitHub Apps to automate and improve your workflow - probot/probot</div>
<div> Probot</div><div>Serverless deployment</div><div>GitHub Apps to automate and improve your workflow</div>
0reactions
stale[bot]commented, May 10, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to override method of the logging module - Stack Overflow
You can firstly implement your own logger class by deriving from logging.Logger, in which you override the methods you target. And then you...
Read more >
Understanding init in Go | DigitalOcean
In Go, the predefined init() function sets off a piece of code to run before any other part of your package. This code...
Read more >
Cluster node initialization scripts | Databricks on AWS
Databricks audit logs capture global init script create, edit, and delete events under the event type globalInitScripts . See Configure audit ...
Read more >
logging.config — Logging configuration — Python 3.11.1 ...
Describing a logging configuration requires listing the various objects to create and the connections between them; for example, you may create a handler...
Read more >
Logging in .NET Core and ASP.NET Core | Microsoft Learn
LogLevel override settings in Logging.LogLevel , where the {PROVIDER NAME} placeholder is the provider name. For example, the level in Debug ...
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