Skip to content

Commit f56038b

Browse files
authored
Merge pull request #1 from emartech/custom-error
feat(logger): add customError function
2 parents 7521bb9 + b53ede8 commit f56038b

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
@@ -31,12 +31,16 @@ class Logger {
3131
return this._enabled;
3232
}
3333

34+
customError(severity, action, error, data = {}) {
35+
logMethodFactory(severity).bind(this)(action, Object.assign(this._getErrorDetails(error), data));
36+
}
37+
3438
fromError(action, error, data = {}) {
35-
this.error(action, Object.assign(this._getErrorDetails(error), data));
39+
this.customError('error', action, error, data);
3640
}
3741

3842
warnFromError(action, error, data = {}) {
39-
this.warn(action, Object.assign(this._getErrorDetails(error), data));
43+
this.customError('warn', action, error, data);
4044
}
4145

4246
timer() {

src/logger/logger.spec.js

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

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

0 commit comments

Comments
 (0)