From f7c845a0b473d49b89e27e3d4a14d0fa9956b0fc Mon Sep 17 00:00:00 2001 From: vilyapilya Date: Fri, 12 Jul 2019 17:33:19 -0700 Subject: [PATCH 1/8] add functions for adding and removng ambient meta --- lib/logger.js | 23 ++++++++++++++++++++++- test/logger.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/logger.js b/lib/logger.js index 822c39d..a5207ac 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -109,6 +109,10 @@ function Logger(key, options) { this._flushInterval = options.FLUSH_INTERVAL; } + if (options.custom && (typeof options.custom === 'string' || typeof options.custom === 'number')) { + this.custom = options.custom; + } + this._max_length = options.max_length || true; this._index_meta = options.index_meta || false; this._url = options.logdna_url || configs.LOGDNA_URL; @@ -144,12 +148,20 @@ function Logger(key, options) { loggers.push(this); } -Logger.prototype.log = function(statement, opts) { +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, ambientMeta: this.ambientMeta})}`; + } + var message = { timestamp: Date.now() , line: statement @@ -157,6 +169,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 +240,14 @@ Logger.prototype._bufferLog = function(message) { } }; +Logger.prototype.addAmbientMeta = function (meta) { + this.ambientMeta = meta; +}; + +Logger.prototype.removeAmbientMeta = function () { + this.ambientMeta = null; +}; + Logger.prototype._flush = function(callback) { if (!callback || typeof callback !== 'function') { throw new Error('flush function expects a callback'); diff --git a/test/logger.js b/test/logger.js index 0dd3b22..5a7c793 100644 --- a/test/logger.js +++ b/test/logger.js @@ -374,3 +374,40 @@ describe('Multiple loggers', function() { }, configs.FLUSH_INTERVAL + 200); }); }); + +describe('ambient meta', function() { + const ambientLogger = Logger.createLogger(testHelper.apikey, testHelper.options); + + it('add string ambinet meta to a string log line', function() { + ambientLogger.addAmbientMeta('someAmbientMeta'); + ambientLogger.log('Sent a string log'); + ambientLogger.log('Sent a second string log'); + + assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambientMeta":"someAmbientMeta"}'); + assert(ambientLogger._buf[1].line === '{"message":"Sent a second string log","ambientMeta":"someAmbientMeta"}'); + }); + it('add an object ambinet meta to a string log line', function() { + ambientLogger.addAmbientMeta({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","ambientMeta":{"someAmbientKey":"value"}}'); + assert(ambientLogger._buf[1].line === '{"message":"Sent a second string log","ambientMeta":{"someAmbientKey":"value"}}'); + }); + it('add an object ambinet meta to an object log line', function() { + ambientLogger.addAmbientMeta({someAmbientKey: 'value'}); + + ambientLogger.log({k:'v'}); + + assert(ambientLogger._buf[0].line === '{"message":"{\\n \\"k\\": \\"v\\",\\n \\"ambientMeta\\": {\\n \\"someAmbientKey\\": \\"value\\"\\n }\\n}","ambientMeta":{"someAmbientKey":"value"}}'); + }); + it('remove ambient meta', function() { + ambientLogger.addAmbientMeta('someAmbientMeta'); + ambientLogger.log('Sent a string log'); + ambientLogger.removeAmbientMeta(); + ambientLogger.log('Sent a string log'); + assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambientMeta":"someAmbientMeta"}'); + assert(ambientLogger._buf[1].line === 'Sent a string log'); + }); +}); From 8be7db637020d6c5cbf3533d05e9305d4ba49b6c Mon Sep 17 00:00:00 2001 From: vilyapilya Date: Fri, 12 Jul 2019 17:35:18 -0700 Subject: [PATCH 2/8] lint --- lib/logger.js | 8 ++++---- test/logger.js | 56 +++++++++++++++++++++++++------------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/lib/logger.js b/lib/logger.js index a5207ac..9b2518a 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -152,14 +152,14 @@ Logger.prototype.log = function (statement, opts) { this._err = false; if (typeof statement === 'object') { if (this.ambientMeta) { - statement['ambientMeta'] = 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, ambientMeta: this.ambientMeta})}`; + if (typeof statement !== 'object' && this.ambientMeta) { + statement = `${JSON.stringify({message: statement, ambientMeta: this.ambientMeta})}`; } var message = { diff --git a/test/logger.js b/test/logger.js index 5a7c793..1651af5 100644 --- a/test/logger.js +++ b/test/logger.js @@ -376,38 +376,38 @@ describe('Multiple loggers', function() { }); describe('ambient meta', function() { - const ambientLogger = Logger.createLogger(testHelper.apikey, testHelper.options); + const ambientLogger = Logger.createLogger(testHelper.apikey, testHelper.options); - it('add string ambinet meta to a string log line', function() { - ambientLogger.addAmbientMeta('someAmbientMeta'); - ambientLogger.log('Sent a string log'); - ambientLogger.log('Sent a second string log'); + it('add string ambinet meta to a string log line', function() { + ambientLogger.addAmbientMeta('someAmbientMeta'); + ambientLogger.log('Sent a string log'); + ambientLogger.log('Sent a second string log'); - assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambientMeta":"someAmbientMeta"}'); - assert(ambientLogger._buf[1].line === '{"message":"Sent a second string log","ambientMeta":"someAmbientMeta"}'); - }); - it('add an object ambinet meta to a string log line', function() { - ambientLogger.addAmbientMeta({someAmbientKey: 'value'}); + assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambientMeta":"someAmbientMeta"}'); + assert(ambientLogger._buf[1].line === '{"message":"Sent a second string log","ambientMeta":"someAmbientMeta"}'); + }); + it('add an object ambinet meta to a string log line', function() { + ambientLogger.addAmbientMeta({someAmbientKey: 'value'}); - ambientLogger.log('Sent a string log'); - ambientLogger.log('Sent a second string log'); + ambientLogger.log('Sent a string log'); + ambientLogger.log('Sent a second string log'); - assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambientMeta":{"someAmbientKey":"value"}}'); - assert(ambientLogger._buf[1].line === '{"message":"Sent a second string log","ambientMeta":{"someAmbientKey":"value"}}'); - }); - it('add an object ambinet meta to an object log line', function() { - ambientLogger.addAmbientMeta({someAmbientKey: 'value'}); + assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambientMeta":{"someAmbientKey":"value"}}'); + assert(ambientLogger._buf[1].line === '{"message":"Sent a second string log","ambientMeta":{"someAmbientKey":"value"}}'); + }); + it('add an object ambinet meta to an object log line', function() { + ambientLogger.addAmbientMeta({someAmbientKey: 'value'}); - ambientLogger.log({k:'v'}); + ambientLogger.log({k: 'v'}); - assert(ambientLogger._buf[0].line === '{"message":"{\\n \\"k\\": \\"v\\",\\n \\"ambientMeta\\": {\\n \\"someAmbientKey\\": \\"value\\"\\n }\\n}","ambientMeta":{"someAmbientKey":"value"}}'); - }); - it('remove ambient meta', function() { - ambientLogger.addAmbientMeta('someAmbientMeta'); - ambientLogger.log('Sent a string log'); - ambientLogger.removeAmbientMeta(); - ambientLogger.log('Sent a string log'); - assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambientMeta":"someAmbientMeta"}'); - assert(ambientLogger._buf[1].line === 'Sent a string log'); - }); + assert(ambientLogger._buf[0].line === '{"message":"{\\n \\"k\\": \\"v\\",\\n \\"ambientMeta\\": {\\n \\"someAmbientKey\\": \\"value\\"\\n }\\n}","ambientMeta":{"someAmbientKey":"value"}}'); + }); + it('remove ambient meta', function() { + ambientLogger.addAmbientMeta('someAmbientMeta'); + ambientLogger.log('Sent a string log'); + ambientLogger.removeAmbientMeta(); + ambientLogger.log('Sent a string log'); + assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambientMeta":"someAmbientMeta"}'); + assert(ambientLogger._buf[1].line === 'Sent a string log'); + }); }); From 80a9c93ac2c3d9e36b969c83c0cc6de15ea26c76 Mon Sep 17 00:00:00 2001 From: vilyapilya Date: Fri, 12 Jul 2019 17:35:51 -0700 Subject: [PATCH 3/8] remove the space --- lib/logger.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logger.js b/lib/logger.js index 9b2518a..7974e60 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -148,7 +148,7 @@ function Logger(key, options) { loggers.push(this); } -Logger.prototype.log = function (statement, opts) { +Logger.prototype.log = function(statement, opts) { this._err = false; if (typeof statement === 'object') { if (this.ambientMeta) { From da318d96a8a5fca0ff5ec5bddde68c48a9a5a0d2 Mon Sep 17 00:00:00 2001 From: vilyapilya Date: Fri, 12 Jul 2019 18:00:35 -0700 Subject: [PATCH 4/8] fix the test --- test/logger.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/logger.js b/test/logger.js index 1651af5..3eb64ab 100644 --- a/test/logger.js +++ b/test/logger.js @@ -378,6 +378,10 @@ describe('Multiple loggers', function() { 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.addAmbientMeta('someAmbientMeta'); ambientLogger.log('Sent a string log'); @@ -399,7 +403,6 @@ describe('ambient meta', function() { ambientLogger.addAmbientMeta({someAmbientKey: 'value'}); ambientLogger.log({k: 'v'}); - assert(ambientLogger._buf[0].line === '{"message":"{\\n \\"k\\": \\"v\\",\\n \\"ambientMeta\\": {\\n \\"someAmbientKey\\": \\"value\\"\\n }\\n}","ambientMeta":{"someAmbientKey":"value"}}'); }); it('remove ambient meta', function() { From e00e1358f5614afa9188ee903ac8444be3583bac Mon Sep 17 00:00:00 2001 From: vilyapilya Date: Mon, 15 Jul 2019 12:41:34 -0700 Subject: [PATCH 5/8] lint --- test/logger.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/logger.js b/test/logger.js index 3eb64ab..bb86308 100644 --- a/test/logger.js +++ b/test/logger.js @@ -379,8 +379,8 @@ describe('ambient meta', function() { const ambientLogger = Logger.createLogger(testHelper.apikey, testHelper.options); beforeEach(function() { - Logger.flushAll(); - }) + Logger.flushAll(); + }); it('add string ambinet meta to a string log line', function() { ambientLogger.addAmbientMeta('someAmbientMeta'); From eed0e5870adc468d8fbd5c1bd4dccc4f8458d6e3 Mon Sep 17 00:00:00 2001 From: vilyapilya Date: Mon, 15 Jul 2019 12:49:18 -0700 Subject: [PATCH 6/8] remove not needed changes --- lib/logger.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/logger.js b/lib/logger.js index 7974e60..77b1a5a 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -109,10 +109,6 @@ function Logger(key, options) { this._flushInterval = options.FLUSH_INTERVAL; } - if (options.custom && (typeof options.custom === 'string' || typeof options.custom === 'number')) { - this.custom = options.custom; - } - this._max_length = options.max_length || true; this._index_meta = options.index_meta || false; this._url = options.logdna_url || configs.LOGDNA_URL; From 320aea86d8829eab31e0adee7f25bc2c0c528d54 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 6 Aug 2019 11:29:22 -0700 Subject: [PATCH 7/8] renmae variable and method names --- lib/logger.js | 6 +++--- test/logger.js | 23 ++++++++++++----------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/logger.js b/lib/logger.js index 77b1a5a..46c4436 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -155,7 +155,7 @@ Logger.prototype.log = function(statement, opts) { } if (typeof statement !== 'object' && this.ambientMeta) { - statement = `${JSON.stringify({message: statement, ambientMeta: this.ambientMeta})}`; + statement = `${JSON.stringify({message: statement, ambient_meta: this.ambientMeta})}`; } var message = { @@ -236,11 +236,11 @@ Logger.prototype._bufferLog = function(message) { } }; -Logger.prototype.addAmbientMeta = function (meta) { +Logger.prototype.addProperty = function (meta) { this.ambientMeta = meta; }; -Logger.prototype.removeAmbientMeta = function () { +Logger.prototype.removeProperty = function () { this.ambientMeta = null; }; diff --git a/test/logger.js b/test/logger.js index bb86308..b29a038 100644 --- a/test/logger.js +++ b/test/logger.js @@ -383,34 +383,35 @@ describe('ambient meta', function() { }); it('add string ambinet meta to a string log line', function() { - ambientLogger.addAmbientMeta('someAmbientMeta'); + 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","ambientMeta":"someAmbientMeta"}'); - assert(ambientLogger._buf[1].line === '{"message":"Sent a second string log","ambientMeta":"someAmbientMeta"}'); + 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.addAmbientMeta({someAmbientKey: 'value'}); + 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","ambientMeta":{"someAmbientKey":"value"}}'); - assert(ambientLogger._buf[1].line === '{"message":"Sent a second string log","ambientMeta":{"someAmbientKey":"value"}}'); + 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.addAmbientMeta({someAmbientKey: 'value'}); + ambientLogger.addProperty({someAmbientKey: 'value'}); ambientLogger.log({k: 'v'}); - assert(ambientLogger._buf[0].line === '{"message":"{\\n \\"k\\": \\"v\\",\\n \\"ambientMeta\\": {\\n \\"someAmbientKey\\": \\"value\\"\\n }\\n}","ambientMeta":{"someAmbientKey":"value"}}'); + + 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.addAmbientMeta('someAmbientMeta'); + ambientLogger.addProperty('someAmbientMeta'); ambientLogger.log('Sent a string log'); - ambientLogger.removeAmbientMeta(); + ambientLogger.removeProperty(); ambientLogger.log('Sent a string log'); - assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambientMeta":"someAmbientMeta"}'); + assert(ambientLogger._buf[0].line === '{"message":"Sent a string log","ambient_meta":"someAmbientMeta"}'); assert(ambientLogger._buf[1].line === 'Sent a string log'); }); }); From 3c3c336b345f7bfa15be89d122979a8af381d3d3 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 6 Aug 2019 11:46:42 -0700 Subject: [PATCH 8/8] lints --- lib/logger.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/logger.js b/lib/logger.js index 46c4436..4e21f8b 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -325,6 +325,7 @@ const flushAll = function(cb) { } }); }); + if (errors.length > 0) { return cb(`The following errors happened while flushing all loggers: ${errors}`); }