format-log.js 1.3 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 9 10 11 12 13
export default function formatLog () {
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
    args[_key] = arguments[_key]
  }

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

fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19 20 21 22 23 24 25
    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 已提交
26 27
        var vType = typof(v).toUpperCase()

fxy060608's avatar
fxy060608 已提交
28 29 30 31 32 33 34
        if (vType === 'NUMBER' || vType === 'BOOLEAN') {
          v = '---BEGIN:' + vType + '---' + v + '---END:' + vType + '---'
        } else {
          v = String(v)
        }
      }
    }
fxy060608's avatar
fxy060608 已提交
35

fxy060608's avatar
fxy060608 已提交
36 37
    return v
  })
fxy060608's avatar
fxy060608 已提交
38 39
  var msg = ''

fxy060608's avatar
fxy060608 已提交
40
  if (msgs.length > 1) {
fxy060608's avatar
fxy060608 已提交
41
    var lastMsg = msgs.pop()
fxy060608's avatar
fxy060608 已提交
42
    msg = msgs.join('---COMMA---')
fxy060608's avatar
fxy060608 已提交
43

fxy060608's avatar
fxy060608 已提交
44 45 46 47 48 49 50 51
    if (lastMsg.indexOf(' at ') === 0) {
      msg += lastMsg
    } else {
      msg += '---COMMA---' + lastMsg
    }
  } else {
    msg = msgs[0]
  }
fxy060608's avatar
fxy060608 已提交
52

fxy060608's avatar
fxy060608 已提交
53 54
  return msg
}