feat: debug logging and better timestamps

This commit is contained in:
2018-10-23 10:22:28 -07:00
parent 62c6216878
commit 08799b48d7

View File

@@ -1,5 +1,23 @@
/* eslint no-console: 0 */
const wrapMessage = msg => `${new Date()}: ${msg.join(' ')}`;
const zeroPad = (str, len) => {
let output = `${str}`;
if (!len || len <= output.length) return str;
while (output.length < len) {
output = `0${output}`;
}
return output;
};
const wrapMessage = msg => {
const d = new Date();
const timestamp = `${[d.getFullYear(), zeroPad(d.getMonth(), 2), zeroPad(d.getDate(), 2)].join(
'/'
)} ${[zeroPad(d.getHours(), 2), zeroPad(d.getMinutes(), 2), zeroPad(d.getSeconds(), 2)].join(
':'
)}`;
return `[${timestamp}]: ${msg.join(' ')}`;
};
const logger = {
log(...args) {
@@ -11,6 +29,10 @@ const logger = {
error(...args) {
console.error(wrapMessage(args));
},
debug(...args) {
if (!process.env.DEBUG) return;
console.log(wrapMessage(args));
},
};
export default logger;