54 lines
2.8 KiB
JavaScript
54 lines
2.8 KiB
JavaScript
// colors only works in nodejs cli
|
|
// consola works in nodejs and browser
|
|
// chalk 和 colors 用法类似。
|
|
|
|
const util = require('util')
|
|
const colors = require('colors')
|
|
|
|
function deepInspect (args = [], colors = false) {
|
|
return args.map((arg) => util.inspect(arg, { showHidden: false, depth: null, colors })) // 如果用这个方案,在下面的方法里就要用 ...deepInspect(args) 否则会显示成一整字符串
|
|
}
|
|
|
|
module.exports =
|
|
{
|
|
// 后台服务器命令行。注意如果输出重定向到文件里,会有 ESC[34m2023-10-07T12:32:00.915ZESC[39m 这样的特殊标识。
|
|
// 在 pm2 里,为了防止特殊标志,可用 --no-color
|
|
cclog (...args) {
|
|
console.log(colors.bgWhite(new Date().toJSON()), colors.bgBlue('[LOG]'), ...deepInspect(args, true))
|
|
},
|
|
ccinfo (...args) {
|
|
console.info(colors.bgWhite(new Date().toJSON()), colors.bgBlue('[INFO]'), ...deepInspect(args, true))
|
|
},
|
|
ccgood (...args) {
|
|
console.info(colors.bgWhite(new Date().toJSON()), colors.bgGreen('[GOOD]'), ...deepInspect(args, true))
|
|
},
|
|
ccwarn (...args) {
|
|
// console.warn will appear in pm2's error log
|
|
console.warn(colors.bgWhite(new Date().toJSON()), colors.bgYellow('[WARN]'), ...deepInspect(args, true))
|
|
},
|
|
ccerror (...args) {
|
|
// console.error will appear in pm2's error log
|
|
console.error(colors.bgWhite(new Date().toJSON()), colors.bgRed('[ERROR]'), ...deepInspect(args, true))
|
|
},
|
|
ccdebug (...args) {
|
|
if ('production' !== process.env.NODE_ENV) {
|
|
// 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV`
|
|
console.log(colors.rainbow(new Date().toJSON()), colors.rainbow('[DEBUG]'), ...deepInspect(args, true))
|
|
}
|
|
},
|
|
ccinput (headers, path, indata) {
|
|
console.log(colors.bgWhite(new Date().toJSON()), colors.bgCyan('[Headers]'), colors.cyan(headers))
|
|
console.info(colors.bgWhite(new Date().toJSON()), colors.bgBrightBlue(`[INDATA] ${path}`), colors.brightBlue(util.inspect(indata, { showHidden: false, depth: null, colors: false }))) // 已经被 colors.xxx 进行上色了
|
|
},
|
|
ccoutput (path, outdata) {
|
|
console.log(colors.bgWhite(new Date().toJSON()), colors.bgGreen(`[OUTDATA] ${path}`), colors.green(util.inspect(outdata, { showHidden: false, depth: null, colors: false }))) // 已经被 colors.xxx 进行上色了
|
|
},
|
|
ccexcept (path, error) {
|
|
outdata = { _state: 'WOBASE_EXCEPTION', error }
|
|
console.error(colors.bgWhite(new Date().toJSON()), colors.bgRed(`[EXCEPTION] ${path}`), colors.red(util.inspect(outdata, { showHidden: false, depth: null, colors: true })))
|
|
},
|
|
ccunknown (path) {
|
|
console.warn(colors.bgWhite(new Date().toJSON()), colors.bgYellow(`[UNKNOWN] ${path}`), colors.yellow({ _state: 'WOBASE_API_UNKNOWN', response: 401 }))
|
|
}
|
|
}
|