整合日志记录

This commit is contained in:
lyswhut 2021-05-25 16:55:25 +08:00
parent c9e58d7d68
commit 32ccb33d2b
6 changed files with 54 additions and 102 deletions

View File

@ -6,7 +6,7 @@
// import '@/utils/log'
import './shim'
import '@/utils/errorHandle'
import { init as initLog } from '@/utils/log'
import { init as initLog, log } from '@/utils/log'
import '@/config/globalData'
import SplashScreen from 'react-native-splash-screen'
import { init as initNavigation, navigations, showPactModal } from '@/navigation'
@ -18,7 +18,7 @@ import { action as listAction } from '@/store/modules/list'
import { init as initMusicTools } from '@/utils/music'
import { init as initLyric } from '@/plugins/lyric'
import { init as initI18n, supportedLngs } from '@/plugins/i18n'
import { deviceLanguage, getPlayInfo } from '@/utils/tools'
import { deviceLanguage, getPlayInfo, toast } from '@/utils/tools'
import { LIST_ID_PLAY_TEMP } from '@/config/constant'
console.log('starting app...')
@ -65,7 +65,17 @@ const init = () => {
if (info.list) info.list = info.list.list
}
if (!info.list || !info.list[info.index]) return
if (!info.list || !info.list[info.index]) {
const info2 = { ...info }
if (info2.list) {
info2.music = info2.list[info2.index]?.name
info2.list = info2.list.length
}
toast('恢复播放数据失败,请去错误日志查看', 'long')
log.warn('Restore Play Info failed: ', JSON.stringify(info2, null, 2))
return
}
global.restorePlayInfo = info
store.dispatch(playerAction.setList({

View File

@ -3,3 +3,7 @@
- 尝试修复软件启动时恢复上一次播放的歌曲可能导致软件崩溃的问题
- 尝试修复播放详情页歌词导致UI冻结的问题
- 修复企鹅音乐搜索歌曲没有结果的问题
### 其他
- 整合日志记录

View File

@ -179,7 +179,7 @@
"setting_other_cache_size": "Currently used cache size: ",
"setting_other_cache_clear_btn": "Clear Cache",
"setting_other_cache_clear_success_tip": "Cache clearing completed",
"setting_other_log": "Error log (error log when the software crashes 💥)",
"setting_other_log": "Error log (log when abnormal operation occurs)",
"setting_other_log_tip_clean_success": "Log cleaning completed",
"setting_other_log_tip_null": "The log is empty~",
"setting_other_log_btn_show": "View log",

View File

@ -179,7 +179,7 @@
"setting_other_cache_size": "当前已用缓存大小:",
"setting_other_cache_clear_btn": "清理缓存",
"setting_other_cache_clear_success_tip": "缓存清理完成",
"setting_other_log": "错误日志(软件崩溃💥时的错误日志)",
"setting_other_log": "错误日志(运行发生异常时的日志)",
"setting_other_log_tip_clean_success": "日志清理完成",
"setting_other_log_tip_null": "日志是空的哦~",
"setting_other_log_btn_show": "查看日志",

View File

@ -1,6 +1,6 @@
import React, { memo, useRef, useState, useEffect } from 'react'
import { StyleSheet, View, Text, InteractionManager } from 'react-native'
import { LOG_TYPE, getLogs, clearLogs } from '@/utils/log'
import { getLogs, clearLogs } from '@/utils/log'
import { useGetter } from '@/store'
// import { gzip, ungzip } from 'pako'
@ -18,11 +18,11 @@ export default memo(() => {
const isUnmountedRef = useRef(true)
const getErrorLog = () => {
getLogs(LOG_TYPE.error).then(log => {
getLogs().then(log => {
if (isUnmountedRef.current) return
const logArr = log.split('\n')
const logArr = log.split('\n\n')
logArr.reverse()
setLogText(logArr.join('\n\n').replace(/\n+$/, ''))
setLogText(logArr.join('\n').replace(/\n+$/, ''))
})
}
@ -36,7 +36,7 @@ export default memo(() => {
}
const handleCleanLog = () => {
clearLogs(LOG_TYPE.error).then(() => {
clearLogs().then(() => {
toast(t('setting_other_log_tip_clean_success'))
getErrorLog()
})

View File

@ -1,44 +1,20 @@
// import { requestStoragePermission } from '@/utils/common'
import { temporaryDirectoryPath, existsFile, writeFile, appendFile, mkdir, readFile, unlink } from '@/utils/fs'
import { temporaryDirectoryPath, existsFile, writeFile, appendFile, readFile, unlink } from '@/utils/fs'
const logPath = temporaryDirectoryPath + '/error.log'
export const LOG_TYPE = {
info: 'INFO',
warn: 'WARN',
error: 'ERROR',
}
const logDir = temporaryDirectoryPath + '/lx_logs'
const logPath = {
info: logDir + '/info.log',
warn: logDir + '/warn.log',
error: logDir + '/error.log',
}
const logTools = {
tempLog: {
info: [],
warn: [],
error: [],
tempLog: [],
writeLog(msg) {
console.log(msg)
appendFile(logPath, '\n\n' + msg)
},
writeLog(type, msg) {
switch (type) {
case LOG_TYPE.info:
appendFile(logPath.info, '\n' + msg)
break
case LOG_TYPE.warn:
appendFile(logPath.warn, '\n' + msg)
break
case LOG_TYPE.error:
appendFile(logPath.error, '\n' + msg)
break
default:
break
}
},
async initLogFile(type, filePath) {
async initLogFile() {
try {
let isExists = await existsFile(filePath)
if (!isExists) await writeFile(filePath, '')
if (this.tempLog[type].length) this.writeLog(LOG_TYPE[type], this.tempLog[type].map(m => `${m.time} ${m.type} ${m.text}`).join('\n'))
this.tempLog[type] = null
let isExists = await existsFile(logPath)
if (!isExists) await writeFile(logPath, '')
if (this.tempLog) this.writeLog(this.tempLog.map(m => `${m.time} ${m.type} ${m.text}`).join('\n'))
this.tempLog = null
} catch (err) {
console.error(err)
}
@ -46,53 +22,15 @@ const logTools = {
}
export const init = () => {
return existsFile(logDir).then(isExists => {
if (isExists) return
return mkdir(logDir)
}).then(() => {
const tasks = []
for (const [type, path] of Object.entries(logPath)) {
tasks.push(logTools.initLogFile(type, path))
}
console.log('init log tools')
return Promise.all(tasks)
})
return logTools.initLogFile()
}
export const getLogs = (type = LOG_TYPE.error) => {
let path
switch (type) {
case LOG_TYPE.info:
path = logPath.info
break
case LOG_TYPE.warn:
path = logPath.warn
break
case LOG_TYPE.error:
path = logPath.error
break
default:
return Promise.reject(new Error('Unknow log type'))
}
return readFile(path)
export const getLogs = () => {
return readFile(logPath)
}
export const clearLogs = (type = LOG_TYPE.error) => {
let path
switch (type) {
case LOG_TYPE.info:
path = logPath.info
break
case LOG_TYPE.warn:
path = logPath.warn
break
case LOG_TYPE.error:
path = logPath.error
break
default:
return Promise.reject(new Error('Unknow log type'))
}
return unlink(path).then(() => writeFile(path, ''))
export const clearLogs = () => {
return unlink(logPath).then(() => writeFile(logPath, ''))
}
export const log = {
@ -101,26 +39,26 @@ export const log = {
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
if (msg.startsWith('%c')) return
const time = new Date().toLocaleString()
if (logTools.tempLog.info) {
logTools.tempLog.info.push({ type: 'LOG', time, text: msg })
} else logTools.writeLog(LOG_TYPE.info, `${time} LOG ${msg}`)
if (logTools.tempLog) {
logTools.tempLog.push({ type: 'LOG', time, text: msg })
} else logTools.writeLog(`${time} LOG ${msg}`)
},
warn(...msgs) {
// console.warn(...msgs)
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
const time = new Date().toLocaleString()
if (logTools.tempLog.warn) {
logTools.tempLog.warn.push({ type: 'WARN', time, text: msg })
} else logTools.writeLog(LOG_TYPE.warn, `${time} WARN ${msg}`)
if (logTools.tempLog) {
logTools.tempLog.push({ type: 'WARN', time, text: msg })
} else logTools.writeLog(`${time} WARN ${msg}`)
},
error(...msgs) {
// console.error...(msgs)
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
const time = new Date().toLocaleString()
if (logTools.tempLog.error) {
logTools.tempLog.error.push({ type: 'ERROR', time, text: msg })
if (logTools.tempLog) {
logTools.tempLog.push({ type: 'ERROR', time, text: msg })
} else {
logTools.writeLog(LOG_TYPE.error, `${time} ERROR ${msg}`)
logTools.writeLog(`${time} ERROR ${msg}`)
}
},
}
@ -142,7 +80,7 @@ if (process.env.NODE_ENV !== 'development') {
if (msg.startsWith('%c')) return
const time = new Date().toLocaleString()
if (tempLog) {
tempLog.push({ type: 'LOG', time, text: msg })
tempLog({ type: 'LOG', time, text: msg })
} else writeLog(`${time} LOG ${msg}`)
}
window.console.error = (...msgs) => {
@ -150,7 +88,7 @@ if (process.env.NODE_ENV !== 'development') {
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
const time = new Date().toLocaleString()
if (tempLog) {
tempLog.push({ type: 'ERROR', time, text: msg })
tempLog({ type: 'ERROR', time, text: msg })
} else writeLog(`${time} ERROR ${msg}`)
}
window.console.warn = (...msgs) => {
@ -158,7 +96,7 @@ if (process.env.NODE_ENV !== 'development') {
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
const time = new Date().toLocaleString()
if (tempLog) {
tempLog.push({ type: 'WARN', time, text: msg })
tempLog({ type: 'WARN', time, text: msg })
} else writeLog(`${time} WARN ${msg}`)
}
@ -169,7 +107,7 @@ if (process.env.NODE_ENV !== 'development') {
let isExists = await existsFile(logPath)
console.log(logPath, isExists)
if (!isExists) await writeFile(logPath, '')
writeLog(tempLog.map(m => `${m.time} ${m.type} ${m.text}`).join('\n'))
writeLog(tempLog(m => `${m.time} ${m.type} ${m.text}`).join('\n'))
tempLog = null
} catch (err) {
console.error(err)