Skip to content
This repository was archived by the owner on Apr 21, 2022. It is now read-only.
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
18 changes: 18 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,25 @@ 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
, level: this.source.level
, app: this.source.app
, env: this.source.env
};

if (opts) {
if (typeof opts === 'string') {
if (opts.length > configs.MAX_INPUT_LENGTH) {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -308,6 +325,7 @@ const flushAll = function(cb) {
}
});
});

if (errors.length > 0) {
return cb(`The following errors happened while flushing all loggers: ${errors}`);
}
Expand Down
41 changes: 41 additions & 0 deletions test/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});