84 lines
3.8 KiB
JavaScript
84 lines
3.8 KiB
JavaScript
const colors = require('colors')
|
||
|
||
// HBuilder 内置环境的 console 不支持颜色。为了检查是否支持颜色,可测试 uniCloud 是否存在(不存在说明在自己的server环境里),或 require('supports-color'),相应的返回不同的 ccXXX 函数。
|
||
|
||
function pageNow () {
|
||
return globalThis.getCurrentPages()[globalThis.getCurrentPages().length - 1]
|
||
}
|
||
|
||
module.exports =
|
||
globalThis.uni && globalThis.UniApp // && globalThis.getApp?.()?.constructor?.name === 'Vue' // 是前端 uniapp。
|
||
? {
|
||
cclog (...args) {
|
||
const pageName = pageNow()?.route || 'VueApp'
|
||
console.log('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:blue;background:lightgrey', ...args)
|
||
},
|
||
ccinfo (...args) {
|
||
const pageName = pageNow()?.route || 'VueApp'
|
||
console.info('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:green;background:lightgrey', ...args)
|
||
},
|
||
ccwarn (...args) {
|
||
const pageName = pageNow()?.route || 'VueApp'
|
||
console.warn('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:orange;background:lightgrey', ...args)
|
||
},
|
||
ccerror (...args) {
|
||
const pageName = pageNow()?.route || 'VueApp'
|
||
console.error('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:red;background:lightgrey', ...args)
|
||
},
|
||
ccdebug (...args) {
|
||
if (process.env.NODE_ENV !== 'production') {
|
||
const pageName = pageNow()?.route || 'VueApp'
|
||
console.debug('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:cyan;background:lightgrey', ...args)
|
||
}
|
||
},
|
||
cctitle (...args) {
|
||
const pageName = pageNow().route || 'VueApp'
|
||
console.debug('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:cyan;background:lightgrey', ...args)
|
||
},
|
||
}
|
||
: typeof uniCloud === 'undefined'
|
||
? {
|
||
cclog (...args) {
|
||
console.log(colors.blue({ timeiso: new Date().toJSON() }), ...args)
|
||
},
|
||
ccinfo (...args) {
|
||
console.info(colors.green({ timeiso: new Date().toJSON() }), ...args)
|
||
},
|
||
ccerror (...args) {
|
||
// console.error will appear in pm2's error log
|
||
console.error(colors.red({ timeiso: new Date().toJSON() }), ...args)
|
||
},
|
||
ccwarn (...args) {
|
||
// console.warn will appear in pm2's error log
|
||
console.warn(colors.yellow({ timeiso: new Date().toJSON() }), ...args)
|
||
},
|
||
ccdebug (...args) {
|
||
if ('development' === process.env.NODE_ENV) {
|
||
// 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV`
|
||
console.log(colors.rainbow({ timeiso: new Date().toJSON() }), ...args)
|
||
}
|
||
},
|
||
}
|
||
: {
|
||
cclog (...args) {
|
||
console.log({ timeiso: new Date().toJSON() }, ...args)
|
||
},
|
||
ccinfo (...args) {
|
||
console.info({ timeiso: new Date().toJSON() }, ...args)
|
||
},
|
||
ccerror (...args) {
|
||
// console.error will appear in pm2's error log
|
||
console.error({ timeiso: new Date().toJSON() }, ...args)
|
||
},
|
||
ccwarn (...args) {
|
||
// console.warn will appear in pm2's error log
|
||
console.warn({ timeiso: new Date().toJSON() }, ...args)
|
||
},
|
||
ccdebug (...args) {
|
||
if ('development' === process.env.NODE_ENV) {
|
||
// 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV`
|
||
console.log({ timeiso: new Date().toJSON() }, ...args)
|
||
}
|
||
},
|
||
}
|