diff --git a/lib/logger.js b/lib/logger.js index 822c39d..4e21f8b 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -147,9 +147,17 @@ function Logger(key, options) { Logger.prototype.log = function(statement, opts) { this._err = false; if (typeof statement === 'object') { + if (this.ambientMeta) { + statement.ambientMeta = this.ambientMeta; + } statement = JSON.parse(JSON.stringify(statement)); statement = stringify(statement, null, 2, function() { return undefined; }); } + + if (typeof statement !== 'object' && this.ambientMeta) { + statement = `${JSON.stringify({message: statement, ambient_meta: this.ambientMeta})}`; + } + var message = { timestamp: Date.now() , line: statement @@ -157,6 +165,7 @@ Logger.prototype.log = function(statement, opts) { , app: this.source.app , env: this.source.env }; + if (opts) { if (typeof opts === 'string') { if (opts.length > configs.MAX_INPUT_LENGTH) { @@ -227,6 +236,14 @@ Logger.prototype._bufferLog = function(message) { } }; +Logger.prototype.addProperty = function (meta) { + this.ambientMeta = meta; +}; + +Logger.prototype.removeProperty = function () { + this.ambientMeta = null; +}; + Logger.prototype._flush = function(callback) { if (!callback || typeof callback !== 'function') { throw new Error('flush function expects a callback'); @@ -308,6 +325,7 @@ const flushAll = function(cb) { } }); }); + if (errors.length > 0) { return cb(`The following errors happened while flushing all loggers: ${errors}`); } diff --git a/test/logger.js b/test/logger.js index 0dd3b22..b29a038 100644 --- a/test/logger.js +++ b/test/logger.js @@ -374,3 +374,44 @@ describe('Multiple loggers', function() { }, configs.FLUSH_INTERVAL + 200); }); }); + +describe('ambient meta', function() { + const ambientLogger = Logger.createLogger(testHelper.apikey, testHelper.options); + + beforeEach(function() { + Logger.flushAll(); + }); + + it('add string ambinet meta to a string log line', function() { + ambientLogger.addProperty('someAmbientMeta'); + ambientLogger.log('Sent a string log'); + ambientLogger.log('Sent a second string log'); + + assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambient_meta":"someAmbientMeta"}'); + assert(ambientLogger._buf[1].line === '{"message":"Sent a second string log","ambient_meta":"someAmbientMeta"}'); + }); + it('add an object ambinet meta to a string log line', function() { + ambientLogger.addProperty({someAmbientKey: 'value'}); + + ambientLogger.log('Sent a string log'); + ambientLogger.log('Sent a second string log'); + + assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambient_meta":{"someAmbientKey":"value"}}'); + assert(ambientLogger._buf[1].line === '{"message":"Sent a second string log","ambient_meta":{"someAmbientKey":"value"}}'); + }); + it('add an object ambinet meta to an object log line', function() { + ambientLogger.addProperty({someAmbientKey: 'value'}); + + ambientLogger.log({k: 'v'}); + + assert(ambientLogger._buf[0].line === '{"message":"{\\n \\"k\\": \\"v\\",\\n \\"ambientMeta\\": {\\n \\"someAmbientKey\\": \\"value\\"\\n }\\n}","ambient_meta":{"someAmbientKey":"value"}}'); + }); + it('remove ambient meta', function() { + ambientLogger.addProperty('someAmbientMeta'); + ambientLogger.log('Sent a string log'); + ambientLogger.removeProperty(); + ambientLogger.log('Sent a string log'); + assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambient_meta":"someAmbientMeta"}'); + assert(ambientLogger._buf[1].line === 'Sent a string log'); + }); +});