使用H5+的IO API写了记录日志的功能,Android可以直接在文件管理系统里查看日志,并可以将日志导出;IOS的不能在文件中直接查看,IOS如何可以直接在文件管理里查看日志,类似下图这样,将日志写在这里?
我的代码如下:
let debug = true; // true : 打印日志 或是 记录日志;false:不打印 不记录
let isConsole = false;// true:只打印到控制台不记录到文件;false:只记录到文件不打印到控制台
/**
*
import logX from '@/common/utils/logX.js'
await logX.log("我是日志哦。");
await logX.info("我是日志哦。");
await logX.warn("我是日志哦。");
await logX.error("我是日志哦。");
// tag建议是页面名称和方法名称[pageName-methodName] 便于定位;
await logX.logWithTag("my-test","我是日志哦。00000111======1===-===1");
await logX.infoWithTag("my-test","我是日志哦。00000111=======2=====2");
await logX.warnWithTag("my-test","我是日志哦。00000111========3====3");
await logX.errorWithTag("my-test","我是日志哦。00000111======4======11");
在 [main.js]里全局挂载,不用在调用的地方导入了
import logX from '@/common/utils/logX.js'
uni.$logX = logX;
await uni.$logX.log("我是日志哦。");
await uni.$logX.info("我是日志哦。");
await uni.$logX.warn("我是日志哦。");
await uni.$logX.error("我是日志哦。");
await uni.logX.logWithTag("my-test","我是日志哦。00000111======1===-===1");
await uni.logX.infoWithTag("my-test","我是日志哦。00000111=======2=====2");
await uni.logX.warnWithTag("my-test","我是日志哦。00000111========3====3");
await uni.logX.errorWithTag("my-test","我是日志哦。00000111======4======11");
*/
/**
* js 日期格式化
* 传时间戳参数进行时间戳转换,不传时返回当前时间(返回年月日时分秒格式)
* @param {Object} timeStamp 时间戳参数 非必传
* @param {Object} format 格式化样式 非必传
*/
function getTimeStampDatetime(format = 'yyyy-MM-dd HH:mm:ss', timeStamp) {
let nowDatetime = new Date()
timeStamp ? nowDatetime = new Date(timeStamp) : nowDatetime = new Date()
return nowDatetime.Format(format)
}
Date.prototype.Format = function(fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"H+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[
k]).substr(("" + o[k]).length)));
return fmt;
}
// 获取日志文件名称
function getLogFileInfo() {
// 今日日期
let nowDate = getTimeStampDatetime("yyyy-MM-dd")
// 当前时间精确到秒
let nowTime = getTimeStampDatetime("HH:mm:ss")
// 文件名称
let fileName = "log" + nowDate + '.txt'
// 内容换行符(自定义)
let newLine = nowTime + " " ;
return {
newLine: newLine,
fileName: fileName
}
}
function getFs(){
return new Promise((resolve,reject) =>{
plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS,function(fs){
resolve(fs);
},function(error){
console.error("文件系统进入错误", error);
reject(error);
});
});
}
function getDir(fs){
return new Promise((resolve,reject) =>{
fs.root.getDirectory("app-logs",{
create:true,
exclusive:false
},function(dir){
// 创建或写入文件
console.log("LogPath->: " + dir.fullPath);
resolve(dir);
},function(error){
console.error("文件夹创建失败", error);
reject(error);
});
});
}
function getFileEntry(dir){
return new Promise((resolve,reject) =>{
dir.getFile(getLogFileInfo().fileName, {
create: true
}, function(fileEntry) {
resolve(fileEntry);
},function(error) {
console.error("日志文件创建失败", error);
reject(error);
});
});
}
function getWriter(fileEntry){
return new Promise((resolve,reject) =>{
fileEntry.createWriter(function(writer) {
resolve(writer);
}, function(error) {
console.error("获取writer失败", error)
reject(error);
});
});
}
let logDir = null;
async function writeLog1(tag,msg) {
if(!debug)return;
if(!logDir){
let fs = await getFs();
logDir = await getDir(fs);
}
let fileEntry = await getFileEntry(logDir);
let writer = await getWriter(fileEntry);
let text = tag +" : "+JSON.stringify(msg);
writer.seek(writer.length + 1);
writer.write(getLogFileInfo().newLine + text+ "\r\n\r\n");
}
async function writeLog(tag,...msg) {
// #ifdef APP-PLUS
if(!debug)return;
if(!logDir){
let fs = await getFs();
logDir = await getDir(fs);
}
let fileEntry = await getFileEntry(logDir);
let writer = await getWriter(fileEntry);
let info = "";
for (let i = 0; i < msg.length; i++) {
if(i == 0){
info = JSON.stringify (msg[i]);
}else{
info = info +","+ JSON.stringify (msg[i]);
}
}
let text = tag +":"+info;
writer.seek(writer.length + 1);
writer.write(getLogFileInfo().newLine + text+ "\r\n");
// #endif
}
export default {
async log(){
if(!debug)return;
if(isConsole){
console.log(...arguments);
}else{
await writeLog(`[log]`,...arguments);
}
},
async info(){
if(!debug)return;
if(isConsole){
console.info(...arguments);
}else{
await writeLog(`[info]`,...arguments);
}
},
async warn(){
if(!debug)return;
if(isConsole){
console.warn(...arguments);
}else{
await writeLog(`[warn]`,...arguments);
}
},
async error(){
if(!debug)return;
if(isConsole){
console.error(...arguments);
}else{
await writeLog(`[error]`,...arguments);
}
},
async logWithTag(tag,...msg){
if(!debug)return;
if(isConsole){
console.log(tag,...msg);
}else{
await writeLog(`[log] <${tag}>`,...msg);
}
},
async infoWithTag(tag,...msg){
if(!debug)return;
if(isConsole){
console.info(tag,...msg);
}else{
await writeLog(`[info] <${tag}>`,...msg);
}
},
async warnWithTag(tag,...msg){
if(!debug)return;
if(isConsole){
console.warn(tag,...msg);
}else{
await writeLog(`[warn] <${tag}>`,...msg);
}
},
async errorWithTag(tag,...msg){
if(!debug)return;
if(isConsole){
console.error(tag,...msg);
}else{
await writeLog(`[error]<${tag}>`,...msg);
}
}
}
l***@qq.com (作者)
插件用了,但是云打包时报错了,不知道什么原因
2022-10-14 09:26