elastic / elastic/apm-agent-nodejs
Allow starting the agent before having to require and initialize custom logger
- Dominant language
- JavaScript
- Stars
- 594
- Forks
- 244
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 16
Description
Today if I want to add a custom logger (say pino) to the agent I have to do this:
```js
const pino = require('pino')()
require('elastic-apm-node').start({
logger: pino
})
```
Notice how I have to require the logger prior to starting the agent.
This might hurt our ability to instrument certain modules if the logger it self requires those. We were [close to implementing](https://github.com/elastic/apm-agent-nodejs/pull/299#discussion_r182014139) a feature that would have helped us in this scenario, but I forgot to think of this when we decided not to.
The solution [briefly discussed](https://github.com/elastic/apm-agent-nodejs/pull/299#discussion_r182014139) in #299, where the agent would initialize the logger for us, still suffers from the issue of not all loggers implementing the same initialization API. So I still don't think that's a good solution.
Instead I propose one of two solutions:
### Solution 1 - Callback
```js
require('elastic-apm-node').start({
logger: () => require('pino')()
})
```
**Pro:** Allow to get the logger in as early as possible, so it can be used during the initialization of the agent.
**Con:** Require the use of either an agent config file or calling start with config options, which makes it harder to use if you want to configure the agent using environment variables only.
### Solution 2 - Overwriting logger post start
```js
const agent = require('elastic-apm-node').start()
agent.setLogger(require('pino')())
```
**Pro:** Allow you to easily start the agent in any way you like
**Con:** The agent will not use the custom logger during initialization
Contributor guide
Assessment
This issue has not been assessed yet.