Created
September 20, 2019 14:00
-
-
Save gabrielsaints/5e4f74822c880e644a3bcde8875eca39 to your computer and use it in GitHub Desktop.
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
import chalk from 'chalk'; | |
import fs from 'fs'; | |
import root from 'app-root-path'; | |
import stripAnsi from 'strip-ansi'; | |
chalk.enabled = true; | |
chalk.level = 3; | |
const { log } = console; | |
const intro = { | |
server: () => `(${process.env.APP_ID}/${process.env.PORT}):`, | |
}; | |
const late = message => `${intro.server()} ${chalk.cyan( | |
typeof message === 'object' ? JSON.stringify(message) : message, | |
)}`; | |
class MyPino { | |
constructor() { | |
process.on('unhandledRejection', e => this.hard(e.message, 'unhandledRejection')); | |
process.on('uncaughtException', e => this.hard(e.message, 'uncaughtException')); | |
process.on('warning', e => this.hard(e.message, 'warning')); | |
process.on('rejectionHandled', e => this.hard(e.message, 'rejectionHandled')); | |
} | |
save(message, dateBuffer, category = 'debug') { | |
try { | |
const date = new Date(dateBuffer); | |
const directory = `${root.path}/log/${date.getFullYear()}/${date.getMonth() | |
+ 1}/${date.getDate()}/${date.getHours()}`; | |
const path = `/${category}.output.log`; | |
log(message); | |
if (!fs.existsSync(directory)) { | |
fs.mkdirSync(directory, { recursive: true }); | |
} | |
fs.appendFileSync(`${directory}${path}`, `[${date.toISOString()}] ${stripAnsi(message)}\n`, 'utf8', () => true); | |
} catch (e) {} | |
} | |
fatal(message) { | |
const date = Date.now(); | |
this.save(`[${date}] ${chalk.bgRed(chalk.black('FATAL'))} ${late(message)}`, date, 'fatal'); | |
} | |
hard(message, type) { | |
const date = Date.now(); | |
this.save(`[${date}] ${chalk.bgMagenta(chalk.black('HARD ERROR'))} - ${chalk.bgRed(chalk.black(type))} ${late(message)}`, date, 'hard'); | |
} | |
info(message) { | |
const date = Date.now(); | |
this.save(`[${date}] ${chalk.green(chalk.underline('INFO'))} ${late(message)}`, date, 'info'); | |
} | |
logger(message) { | |
const date = Date.now(); | |
this.save( | |
`[${date}] ${chalk.bgWhite(chalk.black(chalk.underline('LOGGER')))} ${late(message)}`, | |
date, | |
'logger', | |
); | |
} | |
caller(message) { | |
const date = Date.now(); | |
this.save( | |
`[${date}] ${chalk.bgBlue(chalk.black(chalk.underline('CALLER')))} ${late(message)}`, | |
date, | |
'caller', | |
); | |
} | |
debug(message) { | |
const date = Date.now(); | |
this.save(`[${date}] ${chalk.bgMagenta(chalk.black('DEBUG'))} ${late(message)}`, date, 'debug'); | |
} | |
warn(message) { | |
const date = Date.now(); | |
this.save( | |
`[${date}] ${chalk.bgYellow(chalk.black(chalk.underline('WARN')))} ${late(message)}`, | |
date, | |
'warn', | |
); | |
} | |
error(message) { | |
const date = Date.now(); | |
this.save( | |
`[${date}] ${chalk.underline(chalk.red(chalk.underline('ERROR')))} ${late(message)}`, | |
date, | |
'error', | |
); | |
} | |
} | |
export default new MyPino(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment