更新记录
1.0.0(2025-11-26)
下载此版本
基础功能、适配鸿蒙
平台兼容性
uni-app(3.6.14)
| Vue2 |
Vue2插件版本 |
Vue3 |
Vue2插件版本 |
Chrome |
Safari |
app-vue |
app-vue插件版本 |
app-nvue |
app-nvue插件版本 |
Android |
Android插件版本 |
iOS |
鸿蒙 |
鸿蒙插件版本 |
| √ |
1.0.0 |
√ |
1.0.0 |
× |
× |
√ |
1.0.0 |
√ |
1.0.0 |
5.0 |
1.0.0 |
- |
19 |
1.0.0 |
| 微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
快应用-华为 |
快应用-联盟 |
| × |
× |
× |
× |
× |
× |
× |
× |
× |
× |
× |
uni-app x(4.0)
| Chrome |
Safari |
Android |
Android插件版本 |
iOS |
鸿蒙 |
鸿蒙插件版本 |
微信小程序 |
| × |
× |
5.0 |
1.0.0 |
- |
19 |
1.0.0 |
- |
其他
yk-logger
一个跨平台的日志记录工具,支持 Android、iOS 和 HarmonyOS 平台。提供日志分级、日志文件按天分割、控制台输出等功能。
iOS 代码用 AI 仿 Android 和 HarmonyOS 生成,未测试。
功能特性
- 📝 多级别日志:
debug、info、warn、error
- 🗂️ 日志文件按天分割,自动管理
- 📱 支持 Android、iOS、HarmonyOS 三端行为一致
- 🔧 可配置日志等级、标签、是否打印到控制台
- 💾 自动保存至各平台推荐目录,方便分享与上传
相关插件(简化版)
yk-log 基于uts实现的安卓日志插件
使用方法
1. 引入模块
<script setup>
import {
config,
debug,
info,
warn,
error,
closeLog,
getLogDirectoryPath,
getCurrentLogFilePath
} from '@/uni_modules/yk-logger'
</script>
2. 配置日志选项
config({
level: 'debug', // 设置最低记录级别(debug/info/warn/error)
tag: 'MyApp', // 默认标签名
printConsole: true // 是否同时打印到控制台
})
3. 记录日志
debug('这是一条调试信息')
info('应用启动成功')
warn('网络连接不稳定', 'Network')
error('支付失败,订单号:123456', 'Pay')
4. 获取日志路径(可用于分享或上传)
// 获取日志文件夹路径
const logDir = getLogDirectoryPath()
if (logDir) {
console.log("日志文件夹:", logDir)
}
// 获取当前日志文件路径
const logFile = getCurrentLogFilePath()
if (logFile) {
console.log("当前日志文件:", logFile)
}
5. 页面卸载时关闭日志(可选)
<script setup>
import { onBeforeUnmount } from 'vue'
import { closeLog } from '@/uni_modules/yk-logger'
onBeforeUnmount(() => {
closeLog()
})
</script>
API 说明
| 方法名 |
说明 |
参数类型 |
返回值类型 |
config(options) |
配置日志参数 |
LoggerOptions |
void |
debug(message, tag?) |
输出 debug 级别日志 |
string, string? |
void |
info(message, tag?) |
输出 info 级别日志 |
string, string? |
void |
warn(message, tag?) |
输出 warn 级别日志 |
string, string? |
void |
error(message, tag?) |
输出 error 级别日志 |
string, string? |
void |
closeLog() |
关闭并释放当前日志文件资源 |
- |
void |
getLogDirectoryPath() |
获取日志文件夹完整路径 |
- |
string 或 null |
getCurrentLogFilePath() |
获取今天日志文件完整路径 |
- |
string 或 null |
LoggerOptions 参数说明
type LoggerOptions = {
level?: 'debug' | 'info' | 'warn' | 'error' // 最低记录级别,默认 debug
tag?: string // 默认标签名,默认 YKApp
printConsole?: boolean // 是否打印到控制台,默认 true
}
存储位置
- Android: 内部存储
/Logs/ 文件夹
- iOS: Documents
/Logs/ 文件夹
- HarmonyOS: filesDir
/Logs/ 文件夹
每个日志文件命名格式为:{yyyy-MM-dd}_app_log.txt
注意事项
- 建议在页面卸载前调用
closeLog() 来释放资源
- 正式发布版本建议设置
level 为 'warn' 或更高以减少性能影响
- 日志具有持久性,在 App 卸载前会一直保留
完整示例
<template>
<view></view>
</template>
<script setup>
import { onBeforeUnmount } from 'vue';
import {
config,
debug,
info,
warn,
error,
closeLog,
getLogDirectoryPath, getCurrentLogFilePath,
} from '@/uni_modules/yk-logger';
config({
level: 'debug', // 正式环境可以改成 'warn'
tag: 'MyApp',
printConsole: true // 开发时开,正式包可关
});
// 获取日志文件夹(常用于分享整个文件夹)
const logDir = getLogDirectoryPath();
if (logDir) {
console.log("日志文件夹:", logDir);
}
// 获取当前正在写的日志文件(常用于单文件分享、上传)
const logFile = getCurrentLogFilePath();
if (logFile) {
console.log("当前日志文件:", logFile);
}
debug('App调试');
info('App启动成功');
warn('网络超时,准备重连', 'Network');
error('支付失败,订单号:123456', 'Pay');
// App 退出时(可选)
onBeforeUnmount(() => {
closeLog();
});
</script>