This commit is contained in:
2025-11-10 14:30:23 +08:00
parent ca97e8dba2
commit 508f6154f8
32 changed files with 472 additions and 248 deletions

View File

@@ -0,0 +1,4 @@
## 1.0.12024-10-30
1. 解决中文乱码问题
## 1.0.02024-10-28
1. 实现日志写入/storage/emulated/0/Android/data/包名/files/Logs中

View File

@@ -0,0 +1,87 @@
{
"id": "yk-log",
"displayName": "yk-log 基于uts实现的安卓日志插件",
"version": "1.0.1",
"description": "基于uts实现的日志插件保存日志文件到外部存储的私有目录下目前Android 10及以下测试可用10以上未测试",
"keywords": [
"yk-log",
"uts",
"日志",
"Android"
],
"repository": "",
"engines": {
"HBuilderX": "^3.91"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": "2466286500"
},
"declaration": {
"ads": "无",
"data": "插件不采集任何数据",
"permissions": "无"
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y",
"alipay": "y"
},
"client": {
"Vue": {
"vue2": "y",
"vue3": "y"
},
"App": {
"app-android": {
"minVersion": "21"
},
"app-ios": "u"
},
"H5-mobile": {
"Safari": "n",
"Android Browser": "n",
"微信浏览器(Android)": "n",
"QQ浏览器(Android)": "n"
},
"H5-pc": {
"Chrome": "n",
"IE": "n",
"Edge": "n",
"Firefox": "n",
"Safari": "n"
},
"小程序": {
"微信": "n",
"阿里": "n",
"百度": "n",
"字节跳动": "n",
"QQ": "n",
"钉钉": "n",
"快手": "n",
"飞书": "n",
"京东": "n"
},
"快应用": {
"华为": "n",
"联盟": "n"
}
}
}
}
}

View File

@@ -0,0 +1,31 @@
# yk-log
目前官方的`TextEncoder`提示不支持自己写的字符转byteArray方法后面会导致生成的字节数组末尾出现额外的零字节0所以日志换行之后会有乱码。
由于手机坏了只有备用机目前测试Android 10及以下的版本可以10以上理论上也可以。
日志文件存储在 /storage/emulated/0/Android/data/包名/files/Logs 目录中
### 方法
| 方法名 | 说明 | 参数 |
| ------ | -------------------------------------------- | ---- |
| write | 写入日志yyyy-MM-dd HH:mm:ss.SSS-[传入消息] | text |
### 使用示例
```js
import { write } from '../../uni_modules/yk-log'
export default {
data() {
return {
}
},
methods: {
wirteLog() {
write("1111")
}
}
}
```

View File

@@ -0,0 +1,3 @@
{
"minSdkVersion": "21"
}

View File

@@ -0,0 +1,54 @@
import { WriteApi } from '../interface';
import SimpleDateFormat from 'java.text.SimpleDateFormat';
import Locale from 'java.util.Locale';
import FileOutputStream from 'java.io.FileOutputStream';
import File from 'java.io.File';
import Date from "java.util.Date";
import OutputStreamWriter from 'java.io.OutputStreamWriter';
const LOG_DISPLAY_NAME = "app_log.txt";
// 字符串转Uint8Array
function stringToUint8Array(str: string) : ByteArray{
const arr: ByteArray = new ByteArray(str.length);
for (let i = 0; i < str.length; i++) {
arr.set(i.toInt(), str.charCodeAt(i)!!.toByte());
}
return arr
}
// 创建消息格式
function createMessage(logMessage : string) : string {
const timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()).format(new Date());
const logEntry = timestamp + " - " + logMessage + "\n";
return logEntry
}
// 写入日志
export const write : WriteApi = (logMessage : string) => {
const context = UTSAndroid.getAppContext();
// 获取应用的私有外部存储目录
const externalFilesDir = context!!.getExternalFilesDir(null); // type为null表示根目录
if (externalFilesDir == null) {
// 外部存储不可用或未挂载
return;
}
// 指定放在 应用的私有外部存储目录/Logs 目录中
const logDir = new File(externalFilesDir, `Logs`);
if (!logDir.exists()) {
logDir.mkdirs();
}
const nowDay = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
const fileName = `${nowDay}_${LOG_DISPLAY_NAME}`;
const logFile = new File(logDir, fileName);
try {
const fos = new FileOutputStream(logFile, true)
const oStreamWriter = OutputStreamWriter(fos, 'UTF-8')
oStreamWriter.append(createMessage(logMessage));
oStreamWriter.close()
} catch (e) {
console.log(e);
}
}

View File

@@ -0,0 +1,2 @@
/* 写入文件 */
export type WriteApi = (logMessage : string) => void