Last active
January 3, 2016 09:09
-
-
Save wookets/8441285 to your computer and use it in GitHub Desktop.
A running logger for node.js. This logger will record events
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
logger = require 'logger' | |
log = logger.create() | |
log('START :: Starting a fun job!') | |
log('Some event happened and I have attached data.', data) | |
logs = log('END :: A fun job is over!') | |
console.log(logs) | |
# | |
# output | |
# | |
output = | |
start: date | |
end: date | |
duration: 'human readable string' | |
events: [ | |
{msg: 'the message you wrote', lapse: 'a human readable string of how much time has elapsed from previous event', data: 'whatever obejct you passed in'} | |
] |
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
exports.create = () -> | |
log = | |
start: new Date | |
events: [] | |
return (msg, data) -> | |
event = {} | |
event.msg = msg | |
event.lapse = formatDuration(new Date - log.start) | |
event.data = data if data | |
log.events.push(event) | |
log.end = new Date | |
log.duration = formatDuration(log.end - log.start) | |
return log | |
# | |
# pass in duration in milliseconds and get back a human readible string | |
# | |
formatDuration = (duration) -> | |
if duration < 1000 | |
duration += 'ms' | |
else if duration > 1000 | |
duration /= 1000 | |
duration += 's' | |
return duration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment