mirror of
https://github.com/pockethost/pockethost.git
synced 2025-07-05 12:22:29 +00:00
enh: logger console fn update
This commit is contained in:
parent
c07b3077c6
commit
86eff83f2f
@ -32,7 +32,11 @@ export const createLogger = (config: Partial<Config>) => {
|
|||||||
.map((p) => `[${p}]`)
|
.map((p) => `[${p}]`)
|
||||||
.join(' ')
|
.join(' ')
|
||||||
|
|
||||||
const _log = (shouldDisplay: boolean, ...args: any[]) => {
|
const _log = (
|
||||||
|
fn: 'log' | 'warn' | 'error' | 'trace' | 'debug' = 'log',
|
||||||
|
shouldDisplay: boolean,
|
||||||
|
...args: any[]
|
||||||
|
) => {
|
||||||
if (_buf.length < MAX_BUF) {
|
if (_buf.length < MAX_BUF) {
|
||||||
_buf.push(args)
|
_buf.push(args)
|
||||||
} else {
|
} else {
|
||||||
@ -40,31 +44,31 @@ export const createLogger = (config: Partial<Config>) => {
|
|||||||
_curIdx++
|
_curIdx++
|
||||||
if (_curIdx === MAX_BUF) _curIdx = 0
|
if (_curIdx === MAX_BUF) _curIdx = 0
|
||||||
}
|
}
|
||||||
if (shouldDisplay) console.log(...args)
|
if (shouldDisplay) console[fn](...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
const raw = (...args: any[]) => {
|
const raw = (...args: any[]) => {
|
||||||
_log(_config.raw, _pfx('RAW'), ...args)
|
_log('log', _config.raw, _pfx('RAW'), ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
const dbg = (...args: any[]) => {
|
const dbg = (...args: any[]) => {
|
||||||
_log(_config.debug, _pfx(chalk.blueBright('DBG')), ...args)
|
_log('debug', _config.debug, _pfx(chalk.blueBright('DBG')), ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
const warn = (...args: any[]) => {
|
const warn = (...args: any[]) => {
|
||||||
_log(true, _pfx(chalk.yellow(chalk.cyanBright('WARN'))), ...args)
|
_log('warn', true, _pfx(chalk.yellow(chalk.cyanBright('WARN'))), ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
const info = (...args: any[]) => {
|
const info = (...args: any[]) => {
|
||||||
_log(true, _pfx(chalk.gray(`INFO`)), ...args)
|
_log('log', true, _pfx(chalk.gray(`INFO`)), ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
const trace = (...args: any[]) => {
|
const trace = (...args: any[]) => {
|
||||||
_log(_config.trace, _pfx(`TRACE`), ...args)
|
_log('trace', _config.trace, _pfx(`TRACE`), ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
const error = (...args: any[]) => {
|
const error = (...args: any[]) => {
|
||||||
_log(true, _pfx(chalk.bgRed(`ERROR`)), ...args)
|
_log('error', true, _pfx(chalk.bgRed(`ERROR`)), ...args)
|
||||||
if (!errorTrace) return
|
if (!errorTrace) return
|
||||||
console.error(`========== ERROR TRACEBACK BEGIN ==============`)
|
console.error(`========== ERROR TRACEBACK BEGIN ==============`)
|
||||||
;[..._buf.slice(_curIdx, MAX_BUF), ..._buf.slice(0, _curIdx)].forEach(
|
;[..._buf.slice(_curIdx, MAX_BUF), ..._buf.slice(0, _curIdx)].forEach(
|
||||||
@ -76,7 +80,7 @@ export const createLogger = (config: Partial<Config>) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const abort = (...args: any[]): never => {
|
const abort = (...args: any[]): never => {
|
||||||
_log(true, _pfx(chalk.bgRed(`ABORT`)), ...args)
|
_log('error', true, _pfx(chalk.bgRed(`ABORT`)), ...args)
|
||||||
throw new Error(`Fatal error: ${JSON.stringify(args)}`)
|
throw new Error(`Fatal error: ${JSON.stringify(args)}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user