feat: debug logging and better timestamps
This commit is contained in:
@@ -1,5 +1,23 @@
|
|||||||
/* eslint no-console: 0 */
|
/* 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 = {
|
const logger = {
|
||||||
log(...args) {
|
log(...args) {
|
||||||
@@ -11,6 +29,10 @@ const logger = {
|
|||||||
error(...args) {
|
error(...args) {
|
||||||
console.error(wrapMessage(args));
|
console.error(wrapMessage(args));
|
||||||
},
|
},
|
||||||
|
debug(...args) {
|
||||||
|
if (!process.env.DEBUG) return;
|
||||||
|
console.log(wrapMessage(args));
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default logger;
|
export default logger;
|
||||||
|
|||||||
Reference in New Issue
Block a user