index.js 1.1 KB
// 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()