Created
November 15, 2020 17:31
-
-
Save alfonmga/30eebf19090e5587c9c09f7732d9f524 to your computer and use it in GitHub Desktop.
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
const { format } = require("date-fns"); | |
/** | |
* logger is a small tool to centralize the logging of the application | |
* [@params](/params) {String} src - this will be prefixed before each message. It can be any string | |
* that would be significant when you read them in the logs (often in this code base it's the name | |
* of the file from where the log is sent) | |
*/ | |
const logger = (src) => { | |
return { | |
/**simply displays the message in the console | |
* [@param](/param) {String} msg - the message to be displayed | |
* [@return](/return) - nothing | |
*/ | |
info: (msg) => { | |
const now = format(new Date(), "dd/MM/yyyy HH:mm"); | |
console.log(`${now} info - ${msg} (${src})`); | |
}, | |
/** displays the message in the console with a warning prefix | |
* [@param](/param) {String} msg - the message to be displayed | |
* [@return](/return) - nothing | |
*/ | |
warn: (msg) => { | |
const now = format(new Date(), "dd/MM/yyyy HH:mm"); | |
console.warn(`${now} warning - ${msg} (${src})`); | |
}, | |
/** displays the message in the console with an error prefix | |
* [@todo](/todo) send the error to slack in production environment | |
* [@param](/param) {String} msg - the message to be displayed | |
* [@return](/return) - nothing | |
*/ | |
err: (error) => { | |
const now = format(new Date(), "dd/MM/yyyy HH:mm"); | |
// in production, send to slack/email | |
console.error(`${now} ERROR - ${error.message || error} (${src})`); | |
}, | |
}; | |
}; | |
module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment