-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(timer): add timer to logger, same api as logger
- Loading branch information
1 parent
41413cf
commit ba2ab12
Showing
7 changed files
with
93 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,40 @@ | ||
'use strict'; | ||
|
||
const logMethodFactory = function(level) { | ||
return function(action, data) { | ||
this._logger[level]( | ||
action, | ||
Object.assign({ duration: this._duration() }, data) | ||
); | ||
} | ||
}; | ||
|
||
class Timer { | ||
constructor() { | ||
this.start = new Date().getTime(); | ||
constructor(logger) { | ||
this._logger = logger; | ||
this._start = new Date().getTime(); | ||
} | ||
|
||
elapsedTime() { | ||
fromError(action, error, data = {}) { | ||
this._logger.fromError( | ||
action, | ||
error, | ||
Object.assign({ duration: this._duration() }, data) | ||
); | ||
} | ||
|
||
_duration() { | ||
const end = new Date().getTime(); | ||
|
||
return end - this.start; | ||
return end - this._start; | ||
} | ||
} | ||
|
||
Timer.prototype.trace = logMethodFactory('trace'); | ||
Timer.prototype.debug = logMethodFactory('debug'); | ||
Timer.prototype.info = logMethodFactory('info'); | ||
Timer.prototype.warn = logMethodFactory('warn'); | ||
Timer.prototype.error = logMethodFactory('error'); | ||
Timer.prototype.fatal = logMethodFactory('fatal'); | ||
|
||
module.exports = Timer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const Logger = require('../logger/logger'); | ||
const Timer = require('./timer'); | ||
|
||
describe('Timer', function() { | ||
it('should log elapsed time', function() { | ||
const logger = new Logger('test', false); | ||
const infoStub = this.sandbox.stub(logger, 'info'); | ||
const timer = new Timer(logger); | ||
|
||
this.clock.tick(100); | ||
timer.info('time', { customer_id: 10 }); | ||
|
||
expect(infoStub).to.have.been. calledWith('time', { customer_id: 10, duration: 100 }); | ||
}); | ||
|
||
it('should log elapsed time with error', function() { | ||
const logger = new Logger('test', false); | ||
const errorStub = this.sandbox.stub(logger, 'fromError'); | ||
const timer = new Timer(logger); | ||
const error = new Error('intended'); | ||
|
||
this.clock.tick(100); | ||
timer.fromError('time', error, { customer_id: 10 }); | ||
|
||
expect(errorStub).to.have.been. calledWith('time', error, { customer_id: 10, duration: 100 }); | ||
}); | ||
}); |