uniapp添加操作日志的方法(uniapp、日志、文件、html5+)
前言
最近在做uniapp的項目,由于項目場景比較復(fù)雜,在處理售后問題的時候發(fā)現(xiàn)僅通過后端請求日志無法滿足需求,于是便開始寫一個前端保存操作日志的方法。
一、使用方法:
1、全局引入(main.js)
import {LogCat} from './utils/LogCat.js'
Vue.prototype.$LogCat = LogCat2、初始化日志存儲路徑(App.vue)
onLaunch: function() {
this.$LogCat.init()
}3、參數(shù)
調(diào)用this.$LogCat.d(tag,msg)方法
| 參數(shù) | 說明 | 數(shù)據(jù)類型 | 默認值 |
| tag | 日志標簽,默認會在標簽后面追加“:” | string | |
| msg | 日志內(nèi)容 | string、object、array | '' |
4、Vue文件中使用
methods:{
handleClick() {
let data = {
name:'張三',
age:18
}
this.$LogCat.d('點擊了',data) //2023-04-20 14:15:30 點擊了:{name:'張三',age:18}
}
}二、JS代碼如下:
export const LogCat = {
main: plus.android.runtimeMainActivity(),
Environment: plus.android.importClass('android.os.Environment'),
BufferedWriter: plus.android.importClass('java.io.BufferedWriter'),
File: plus.android.importClass('java.io.File'),
FileOutputStream: plus.android.importClass('java.io.FileOutputStream'),
OutputStreamWriter: plus.android.importClass('java.io.OutputStreamWriter'),
LogPath: '', //日志存儲目錄
saveDays: 7, //日志最大存儲天數(shù)
init: function() {
if (this.Environment.MEDIA_MOUNTED || !this.Environment.isExternalStorageRemovable()) {
this.LogPath = this.main.getExternalFilesDir(null).getPath();
} else {
this.LogPath = this.main.getFilesDir().getPath();
}
let fileManager = new this.File(this.LogPath);
let files = fileManager.listFiles()
let now = new Date();
let maxSavedDay = utils.getFormatDate(now - this.saveDays * 24 * 60 * 60 * 1000)
// 遍歷目錄下的日志文件,并刪除日志最大存儲天數(shù)前的日志
for (var i in files) {
let name = files[i].getName().split('.')[0],
time = name.split('_')[1]
if (time <= maxSavedDay && name.search('log_') == 0) {
files[i].delete()
}
}
console.log('LogPath->', this.LogPath);
},
d: function(tag, msg = '') {
let now = new Date();
let date = utils.getFormatDate(now);
let datetime = utils.getFormatDateTime(now);
msg = (typeof msg !== 'string' ? JSON.stringify(msg) : msg);
//文件名
let fileName = this.LogPath + "/log_" + date + ".txt"
//寫入的內(nèi)容
let content = `\n${datetime} ${tag}${msg ? ':'+msg : msg}\n`;
let file = new this.File(this.LogPath);
if (!file.exists()) {
file.mkdirs(); //創(chuàng)建父路徑
}
let fos = null;
let bw = null;
try {
fos = new this.FileOutputStream(fileName, true);
bw = new this.BufferedWriter(new this.OutputStreamWriter(fos));
//bw.write(log);
bw.append(content);
} catch (e) {
console.log('e->', e);
} finally {
try {
if (bw != null) {
bw.close(); //關(guān)閉緩沖流
fos.close(); //關(guān)閉文件輸出流
}
} catch (closeEx) {
console.log('closeEx->', closeEx);
}
}
}
}
export const utils = {
getFormatDate: (dateString) => {
const date = new Date(dateString);
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
month = month > 9 ? month : '0' + month;;
day = day > 9 ? day : '0' + day;
return `${year}-${month}-${day}`;
},
getFormatDateTime: (dateString) => {
const date = new Date(dateString);
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hour = date.getHours();
let min = date.getMinutes();
let second = date.getSeconds();
month = month > 9 ? month : '0' + month;;
day = day > 9 ? day : '0' + day;
hour = hour > 9 ? hour : '0' + hour;
min = min > 9 ? min : '0' + min;
second = second > 9 ? second : '0' + second;
return `${year}-${month}-${day} ${hour}:${min}:${second}`;
}
}三、說明
默認配置了日志最大存儲時間(saveDays)為7天,可自行修改
四、文章參考
https://ext.dcloud.net.cn/plugin?id=1816
總結(jié)
到此這篇關(guān)于uniapp添加操作日志(uniapp、日志、文件、html5+)的文章就介紹到這了,更多相關(guān)uniapp添加操作日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
『JavaScript』限制Input只能輸入數(shù)字實現(xiàn)思路及代碼
一個文字方塊必須限制只能輸入數(shù)字(或是小數(shù)點)并且要支援 IE 和 Firefox,接下來為大家介紹下如何解決這個需求2013-04-04
javascript動態(tài)創(chuàng)建鏈接的方法
這篇文章主要介紹了javascript動態(tài)創(chuàng)建鏈接的方法,涉及javascript動態(tài)操作頁面元素的技巧,需要的朋友可以參考下2015-05-05
layui+ssm實現(xiàn)數(shù)據(jù)表格雙擊編輯更新數(shù)據(jù)功能
在使用layui加載后端數(shù)據(jù)請求時,對數(shù)據(jù)選項框進行雙擊即可實現(xiàn)數(shù)據(jù)的輸入編輯更改,本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-12-12
詳解javascript立即執(zhí)行函數(shù)表達式IIFE
本文主要介紹了javascript立即執(zhí)行函數(shù)表達式IIFE的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
將函數(shù)的實際參數(shù)轉(zhuǎn)換成數(shù)組的方法
實際參數(shù)在函數(shù)中我們可以使用 arguments 對象獲得 (注:形參可通過 arguments.callee 獲得),雖然 arguments 對象與數(shù)組形似,但仍不是真正意義上的數(shù)組。2010-01-01
微信小程序wxss如何引用外部CSS文件以及iconfont
這篇文章主要給大家介紹了關(guān)于微信小程序wxss如何引用外部CSS文件以及iconfont的相關(guān)資料,文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

