mirror of
https://github.com/ikun0014/lx-music-mobile.git
synced 2025-05-23 22:37:41 +08:00
整合日志记录
This commit is contained in:
parent
c9e58d7d68
commit
32ccb33d2b
16
index.js
16
index.js
@ -6,7 +6,7 @@
|
|||||||
// import '@/utils/log'
|
// import '@/utils/log'
|
||||||
import './shim'
|
import './shim'
|
||||||
import '@/utils/errorHandle'
|
import '@/utils/errorHandle'
|
||||||
import { init as initLog } from '@/utils/log'
|
import { init as initLog, log } from '@/utils/log'
|
||||||
import '@/config/globalData'
|
import '@/config/globalData'
|
||||||
import SplashScreen from 'react-native-splash-screen'
|
import SplashScreen from 'react-native-splash-screen'
|
||||||
import { init as initNavigation, navigations, showPactModal } from '@/navigation'
|
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 initMusicTools } from '@/utils/music'
|
||||||
import { init as initLyric } from '@/plugins/lyric'
|
import { init as initLyric } from '@/plugins/lyric'
|
||||||
import { init as initI18n, supportedLngs } from '@/plugins/i18n'
|
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'
|
import { LIST_ID_PLAY_TEMP } from '@/config/constant'
|
||||||
|
|
||||||
console.log('starting app...')
|
console.log('starting app...')
|
||||||
@ -65,7 +65,17 @@ const init = () => {
|
|||||||
if (info.list) info.list = info.list.list
|
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
|
global.restorePlayInfo = info
|
||||||
|
|
||||||
store.dispatch(playerAction.setList({
|
store.dispatch(playerAction.setList({
|
||||||
|
@ -3,3 +3,7 @@
|
|||||||
- 尝试修复软件启动时恢复上一次播放的歌曲可能导致软件崩溃的问题
|
- 尝试修复软件启动时恢复上一次播放的歌曲可能导致软件崩溃的问题
|
||||||
- 尝试修复播放详情页歌词导致UI冻结的问题
|
- 尝试修复播放详情页歌词导致UI冻结的问题
|
||||||
- 修复企鹅音乐搜索歌曲没有结果的问题
|
- 修复企鹅音乐搜索歌曲没有结果的问题
|
||||||
|
|
||||||
|
### 其他
|
||||||
|
|
||||||
|
- 整合日志记录
|
||||||
|
@ -179,7 +179,7 @@
|
|||||||
"setting_other_cache_size": "Currently used cache size: ",
|
"setting_other_cache_size": "Currently used cache size: ",
|
||||||
"setting_other_cache_clear_btn": "Clear Cache",
|
"setting_other_cache_clear_btn": "Clear Cache",
|
||||||
"setting_other_cache_clear_success_tip": "Cache clearing completed",
|
"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_clean_success": "Log cleaning completed",
|
||||||
"setting_other_log_tip_null": "The log is empty~",
|
"setting_other_log_tip_null": "The log is empty~",
|
||||||
"setting_other_log_btn_show": "View log",
|
"setting_other_log_btn_show": "View log",
|
||||||
|
@ -179,7 +179,7 @@
|
|||||||
"setting_other_cache_size": "当前已用缓存大小:",
|
"setting_other_cache_size": "当前已用缓存大小:",
|
||||||
"setting_other_cache_clear_btn": "清理缓存",
|
"setting_other_cache_clear_btn": "清理缓存",
|
||||||
"setting_other_cache_clear_success_tip": "缓存清理完成",
|
"setting_other_cache_clear_success_tip": "缓存清理完成",
|
||||||
"setting_other_log": "错误日志(软件崩溃💥时的错误日志)",
|
"setting_other_log": "错误日志(运行发生异常时的日志)",
|
||||||
"setting_other_log_tip_clean_success": "日志清理完成",
|
"setting_other_log_tip_clean_success": "日志清理完成",
|
||||||
"setting_other_log_tip_null": "日志是空的哦~",
|
"setting_other_log_tip_null": "日志是空的哦~",
|
||||||
"setting_other_log_btn_show": "查看日志",
|
"setting_other_log_btn_show": "查看日志",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import React, { memo, useRef, useState, useEffect } from 'react'
|
import React, { memo, useRef, useState, useEffect } from 'react'
|
||||||
import { StyleSheet, View, Text, InteractionManager } from 'react-native'
|
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 { useGetter } from '@/store'
|
||||||
// import { gzip, ungzip } from 'pako'
|
// import { gzip, ungzip } from 'pako'
|
||||||
|
|
||||||
@ -18,11 +18,11 @@ export default memo(() => {
|
|||||||
const isUnmountedRef = useRef(true)
|
const isUnmountedRef = useRef(true)
|
||||||
|
|
||||||
const getErrorLog = () => {
|
const getErrorLog = () => {
|
||||||
getLogs(LOG_TYPE.error).then(log => {
|
getLogs().then(log => {
|
||||||
if (isUnmountedRef.current) return
|
if (isUnmountedRef.current) return
|
||||||
const logArr = log.split('\n')
|
const logArr = log.split('\n\n')
|
||||||
logArr.reverse()
|
logArr.reverse()
|
||||||
setLogText(logArr.join('\n\n').replace(/\n+$/, ''))
|
setLogText(logArr.join('\n').replace(/\n+$/, ''))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ export default memo(() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const handleCleanLog = () => {
|
const handleCleanLog = () => {
|
||||||
clearLogs(LOG_TYPE.error).then(() => {
|
clearLogs().then(() => {
|
||||||
toast(t('setting_other_log_tip_clean_success'))
|
toast(t('setting_other_log_tip_clean_success'))
|
||||||
getErrorLog()
|
getErrorLog()
|
||||||
})
|
})
|
||||||
|
122
src/utils/log.js
122
src/utils/log.js
@ -1,44 +1,20 @@
|
|||||||
// import { requestStoragePermission } from '@/utils/common'
|
// 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 = {
|
const logTools = {
|
||||||
tempLog: {
|
tempLog: [],
|
||||||
info: [],
|
writeLog(msg) {
|
||||||
warn: [],
|
console.log(msg)
|
||||||
error: [],
|
appendFile(logPath, '\n\n' + msg)
|
||||||
},
|
},
|
||||||
writeLog(type, msg) {
|
async initLogFile() {
|
||||||
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) {
|
|
||||||
try {
|
try {
|
||||||
let isExists = await existsFile(filePath)
|
let isExists = await existsFile(logPath)
|
||||||
if (!isExists) await writeFile(filePath, '')
|
if (!isExists) await writeFile(logPath, '')
|
||||||
if (this.tempLog[type].length) this.writeLog(LOG_TYPE[type], this.tempLog[type].map(m => `${m.time} ${m.type} ${m.text}`).join('\n'))
|
if (this.tempLog) this.writeLog(this.tempLog.map(m => `${m.time} ${m.type} ${m.text}`).join('\n'))
|
||||||
this.tempLog[type] = null
|
this.tempLog = null
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
}
|
}
|
||||||
@ -46,53 +22,15 @@ const logTools = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const init = () => {
|
export const init = () => {
|
||||||
return existsFile(logDir).then(isExists => {
|
return logTools.initLogFile()
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getLogs = (type = LOG_TYPE.error) => {
|
export const getLogs = () => {
|
||||||
let path
|
return readFile(logPath)
|
||||||
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 clearLogs = (type = LOG_TYPE.error) => {
|
export const clearLogs = () => {
|
||||||
let path
|
return unlink(logPath).then(() => writeFile(logPath, ''))
|
||||||
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 log = {
|
export const log = {
|
||||||
@ -101,26 +39,26 @@ export const log = {
|
|||||||
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
||||||
if (msg.startsWith('%c')) return
|
if (msg.startsWith('%c')) return
|
||||||
const time = new Date().toLocaleString()
|
const time = new Date().toLocaleString()
|
||||||
if (logTools.tempLog.info) {
|
if (logTools.tempLog) {
|
||||||
logTools.tempLog.info.push({ type: 'LOG', time, text: msg })
|
logTools.tempLog.push({ type: 'LOG', time, text: msg })
|
||||||
} else logTools.writeLog(LOG_TYPE.info, `${time} LOG ${msg}`)
|
} else logTools.writeLog(`${time} LOG ${msg}`)
|
||||||
},
|
},
|
||||||
warn(...msgs) {
|
warn(...msgs) {
|
||||||
// console.warn(...msgs)
|
// console.warn(...msgs)
|
||||||
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
||||||
const time = new Date().toLocaleString()
|
const time = new Date().toLocaleString()
|
||||||
if (logTools.tempLog.warn) {
|
if (logTools.tempLog) {
|
||||||
logTools.tempLog.warn.push({ type: 'WARN', time, text: msg })
|
logTools.tempLog.push({ type: 'WARN', time, text: msg })
|
||||||
} else logTools.writeLog(LOG_TYPE.warn, `${time} WARN ${msg}`)
|
} else logTools.writeLog(`${time} WARN ${msg}`)
|
||||||
},
|
},
|
||||||
error(...msgs) {
|
error(...msgs) {
|
||||||
// console.error...(msgs)
|
// console.error...(msgs)
|
||||||
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
||||||
const time = new Date().toLocaleString()
|
const time = new Date().toLocaleString()
|
||||||
if (logTools.tempLog.error) {
|
if (logTools.tempLog) {
|
||||||
logTools.tempLog.error.push({ type: 'ERROR', time, text: msg })
|
logTools.tempLog.push({ type: 'ERROR', time, text: msg })
|
||||||
} else {
|
} 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
|
if (msg.startsWith('%c')) return
|
||||||
const time = new Date().toLocaleString()
|
const time = new Date().toLocaleString()
|
||||||
if (tempLog) {
|
if (tempLog) {
|
||||||
tempLog.push({ type: 'LOG', time, text: msg })
|
tempLog({ type: 'LOG', time, text: msg })
|
||||||
} else writeLog(`${time} LOG ${msg}`)
|
} else writeLog(`${time} LOG ${msg}`)
|
||||||
}
|
}
|
||||||
window.console.error = (...msgs) => {
|
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 msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
||||||
const time = new Date().toLocaleString()
|
const time = new Date().toLocaleString()
|
||||||
if (tempLog) {
|
if (tempLog) {
|
||||||
tempLog.push({ type: 'ERROR', time, text: msg })
|
tempLog({ type: 'ERROR', time, text: msg })
|
||||||
} else writeLog(`${time} ERROR ${msg}`)
|
} else writeLog(`${time} ERROR ${msg}`)
|
||||||
}
|
}
|
||||||
window.console.warn = (...msgs) => {
|
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 msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
||||||
const time = new Date().toLocaleString()
|
const time = new Date().toLocaleString()
|
||||||
if (tempLog) {
|
if (tempLog) {
|
||||||
tempLog.push({ type: 'WARN', time, text: msg })
|
tempLog({ type: 'WARN', time, text: msg })
|
||||||
} else writeLog(`${time} WARN ${msg}`)
|
} else writeLog(`${time} WARN ${msg}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,7 +107,7 @@ if (process.env.NODE_ENV !== 'development') {
|
|||||||
let isExists = await existsFile(logPath)
|
let isExists = await existsFile(logPath)
|
||||||
console.log(logPath, isExists)
|
console.log(logPath, isExists)
|
||||||
if (!isExists) await writeFile(logPath, '')
|
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
|
tempLog = null
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user