console.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import devCache from "../libs/devCache";
  2. import devOptions from "../libs/devOptions";
  3. import jsonCompress from "../libs/jsonCompress";
  4. export default {
  5. logList: [],
  6. options: null,
  7. /**
  8. * 挂载打印拦截器
  9. */
  10. install() {
  11. let that = this;
  12. this.options = devOptions.getOptions()
  13. if (!this.options.console.status) return;
  14. this.logList = devCache.get("console");
  15. if (!this.logList) this.logList = [];
  16. this.syncReqData(); //同步缓存
  17. if (uni.__log__) {
  18. // ! VUE3在app端时有这个特殊的方法
  19. that.mountUniConsole()
  20. } else {
  21. that.mountJsConsole()
  22. }
  23. //! 删除指定记录
  24. uni.$on("devTools_delConsoleItem", (item) => {
  25. let i = that.logList.findIndex(
  26. (x) => {
  27. let t = JSON.stringify(x.list)
  28. return t == JSON.stringify(item.list) &&
  29. x.time == item.time &&
  30. x.page == item.page &&
  31. x.type == item.type
  32. }
  33. );
  34. if (i != -1) {
  35. that.logList.splice(i, 1);
  36. }
  37. that.saveData()
  38. });
  39. //! 清空console日志
  40. uni.$on("devTools_delConsoleAll", () => {
  41. that.logList = []
  42. that.saveData()
  43. });
  44. },
  45. /**
  46. * 同步请求信息到缓存数据中
  47. */
  48. syncReqData() {
  49. let that = this;
  50. setTimeout(() => {
  51. try {
  52. that.saveData()
  53. } catch (error) {
  54. console.log("console.syncReqData error", error);
  55. }
  56. that.syncReqData()
  57. }, 3000);
  58. },
  59. /**
  60. * 同步数据到缓存
  61. */
  62. saveData() {
  63. let that = this;
  64. that.logList = jsonCompress.compressArray(that.logList, 'end', that.options.console.cache.size)
  65. devCache.set("console", that.logList)
  66. },
  67. /**
  68. * 挂载监听js自带的console函数
  69. */
  70. mountJsConsole() {
  71. let that = this;
  72. try {
  73. let l = console.log;
  74. try {
  75. globalThis.consoleLog = function () {
  76. console.log(...arguments)
  77. };
  78. } catch (error) { }
  79. try {
  80. window.consoleLog = function () {
  81. console.log(...arguments)
  82. };
  83. } catch (error) { }
  84. console.log = function () {
  85. replaceConsole("log", arguments)
  86. };
  87. let e = console.error;
  88. function _error() {
  89. try {
  90. let args = [...arguments]
  91. if (
  92. args[0]
  93. && typeof args[0] == "string"
  94. && (
  95. args[0] == "__ignoreReport__" //! 忽略错误日志上报
  96. || args[0].indexOf("__ignoreReport__") == 0
  97. )
  98. ) {
  99. let _args = []
  100. if (args.length > 1) {
  101. for (let i = 0; i < args.length; i++) {
  102. if (i != 0) {
  103. _args.push(args[i])
  104. }
  105. }
  106. } else {
  107. _args[0] = args[0];
  108. _args[0] = _args[0].replace("__ignoreReport__", "");
  109. }
  110. if (that.options.console.isOutput) {
  111. e(..._args)
  112. }
  113. return;
  114. }
  115. replaceConsole("error", args)
  116. } catch (error) {
  117. e("监听console.error出错", error)
  118. }
  119. }
  120. console.error = _error;
  121. let w = console.warn;
  122. console.warn = function () {
  123. replaceConsole("warn", arguments)
  124. };
  125. let i = console.info;
  126. console.info = function () {
  127. replaceConsole("info", arguments)
  128. };
  129. /**
  130. * 替换系统打印函数
  131. */
  132. function replaceConsole(type, args) {
  133. try {
  134. let data = []
  135. if (args && args.length > 0) {
  136. let argList = args;
  137. // #ifdef APP-PLUS
  138. if (args.length == 1) {
  139. argList = args[0].split("---COMMA---");
  140. let endItem = argList[argList.length - 1];
  141. if (
  142. endItem
  143. && typeof endItem == "string"
  144. && endItem.indexOf(" at ") > -1
  145. && endItem.indexOf(":") > -1
  146. ) { // 可能包含路径信息
  147. let endList = endItem.split(" at ");
  148. if (endList.length == 2) {
  149. argList.pop()
  150. argList.push(endList[0])
  151. argList.push("at " + endList[1])
  152. }
  153. }
  154. argList = argList.map((item, index) => {
  155. try {
  156. if (typeof item == "string") {
  157. if (item.indexOf("---BEGIN") > -1) {
  158. let isJson = item.indexOf("---BEGIN:JSON---") > -1;
  159. item = item.replace(/---BEGIN:.*?---/g, '')
  160. item = item.replace(/---END:.*?---/g, '')
  161. if (isJson) {
  162. item = JSON.parse(item);
  163. }
  164. } else if (item == "---NULL---") {
  165. item = null;
  166. } else if (item == "---UNDEFINED---") {
  167. item = undefined;
  168. }
  169. }
  170. } catch (error) {
  171. console.log("replaceConsole 尝试解析对象出错:", error);
  172. }
  173. return item;
  174. })
  175. }
  176. // #endif
  177. let oneSize = that.options.console.cache.rowSize / argList.length;
  178. for (let i = 0; i < argList.length; i++) {
  179. let row = jsonCompress.compressObject(argList[i], oneSize)
  180. data.push(row)
  181. }
  182. } else {
  183. data = []
  184. }
  185. let page = "未知";
  186. try {
  187. let pages = getCurrentPages()
  188. let item = pages[pages.length - 1];
  189. if (item && item.route) {
  190. page = item.route;
  191. }
  192. } catch (error) { }
  193. that.logList.unshift({
  194. list: data,
  195. time: new Date().getTime(),
  196. page,
  197. type,
  198. })
  199. if (that.options.console.isOutput) {
  200. if (type == "error") {
  201. e(...args)
  202. } else if (type == "warn") {
  203. w(...args)
  204. } else if (type == "info") {
  205. i(...args)
  206. } else {
  207. l(...args)
  208. }
  209. }
  210. } catch (error) {
  211. if (that.options.console.isOutput) {
  212. e("监听console出错", error)
  213. }
  214. }
  215. }
  216. } catch (error) {
  217. console.log("console.install error", error);
  218. }
  219. },
  220. /**
  221. * 挂载监听uni自带的打印函数
  222. */
  223. mountUniConsole() {
  224. let that = this;
  225. try {
  226. let uniSysConsole = uni.__log__;
  227. try {
  228. globalThis.consoleLog = function () {
  229. uni.__log__("log", "未知来源", ...arguments)
  230. };
  231. } catch (error) { }
  232. try {
  233. window.consoleLog = function () {
  234. uni.__log__("log", "未知来源", ...arguments)
  235. };
  236. } catch (error) { }
  237. uni.__log__ = function (type, line, ...args) {
  238. try {
  239. // 处理特殊情况 "__ignoreReport__" 忽略错误日志上报
  240. if (type == "error") {
  241. if (
  242. args[0]
  243. && typeof args[0] == "string"
  244. && (
  245. args[0] == "__ignoreReport__" //! 忽略错误日志上报
  246. || args[0].indexOf("__ignoreReport__") == 0
  247. )
  248. ) {
  249. let _args = []
  250. if (args.length > 1) {
  251. for (let i = 0; i < args.length; i++) {
  252. if (i != 0) {
  253. _args.push(args[i])
  254. }
  255. }
  256. } else {
  257. _args[0] = args[0];
  258. _args[0] = _args[0].replace("__ignoreReport__", "");
  259. }
  260. if (that.options.console.isOutput) {
  261. uniSysConsole(type, line, ..._args)
  262. }
  263. return;
  264. }
  265. }
  266. let data = []
  267. if (args && args.length > 0) {
  268. let argList = args;
  269. let oneSize = that.options.console.cache.rowSize / argList.length;
  270. for (let i = 0; i < argList.length; i++) {
  271. let row = jsonCompress.compressObject(argList[i], oneSize)
  272. data.push(row)
  273. }
  274. } else {
  275. data = []
  276. }
  277. let page = "未知";
  278. try {
  279. let pages = getCurrentPages()
  280. let item = pages[pages.length - 1];
  281. if (item && item.route) {
  282. page = item.route;
  283. }
  284. } catch (error) { }
  285. data.push(line)
  286. that.logList.unshift({
  287. list: data,
  288. time: new Date().getTime(),
  289. page,
  290. type,
  291. })
  292. if (that.options.console.isOutput) {
  293. uniSysConsole(type, line, ...data)
  294. }
  295. } catch (error) {
  296. if (that.options.console.isOutput) {
  297. uniSysConsole("error", "监听console出错", error)
  298. }
  299. }
  300. }
  301. /**
  302. * 替换系统打印函数
  303. */
  304. function replaceConsole(type, args) {
  305. try {
  306. let data = []
  307. if (args && args.length > 0) {
  308. let argList = args;
  309. // #ifdef APP-PLUS
  310. if (args.length == 1) {
  311. argList = args[0].split("---COMMA---");
  312. let endItem = argList[argList.length - 1];
  313. if (
  314. endItem
  315. && typeof endItem == "string"
  316. && endItem.indexOf(" at ") > -1
  317. && endItem.indexOf(":") > -1
  318. ) { // 可能包含路径信息
  319. let endList = endItem.split(" at ");
  320. if (endList.length == 2) {
  321. argList.pop()
  322. argList.push(endList[0])
  323. argList.push("at " + endList[1])
  324. }
  325. }
  326. argList = argList.map((item, index) => {
  327. try {
  328. if (typeof item == "string") {
  329. if (item.indexOf("---BEGIN") > -1) {
  330. let isJson = item.indexOf("---BEGIN:JSON---") > -1;
  331. item = item.replace(/---BEGIN:.*?---/g, '')
  332. item = item.replace(/---END:.*?---/g, '')
  333. if (isJson) {
  334. item = JSON.parse(item);
  335. }
  336. } else if (item == "---NULL---") {
  337. item = null;
  338. } else if (item == "---UNDEFINED---") {
  339. item = undefined;
  340. }
  341. }
  342. } catch (error) {
  343. console.log("replaceConsole 尝试解析对象出错:", error);
  344. }
  345. return item;
  346. })
  347. }
  348. // #endif
  349. let oneSize = that.options.console.cache.rowSize / argList.length;
  350. for (let i = 0; i < argList.length; i++) {
  351. let row = jsonCompress.compressObject(argList[i], oneSize)
  352. data.push(row)
  353. }
  354. } else {
  355. data = []
  356. }
  357. let page = "未知";
  358. try {
  359. let pages = getCurrentPages()
  360. let item = pages[pages.length - 1];
  361. if (item && item.route) {
  362. page = item.route;
  363. }
  364. } catch (error) { }
  365. that.logList.unshift({
  366. list: data,
  367. time: new Date().getTime(),
  368. page,
  369. type,
  370. })
  371. if (that.options.console.isOutput) {
  372. if (type == "error") {
  373. e(...args)
  374. } else if (type == "warn") {
  375. w(...args)
  376. } else if (type == "info") {
  377. i(...args)
  378. } else {
  379. l(...args)
  380. }
  381. }
  382. } catch (error) {
  383. if (that.options.console.isOutput) {
  384. e("监听console出错", error)
  385. }
  386. }
  387. }
  388. } catch (error) {
  389. console.log("console.install error", error);
  390. }
  391. },
  392. }