Skip to content

Commit bf2225d

Browse files
committed
feat(timer): add warnFromError function
1 parent ac21b09 commit bf2225d

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/logger/logger.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ class Logger {
4949
}, data));
5050
}
5151

52+
warnFromError(action, error, data = {}) {
53+
this.warn(action, Object.assign({
54+
error_name: error.name,
55+
error_stack: this._shortenStackTrace(error),
56+
error_message: error.message
57+
}, data));
58+
}
59+
5260
timer() {
5361
return new Timer(this);
5462
}

src/logger/logger.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ describe('Logger', function() {
7777
expect(logArguments.error_message).to.eql(error.message);
7878
});
7979

80+
it('should log error as warning with action', function() {
81+
const error = new Error('failed');
82+
83+
logger.warnFromError('hi', error, { details: 'here' });
84+
85+
const logArguments = JSON.parse(console.log.args[0]);
86+
expect(logArguments.name).to.eql('mongo');
87+
expect(logArguments.action).to.eql('hi');
88+
expect(logArguments.level).to.eql(40);
89+
expect(logArguments.details).to.eql('here');
90+
91+
expect(logArguments.error_name).to.eql(error.name);
92+
expect(logArguments.error_stack).to.eql(error.stack);
93+
expect(logArguments.error_message).to.eql(error.message);
94+
});
95+
8096
describe('#configure', function() {
8197
it('should change format method', function() {
8298
const formattedOutput = '{"my":"method"}';

src/timer/timer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ class Timer {
2323
);
2424
}
2525

26+
warnFromError(action, error, data = {}) {
27+
this._logger.warnFromError(
28+
action,
29+
error,
30+
Object.assign({ duration: this._duration() }, data)
31+
);
32+
}
33+
2634
_duration() {
2735
const end = new Date().getTime();
2836

src/timer/timer.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,16 @@ describe('Timer', function() {
2626

2727
expect(errorStub).to.have.been. calledWith('time', error, { customer_id: 10, duration: 100 });
2828
});
29+
30+
it('should log elapsed time with error', function() {
31+
const logger = new Logger('test', false);
32+
const errorStub = this.sandbox.stub(logger, 'warnFromError');
33+
const timer = new Timer(logger);
34+
const error = new Error('intended');
35+
36+
this.clock.tick(100);
37+
timer.warnFromError('time', error, { customer_id: 10 });
38+
39+
expect(errorStub).to.have.been. calledWith('time', error, { customer_id: 10, duration: 100 });
40+
});
2941
});

0 commit comments

Comments
 (0)