diff --git a/CONFIGURATION.md b/CONFIGURATION.md index f5266801ce..d5c7cf7a4a 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -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: diff --git a/src/logger.js b/src/logger.js index a222147baf..9cb87b7db9 100644 --- a/src/logger.js +++ b/src/logger.js @@ -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; + return; } else { parentLogger = bunyan.createLogger({name: 'instana-nodejs-sensor'}); } @@ -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'; +} diff --git a/test/logger_test.js b/test/logger_test.js index efa24ac65e..bacfd933c5 100644 --- a/test/logger_test.js +++ b/test/logger_test.js @@ -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); + }); });