format-log.js 1.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
function typof (v) {
fxy060608's avatar
fxy060608 已提交
2
  var s = Object.prototype.toString.call(v)
fxy060608's avatar
fxy060608 已提交
3 4 5
  return s.substring(8, s.length - 1)
}

fxy060608's avatar
fxy060608 已提交
6 7 8
function isDebugMode () {
  /* eslint-disable no-undef */
  return typeof __channelId__ === 'string' && __channelId__
fxy060608's avatar
fxy060608 已提交
9 10
}

fxy060608's avatar
fxy060608 已提交
11 12 13 14
export default function formatLog () {
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
    args[_key] = arguments[_key]
  }
fxy060608's avatar
fxy060608 已提交
15 16 17 18
  const type = args.shift()
  if (isDebugMode()) {
    args.push(args.pop().replace('at ', 'uni-app:///'))
    return console[type]['apply'](console, args)
fxy060608's avatar
fxy060608 已提交
19
  }
fxy060608's avatar
fxy060608 已提交
20 21 22 23

  var msgs = args.map(function (v) {
    var type = Object.prototype.toString.call(v)

fxy060608's avatar
fxy060608 已提交
24 25 26 27 28 29 30 31 32 33 34 35
    if (type.toLowerCase() === '[object object]') {
      try {
        v = '---BEGIN:JSON---' + JSON.stringify(v) + '---END:JSON---'
      } catch (e) {
        v = '[object object]'
      }
    } else {
      if (v === null) {
        v = '---NULL---'
      } else if (v === undefined) {
        v = '---UNDEFINED---'
      } else {
fxy060608's avatar
fxy060608 已提交
36 37
        var vType = typof(v).toUpperCase()

fxy060608's avatar
fxy060608 已提交
38 39 40 41 42 43 44
        if (vType === 'NUMBER' || vType === 'BOOLEAN') {
          v = '---BEGIN:' + vType + '---' + v + '---END:' + vType + '---'
        } else {
          v = String(v)
        }
      }
    }
fxy060608's avatar
fxy060608 已提交
45

fxy060608's avatar
fxy060608 已提交
46 47
    return v
  })
fxy060608's avatar
fxy060608 已提交
48 49
  var msg = ''

fxy060608's avatar
fxy060608 已提交
50
  if (msgs.length > 1) {
fxy060608's avatar
fxy060608 已提交
51
    var lastMsg = msgs.pop()
fxy060608's avatar
fxy060608 已提交
52
    msg = msgs.join('---COMMA---')
fxy060608's avatar
fxy060608 已提交
53

fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60 61
    if (lastMsg.indexOf(' at ') === 0) {
      msg += lastMsg
    } else {
      msg += '---COMMA---' + lastMsg
    }
  } else {
    msg = msgs[0]
  }
fxy060608's avatar
fxy060608 已提交
62

fxy060608's avatar
fxy060608 已提交
63
  console[type](msg)
fxy060608's avatar
fxy060608 已提交
64
}