const util = require('util') const my = { inUniapp: typeof globalThis.getApp === 'function' && typeof globalThis.uni === 'object', } function deepStringify (args = []) { // 在 nodejs 的 prod 环境,上色导致 pm2 log 里有特殊的颜色字符。在浏览器里,prod 时上色无效,反而导致console输出特殊颜色字符。 if (globalThis.process?.release?.name === 'node') { // in nodejs console, object only shows children of depth <= 3 by default. 如果要完整数据,就要进行扩展。 return globalThis.wo?.envar?.logDeep ? util.inspect(args, { showHidden: false, depth: null, colors: process.env.NODE_ENV === 'production' ? false : typeof globalThis.wo?.envar?.logColor === 'undefined' ? true : globalThis.wo?.envar?.logColor, }) : args // JSON.stringify(args, null, 2) } else if (my.inUniapp) { // 可再分为 web 和 app,通过 #ifdef 或 globalThis.window/location 判断 // in browser console, object is expandable by default. return globalThis.wo?.envar?.logDeep ? util.inspect(args, { showHidden: false, depth: null, colors: process.env.NODE_ENV === 'production' ? false : typeof globalThis.wo?.envar?.logColor === 'undefined' ? true : globalThis.wo?.envar?.logColor, }) : args } else if (globalThis.uniCloud) { return args } else { return args } } function fromPath () { if (globalThis.process?.release?.name === 'node') { // new Error().stack?.split('\n') , .match(/at (.*?) /g)[2] 其中 0 是本函数名本身,1 是 cclog 等 return { _from: new Error().stack?.match?.(/\bat (.*?) /g)?.[2] } } else if (globalThis.uni && globalThis.UniApp) { return { _from: globalThis.getCurrentPages?.()?.pop?.()?.route?.substring?.(6) || 'App' } } else { return {} } } function expandArgs (args) { if (args.length > 1) { return { _args: args } } else if (typeof args[0] === 'object') { return args[0] } else { return { _arg: args[0] } } } module.exports = { cclog (...args) { if (!my.inUniapp || process.env.NODE_ENV !== 'production') { console.log( deepStringify({ _at: new Date().toJSON(), _type: 'CLOG', // arguments.callee.name, // TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them ...fromPath(), ...expandArgs(args), }) ) my.inUniapp || console.log(',') } }, ccinfo (...args) { if (!my.inUniapp || process.env.NODE_ENV !== 'production') { console.info( deepStringify({ _at: new Date().toJSON(), _type: 'CINFO', ...fromPath(), ...expandArgs(args), }) ) my.inUniapp || console.log(',') } }, ccwarn (...args) { if (!my.inUniapp || process.env.NODE_ENV !== 'production') { console.warn( deepStringify({ _at: new Date().toJSON(), _type: 'CWARN', ...fromPath(), ...expandArgs(args), }) ) my.inUniapp || console.log(',') } }, ccerror (...args) { if (!my.inUniapp || process.env.NODE_ENV !== 'production') { console.error( deepStringify({ _at: new Date().toJSON(), _type: 'CERROR', ...fromPath(), ...expandArgs(args), }) ) my.inUniapp || console.log(',') } }, ccdebug (...args) { if (!my.inUniapp || process.env.NODE_ENV !== 'production') { console.debug( deepStringify({ _at: new Date().toJSON(), _type: 'CDEBUG', ...fromPath(), ...expandArgs(args), }) ) my.inUniapp || console.log(',') } }, }