Created
March 27, 2020 09:52
-
-
Save audunolsen/c06b685851603bad1defe4af34e36746 to your computer and use it in GitHub Desktop.
A node logger script
This file contains hidden or 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
/* | |
SCRIPT WHICH LOGS MISC MESSAGES | |
Parameters: | |
--success || | |
--error || | |
--warning || | |
--info boolean Type of log entry, picks first argument found | |
--heading string a brief log heading | |
--description string an oprtional log description | |
*/ | |
process.env.FORCE_COLOR = true; | |
const | |
logger = require("./logger/logger"), | |
argv = require("yargs").argv, | |
levels = ["success", "warning", "error", "info"], | |
level = Object.keys(argv).filter(e => levels.includes(e))[0]; | |
const { heading, description } = argv; | |
if (!level && !heading) return; | |
logger[level](heading, description); |
This file contains hidden or 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 | |
chalk = require("chalk"), | |
length = require('string-length'), | |
time = () => chalk.bold(Date().slice(16,24)), | |
logger = {}; | |
const levels = { | |
success: { | |
color: "green", | |
glyph: "✓" | |
}, | |
error: { | |
color: "red", | |
glyph: "✗" | |
}, | |
warning: { | |
color: "yellow", | |
glyph: "!" | |
}, | |
info: { | |
color: "blue", | |
glyph: "○" | |
} | |
} | |
for (const [level, {color, glyph}] of Object.entries(levels)) { | |
logger[level] = (heading, description) => { | |
heading = ` ${time()} | ${chalk.bold[color](`${glyph} ${heading}`)}`; | |
console.log(heading); | |
const hr = " " + chalk.bold.gray("·").repeat(length(heading) - 1); | |
if (description) { | |
console.log(hr); | |
console.log(description, "\n") | |
} | |
return this; | |
} | |
} | |
logger.clear = () => { | |
console.log("\x1B[2J\x1B[s3J\x1B[H\x1Bc"); | |
/* Windows (?) bug. Can't chain other methods calls after clear. | |
For some reason the method chained after clear, is | |
ironically also cleard. Wierd!*/ | |
return this; | |
} | |
module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment