Skip to content

Commit

Permalink
chore: Refactored logger, removed picocolors
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrsskls committed Jul 17, 2022
1 parent 5c27c3a commit b554ce6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
74 changes: 50 additions & 24 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,62 @@
const colors = {
grey: "\x1b[90m",
red: "\x1b[31m",
yellow: "\x1b[33m",
green: "\x1b[32m",
cyan: "\x1b[36m",
white: "\x1b[37m",
reset: "\x1b[0m",
};

const types = {
error: { console: "error", label: "Error", color: "red" },
warn: { console: "log", label: "Warning", color: "yellow" },
success: { console: "log", label: "Success", color: "green" },
info: { console: "info", label: "Info", color: "cyan" },
};

/**
* Module for pretty cli logs
*
* @module logger
* @param {string} [type] - Can be any of "error", "warn" or "success"
* @param {string} [type]
* @param {string} message
* @returns {string}
*/

const colors = require("picocolors");

module.exports = function log(type, message) {
if (process.env.MIYAGI_JS_API) return;

const date = new Date();
const dateStr = `${date.getFullYear()}/${date
.getMonth()
.toString()
.padStart(2, "0")}/${date
.getDate()
.toString()
.padStart(2, "0")} ${date.getHours()}:${date.getMinutes()}`;
const year = date.getFullYear();
const month = pad(date.getMonth());
const day = pad(date.getDate());
const hours = pad(date.getHours());
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());

const dateStr = `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;

if (type === "error") {
console.error(`${colors.gray(dateStr)} ${colors.red("Error:")} ${message}`);
} else if (type === "warn") {
console.info(
`${colors.gray(dateStr)} ${colors.yellow("Warning:")} ${message}`
);
} else if (type === "success") {
console.info(
`${colors.gray(dateStr)} ${colors.green("Success:")} ${message}`
);
} else {
console.info(`${colors.gray(dateStr)} ${colors.cyan("Info:")} ${message}`);
}
return console[types[type].console](
`${colorize("grey", dateStr)} ${colorize(
types[type].color,
`${types[type].label}:`
)} ${colors.reset}${message}`
);
};

/**
* @param {string} color
* @param {string} str
* @returns {string}
*/
function colorize(color, str) {
return `${colors[color]}${str}`;
}

/**
* @param {Date} value
* @returns {string}
*/
function pad(value) {
return value.toString().padStart(2, "0");
}
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"jsdom": "^20.0.0",
"marked": "^4.0.17",
"node-watch": "^0.7.3",
"picocolors": "^1.0.0",
"socket.io": "^4.5.1",
"twig": "^1.15.4",
"yargs": "^17.5.1"
Expand Down

0 comments on commit b554ce6

Please sign in to comment.