Skip to content

Commit 7b543d7

Browse files
committed
feat(logger): add customError function
resolve EDITOR-1577
1 parent 416086e commit 7b543d7

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/logger/logger.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Logger {
2222
static _validate(options) {
2323
Object.keys(options).forEach(key => {
2424
if (!allowedKeys.includes(key)) {
25-
throw new Error('Only the following keys are allowed: formatter, output')
25+
throw new Error('Only the following keys are allowed: formatter, output');
2626
}
2727
});
2828
}
@@ -39,6 +39,10 @@ class Logger {
3939
this.warn(action, Object.assign(this._getErrorDetails(error), data));
4040
}
4141

42+
customError(severity, action, error, data = {}) {
43+
logMethodFactory(severity).bind(this)(action, Object.assign(this._getErrorDetails(error), data));
44+
}
45+
4246
timer() {
4347
return new Timer(this);
4448
}
@@ -67,7 +71,7 @@ class Logger {
6771
error_stack: this._shortenStackTrace(error.stack),
6872
error_message: error.message,
6973
error_data: this._shortenData(error.data)
70-
}
74+
};
7175
}
7276
}
7377

src/logger/logger.spec.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,61 @@ describe('Logger', function() {
120120
expect(logArguments.error_data.length).to.eql(3004);
121121
});
122122

123+
describe('#customError', function() {
124+
it('should log error as the given severity with action', function() {
125+
const error = new Error('failed');
126+
error.data = { test: 'data' };
127+
128+
logger.customError('info', 'hi', error, { details: 'here' });
129+
130+
const logArguments = JSON.parse(console.log.args[0]);
131+
expect(logArguments.name).to.eql('mongo');
132+
expect(logArguments.action).to.eql('hi');
133+
expect(logArguments.level).to.eql(30);
134+
expect(logArguments.details).to.eql('here');
135+
136+
expect(logArguments.error_name).to.eql(error.name);
137+
expect(logArguments.error_stack).to.eql(error.stack);
138+
expect(logArguments.error_message).to.eql(error.message);
139+
expect(logArguments.error_data).to.eql(JSON.stringify(error.data));
140+
});
141+
142+
it('should not log error data when it is undefined', function() {
143+
const error = new Error('failed');
144+
145+
logger.customError('warn', 'hi', error, { details: 'here' });
146+
147+
const logArguments = JSON.parse(console.log.args[0]);
148+
expect(logArguments.name).to.eql('mongo');
149+
expect(logArguments.action).to.eql('hi');
150+
expect(logArguments.level).to.eql(40);
151+
expect(logArguments.details).to.eql('here');
152+
153+
expect(logArguments.error_name).to.eql(error.name);
154+
expect(logArguments.error_stack).to.eql(error.stack);
155+
expect(logArguments.error_message).to.eql(error.message);
156+
expect(logArguments).to.not.have.any.keys('error_data');
157+
});
158+
159+
it('should log only 3000 character of data', function() {
160+
const error = new Error('failed');
161+
error.data = 'exactlyTen'.repeat(400);
162+
163+
logger.customError('error', 'hi', error, { details: 'here' });
164+
165+
const logArguments = JSON.parse(console.log.args[0]);
166+
expect(logArguments.name).to.eql('mongo');
167+
expect(logArguments.action).to.eql('hi');
168+
expect(logArguments.level).to.eql(50);
169+
expect(logArguments.details).to.eql('here');
170+
171+
expect(logArguments.error_name).to.eql(error.name);
172+
expect(logArguments.error_stack).to.eql(error.stack);
173+
expect(logArguments.error_message).to.eql(error.message);
174+
expect(logArguments.error_data.length).to.eql(3004);
175+
});
176+
});
177+
123178
describe('#configure', function() {
124179
it('should change format method', function() {
125180
const formattedOutput = '{"my":"method"}';

0 commit comments

Comments
 (0)