format-log.js 1.8 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
function isDebugMode () {
fxy060608's avatar
fxy060608 已提交
7 8
  /* eslint-disable no-undef */
  return typeof __channelId__ === 'string' && __channelId__
fxy060608's avatar
fxy060608 已提交
9 10
}

fxy060608's avatar
fxy060608 已提交
11 12 13 14 15 16 17
export function log (type) {
  for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    args[_key - 1] = arguments[_key]
  }
  console[type].apply(console, args)
}

fxy060608's avatar
fxy060608 已提交
18 19 20 21
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 已提交
22
  var type = args.shift()
fxy060608's avatar
fxy060608 已提交
23
  if (isDebugMode()) {
fxy060608's avatar
fxy060608 已提交
24 25
    args.push(args.pop().replace('at ', 'uni-app:///'))
    return console[type]['apply'](console, args)
fxy060608's avatar
fxy060608 已提交
26
  }
fxy060608's avatar
fxy060608 已提交
27 28

  var msgs = args.map(function (v) {
29
    var type = Object.prototype.toString.call(v).toLowerCase()
fxy060608's avatar
fxy060608 已提交
30

31
    if (type === '[object object]' || type === '[object array]') {
fxy060608's avatar
fxy060608 已提交
32 33 34 35 36 37 38 39 40 41 42
      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 已提交
43 44
        var vType = typof(v).toUpperCase()

fxy060608's avatar
fxy060608 已提交
45 46 47 48 49 50 51
        if (vType === 'NUMBER' || vType === 'BOOLEAN') {
          v = '---BEGIN:' + vType + '---' + v + '---END:' + vType + '---'
        } else {
          v = String(v)
        }
      }
    }
fxy060608's avatar
fxy060608 已提交
52

fxy060608's avatar
fxy060608 已提交
53 54
    return v
  })
fxy060608's avatar
fxy060608 已提交
55 56
  var msg = ''

fxy060608's avatar
fxy060608 已提交
57
  if (msgs.length > 1) {
fxy060608's avatar
fxy060608 已提交
58
    var lastMsg = msgs.pop()
fxy060608's avatar
fxy060608 已提交
59
    msg = msgs.join('---COMMA---')
fxy060608's avatar
fxy060608 已提交
60

fxy060608's avatar
fxy060608 已提交
61 62 63 64 65 66 67 68
    if (lastMsg.indexOf(' at ') === 0) {
      msg += lastMsg
    } else {
      msg += '---COMMA---' + lastMsg
    }
  } else {
    msg = msgs[0]
  }
fxy060608's avatar
fxy060608 已提交
69

fxy060608's avatar
fxy060608 已提交
70
  console[type](msg)
fxy060608's avatar
fxy060608 已提交
71
}