RemoteLog.js
1.36 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
import sentryInit from '@/utils/sentryConfig/sentryInit'
import { LogLevel , RemoteExtraDataKey} from './constants'
class RemoteLog {
sentry = null
constructor(user) {
this.sentry = sentryInit()
this.sentry.setUser(user)
}
debug(info) {
this.send(LogLevel.DEBUG, info)
}
info(info) {
this.send(LogLevel.INFO, info)
}
warn(info) {
this.send(LogLevel.WARN, info)
}
error(info) {
this.send(LogLevel.ERROR, info)
}
send(level, { extra = {}, msg, tag = { key: '', value: '' } }) {
this.sentry.withScope(scope => {
if (tag && tag.key) {
scope.setTag(tag.key, tag.value)
}
// 记录更多上下文信息
scope.setExtra(RemoteExtraDataKey, extra)
scope.setLevel(level)
// 添加设备信息到 extra
try {
const systemInfo = uni.getSystemInfoSync()
scope.setExtra('device_brand', systemInfo.brand)
scope.setExtra('device_model', systemInfo.model)
scope.setExtra('os_version', systemInfo.system)
scope.setExtra('app_version', systemInfo.appVersion)
} catch (e) {
// 忽略获取设备信息失败
}
// 区分 Error 对象和普通消息
if (msg instanceof Error) {
this.sentry.captureException(msg)
} else {
this.sentry.captureMessage(String(msg))
}
})
}
}
export default RemoteLog