在 開發過程中 我們可以通過日志 來查看出現的各種問題,但是在生產環境中遇到問題時我們有時很難復現,不易我們了解到具體的問題,收集生產環境的報錯日志會幫助我們記錄問題,方便后期處理。
在main.js進行全局配置。
// 上報錯誤日志 const errors = [] const getTimestamp = () => { return new Date() } Vue.config.errorHandler = error => { errors.push({time:getTimestamp(),content:error.stack}) } window.onerror = (message,source,lineno,colon,error) => { errors.push({time:getTimestamp(),content:error.stack}) } window.addEventListener('error',event => { errors.push({time:getTimestamp(),content:event.error.stack}) }) setInterval(()=>{ if(errors.length > 0){ console.log(errors) const length = errors.length axios.post( '/',errors ).then(response => { errors.splice(0,length) },error => { console.log('保存日志報錯了',res) }) } },3000)
(還在學習中,不足后期會修改,補充)