Skip to content

Instantly share code, notes, and snippets.

@alfonmga
Created November 15, 2020 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfonmga/30eebf19090e5587c9c09f7732d9f524 to your computer and use it in GitHub Desktop.
Save alfonmga/30eebf19090e5587c9c09f7732d9f524 to your computer and use it in GitHub Desktop.
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