index.js
1.1 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
// import { index } from './env'
import RemoteLog from './RemoteLog'
import LocalLog from './LocalLog'
import { LogLevel } from './constants'
import { storageApi } from '../cache'
import { getIp } from './util'
class Logger {
remote = null
local = null
fields = []
constructor(fields = []) {
this.fields = fields
this.init()
}
init() {
const user = this.getUserInfo()
this.remote = new RemoteLog(user)
this.local = new LocalLog()
}
debug(info) {
this.write(LogLevel.DEBUG, info)
}
info(info) {
this.write(LogLevel.INFO, info)
}
warn(info) {
this.write(LogLevel.WARN, info)
}
error(info) {
this.write(LogLevel.ERROR, info)
}
write(level, info) {
this.remote[level](info)
this.local[level](info)
}
getUserInfo() {
const userInfo = storageApi.getUserInfo()
// const ip = await getIp()
const user = this.fields.reduce((pre, cur) => {
pre[cur] = userInfo[cur]
return pre
}, {})
user.name = userInfo.name
user.merchantName = userInfo.merchantName
// user.ip = ip
return user
}
}
export default new Logger()