-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactored logger, removed picocolors
- Loading branch information
Showing
3 changed files
with
54 additions
and
28 deletions.
There are no files selected for viewing
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
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"); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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