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

Add support for non-bunyan loggers #88

Merged
merged 3 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ This sensor is using the [bunyan](https://www.npmjs.com/package/bunyan) logging

```javascript
require('instana-nodejs-sensor')({
logger: A_BUNYAN_LOGGER
logger: A_LOGGER
});
```

Other logging modules are supported if they provide functions for the log levels `debug`, `info`, `warn` and `error`.

### Log Level Configuration
The Node.js sensor will now create children of this logger with the same log level and target streams. If you only want to change the default log level, you can configure it via:

Expand Down
16 changes: 15 additions & 1 deletion src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ var bunyanToAgentStream = require('./agent/bunyanToAgentStream');
var parentLogger;

exports.init = function(config) {
if (config.logger) {
if (config.logger && typeof config.logger.child === 'function') {
parentLogger = config.logger.child({module: 'instana-nodejs-logger-parent'});
} else if (config.logger && hasLoggingFunctions(config.logger)) {
parentLogger = config.logger;
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand your change correctly, your intention would be to use config.nonBunyanLogger to pass in a logger object. Any particular reason to change the above line, handling the Bunyan case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was changed accidentally. i removed this change.

return;
} else {
parentLogger = bunyan.createLogger({name: 'instana-nodejs-sensor'});
}
Expand All @@ -29,9 +32,20 @@ exports.getLogger = function(moduleName) {
exports.init({});
}

if (typeof parentLogger.child !== 'function') {
return parentLogger;
}

var logger = parentLogger.child({
module: moduleName
});

return logger;
};

function hasLoggingFunctions(logger) {
return typeof logger.debug === 'function' &&
typeof logger.info === 'function' &&
typeof logger.warn === 'function' &&
typeof logger.error === 'function';
}
23 changes: 23 additions & 0 deletions test/logger_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,27 @@ describe('logger', function() {

expect(logger.level()).to.equal(50);
});

it('should not accept non-bunyan loggers without necessary logging functions', function() {
var nonBunyanLogger = {};

log.init({ logger: nonBunyanLogger });

var logger = log.getLogger('myLogger');
expect(logger).to.be.an.instanceOf(bunyan);
});

it('should accept non-bunyan loggers with necessary logging functions', function() {
var nonBunyanLogger = {
debug: function () {},
info: function () {},
warn: function () {},
error: function () {}
};

log.init({ logger: nonBunyanLogger });

var logger = log.getLogger('myLogger');
expect(logger).not.to.be.an.instanceOf(bunyan);
});
});