Skip to content
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
12 changes: 8 additions & 4 deletions src/logger/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Logger {
static _validate(options) {
Object.keys(options).forEach(key => {
if (!allowedKeys.includes(key)) {
throw new Error('Only the following keys are allowed: formatter, output')
throw new Error('Only the following keys are allowed: formatter, output');
}
});
}
Expand All @@ -31,12 +31,16 @@ class Logger {
return this._enabled;
}

customError(severity, action, error, data = {}) {
logMethodFactory(severity).bind(this)(action, Object.assign(this._getErrorDetails(error), data));
}

fromError(action, error, data = {}) {
this.error(action, Object.assign(this._getErrorDetails(error), data));
this.customError('error', action, error, data);
}

warnFromError(action, error, data = {}) {
this.warn(action, Object.assign(this._getErrorDetails(error), data));
this.customError('warn', action, error, data);
}

timer() {
Expand Down Expand Up @@ -67,7 +71,7 @@ class Logger {
error_stack: this._shortenStackTrace(error.stack),
error_message: error.message,
error_data: this._shortenData(error.data)
}
};
}
}

Expand Down
55 changes: 55 additions & 0 deletions src/logger/logger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,61 @@ describe('Logger', function() {
expect(logArguments.error_data.length).to.eql(3004);
});

describe('#customError', function() {
it('should log error as the given severity with action', function() {
const error = new Error('failed');
error.data = { test: 'data' };

logger.customError('info', 'hi', error, { details: 'here' });

const logArguments = JSON.parse(console.log.args[0]);
expect(logArguments.name).to.eql('mongo');
expect(logArguments.action).to.eql('hi');
expect(logArguments.level).to.eql(30);
expect(logArguments.details).to.eql('here');

expect(logArguments.error_name).to.eql(error.name);
expect(logArguments.error_stack).to.eql(error.stack);
expect(logArguments.error_message).to.eql(error.message);
expect(logArguments.error_data).to.eql(JSON.stringify(error.data));
});

it('should not log error data when it is undefined', function() {
const error = new Error('failed');

logger.customError('warn', 'hi', error, { details: 'here' });

const logArguments = JSON.parse(console.log.args[0]);
expect(logArguments.name).to.eql('mongo');
expect(logArguments.action).to.eql('hi');
expect(logArguments.level).to.eql(40);
expect(logArguments.details).to.eql('here');

expect(logArguments.error_name).to.eql(error.name);
expect(logArguments.error_stack).to.eql(error.stack);
expect(logArguments.error_message).to.eql(error.message);
expect(logArguments).to.not.have.any.keys('error_data');
});

it('should log only 3000 character of data', function() {
const error = new Error('failed');
error.data = 'exactlyTen'.repeat(400);

logger.customError('error', 'hi', error, { details: 'here' });

const logArguments = JSON.parse(console.log.args[0]);
expect(logArguments.name).to.eql('mongo');
expect(logArguments.action).to.eql('hi');
expect(logArguments.level).to.eql(50);
expect(logArguments.details).to.eql('here');

expect(logArguments.error_name).to.eql(error.name);
expect(logArguments.error_stack).to.eql(error.stack);
expect(logArguments.error_message).to.eql(error.message);
expect(logArguments.error_data.length).to.eql(3004);
});
});

describe('#configure', function() {
it('should change format method', function() {
const formattedOutput = '{"my":"method"}';
Expand Down