log.js
3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import FloatLogBus from '@/utils/logger/FloatLogBus.js'
import LogCat from '@/utils/logger/LogCat.js'
import { LogLevel } from '@/utils/logger/constants.js'
export const MERGE_LOG_TAG = 'StoragePathMerge'
const MERGE_LOG_STORAGE_KEY = 'zyzs_storage_merge_log_history'
/** 与业务 LocalLog 同目录,避免再建一套 logger 封装导致悬浮窗/落盘不同步 */
let mergeLogCat = null
function getMergeLogCat() {
if (!mergeLogCat) {
mergeLogCat = new LogCat('_zyzs_log')
}
return mergeLogCat
}
function formatLine(msg, extra) {
return extra !== undefined ? `${msg} ${JSON.stringify(extra)}` : msg
}
function formatFull(level, msg, extra) {
return `[${MERGE_LOG_TAG}] ${formatLine(msg, extra)}`
}
function persistMergeLog(full) {
try {
let raw = uni.getStorageSync(MERGE_LOG_STORAGE_KEY)
let arr = []
if (raw) {
arr = typeof raw === 'string' ? JSON.parse(raw) : raw
}
if (!Array.isArray(arr)) arr = []
const d = new Date()
const pad = n => (n < 10 ? '0' + n : String(n))
const time = `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
arr.push({ time, level: 'info', text: full, id: `persist_${Date.now()}` })
if (arr.length > 80) arr = arr.slice(-80)
uni.setStorageSync(MERGE_LOG_STORAGE_KEY, JSON.stringify(arr))
} catch (_) {
/* ignore */
}
}
/**
* 路径合并专用日志(与业务日志系统对齐):
* - FloatLogBus:悬浮窗 / getMergeLines(始终 push,带 [StoragePathMerge])
* - LogCat:与 uni.$logger.local 相同 _zyzs_log 落盘
* 不经过 storageMerge 单独通道,避免 install 前后悬浮窗漏日志
*/
function emit(level, msg, extra) {
const full = formatFull(level, msg, extra)
const busLevel = level === 'error' ? 'error' : 'info'
const catLevel = level === 'error' ? LogLevel.ERROR : LogLevel.INFO
try {
FloatLogBus.push(busLevel, full)
} catch (_) {
/* ignore */
}
try {
getMergeLogCat().writeLog(catLevel, full)
} catch (_) {
/* ignore */
}
const oc = FloatLogBus.getOriginalConsole?.()
if (level === 'error') {
;(oc?.error || console.error)(full)
} else {
;(oc?.log || console.log)(full)
}
persistMergeLog(full)
}
export function logInfo(msg, extra) {
emit('info', msg, extra)
}
export function logError(msg, extra) {
emit('error', msg, extra)
}
/** 归并流程分阶段步骤(info) */
export function logStep(stage, msg, extra) {
emit('info', `[${stage}] ${msg}`, extra)
}
/** 归并流程分阶段失败(error) */
export function logStepFail(stage, msg, extra) {
emit('error', `[${stage}] ${msg}`, extra)
}
export function loadPersistedMergeLogs() {
try {
const raw = uni.getStorageSync(MERGE_LOG_STORAGE_KEY)
if (!raw) return []
const arr = typeof raw === 'string' ? JSON.parse(raw) : raw
return Array.isArray(arr) ? arr : []
} catch (_) {
return []
}
}
export function waitForPlus(timeoutMs = 5000) {
return new Promise(resolve => {
if (typeof plus !== 'undefined') {
resolve(plus)
return
}
let settled = false
const done = p => {
if (settled) return
settled = true
resolve(p || null)
}
if (typeof document !== 'undefined') {
document.addEventListener('plusready', () => done(typeof plus !== 'undefined' ? plus : null), {
once: true
})
}
setTimeout(() => done(typeof plus !== 'undefined' ? plus : null), timeoutMs)
})
}