Skip to content

Instantly share code, notes, and snippets.

@melbourne2991
Last active October 20, 2020 00:11
Show Gist options
  • Save melbourne2991/8cb8f4f26f89f33c7e66b3c2ec64e121 to your computer and use it in GitHub Desktop.
Save melbourne2991/8cb8f4f26f89f33c7e66b3c2ec64e121 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const readline = require('readline')
const colors = require('colors/safe')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
})
const logMap = {
60: {
name: 'Fatal',
colors: ['bgRed']
},
50: {
name: 'Error',
colors: ['bgRed']
},
40: {
name: 'Warn',
colors: ['bgYellow', 'black']
},
30: {
name: 'Info',
colors: ['bgBlue']
},
20: {
name: 'Debug',
colors: ['bgGray']
},
10: {
name: 'Trace',
colors: ['bgGray']
}
}
rl.on('line', function (line) {
try {
const parsed = JSON.parse(line)
const levelMapped = logMap[parsed.level]
let str = `${levelMapped.name}[${parsed.context || 'Top'}]`
let prefix = levelMapped.colors.reduce((str, color) => colors[color](str), str)
console.log(`${prefix} ${parsed.msg}`)
} catch (err) {
console.log(line)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment