Create Init function to override global logging
See original GitHub issueFeature 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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
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 whatprobot 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 aslog
option to theServer
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-middlewareYour code would look something like this:
Does that work for you?
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.