const log = require('./log.js')

function shortTail(s, tailLen) {
  tailLen = tailLen || 8
  if (!s) return ''
  var t = String(s)
  return t.length <= tailLen ? t : '…' + t.slice(-tailLen)
}

function logWeapp(phase, payload) {
  try {
    var line = typeof payload === 'string' ? payload : JSON.stringify(payload)
    if (line.length > 1500) {
      line = line.slice(0, 1500) + '…(truncated)'
    }
    log.info('[HtyWeapp]', phase, line)
  } catch (err) {
    log.info('[HtyWeapp]', phase, String(payload))
  }
}

module.exports = {
  shortTail: shortTail,
  logWeapp: logWeapp
}
