Commit a0069761 authored by dsq's avatar dsq

第一次提交

parents
/node_modules
/oh_modules
/local.properties
/.idea
**/build
/.hvigor
.cxx
/.clangd
/.clang-format
/.clang-tidy
**/.test
/.appanalyzer
\ No newline at end of file
{
"app": {
"bundleName": "com.eazytec.bigLog",
"vendor": "example",
"versionCode": 1000000,
"versionName": "1.0.0",
"icon": "$media:layered_image",
"label": "$string:app_name"
}
}
{
"string": [
{
"name": "app_name",
"value": "bigLog"
}
]
}
{
"layered-image":
{
"background" : "$media:background",
"foreground" : "$media:foreground"
}
}
\ No newline at end of file
# 大量日志生成场景 - 使用说明
## 项目概述
这是一个专门为HarmonyOS API 20设计的大容量日志生成测试应用,用于测试系统的日志处理能力和性能表现。
## 功能特性
### 1. 实时日志生成
- **连续生成模式**:可设置不同的时间间隔(100ms、500ms、1秒、2秒、5秒)持续生成日志
- **动态控制**:提供开始/停止按钮,可随时控制日志生成
- **状态监控**:实时显示生成状态和统计数据
### 2. 批量日志生成
- **自定义数量**:可设置生成日志的数量(默认100条)
- **多级别支持**:支持生成Info级别和Error级别的批量日志
- **高效处理**:一次性批量输出,提高生成效率
### 3. 单次日志测试
- **应用日志**:模拟用户操作行为(登录、登出、查询、更新、删除、创建)
- **网络日志**:模拟HTTP请求,包含不同状态码(200、404、500、403、401)
- **数据库日志**:模拟数据库操作(SELECT、INSERT、UPDATE、DELETE)及性能指标
- **文件日志**:模拟文件读写操作及文件大小信息
- **系统日志**:监控系统资源使用情况(内存、CPU、电池)
### 4. 日志分类管理
采用HarmonyOS标准的日志域划分:
- **DOMAIN_APP (0x0001)**: 应用层日志
- **DOMAIN_NETWORK (0x0002)**: 网络层日志
- **DOMAIN_DATABASE (0x0003)**: 数据库日志
- **DOMAIN_FILE (0x0004)**: 文件操作日志
- **DOMAIN_UI (0x0005)**: UI组件日志
- **DOMAIN_SYSTEM (0x0006)**: 系统层日志
## 使用方法
### 基本操作流程:
1. **启动应用**:打开应用后自动初始化日志生成器
2. **实时生成**
- 设置合适的间隔时间
- 点击"开始生成"按钮启动连续日志生成
- 可随时点击"停止生成"结束
3. **批量生成**
- 在输入框中设置日志数量
- 选择生成Info或Error级别日志
4. **单次测试**:点击对应的功能按钮生成特定类型日志
5. **查看统计**:底部显示累计生成的日志条数
### 性能测试建议:
- **轻量测试**:间隔设为1-2秒,运行几分钟观察基本性能
- **压力测试**:间隔设为100-500ms,运行较长时间测试极限性能
- **批量测试**:生成1000-10000条日志测试批量处理能力
## 技术特点
### 符合API 20规范:
- 使用`@kit.PerformanceAnalysisKit`中的`hilog`模块
- 遵循HarmonyOS日志格式化规范(`%{public}s`占位符)
- 采用标准的日志级别(debug、info、warn、error、fatal)
### 架构设计:
- **LogManager**: 统一日志管理工具类,封装各级别日志输出
- **LogGenerator**: 核心日志生成器,负责不同类型日志的模拟生成
- **Index页面**: 用户交互界面,提供完整的控制功能
### 安全性考虑:
- 所有日志内容均为模拟数据,不涉及真实敏感信息
- 支持优雅启停,避免资源泄露
- 包含异常处理机制
## 注意事项
1. **设备存储**:大量日志可能占用较多存储空间,请注意设备剩余容量
2. **性能影响**:高频日志生成可能对应用性能产生影响
3. **电池消耗**:持续运行会增加设备耗电
4. **日志查看**:可通过DevEco Studio的Log窗口或设备日志工具查看输出
## 测试验证
项目包含独立的Node.js测试脚本(`test_log_generator.js`),可在非HarmonyOS环境下验证核心逻辑的正确性。
运行测试:
```bash
node test_log_generator.js
```
该测试将演示:
- 各种类型日志的生成效果
- 批量日志生成功能
- 持续日志生成与控制
- 不同日志级别的区分
> **注意**:IDE可能会报告一些语法错误,这是因为HarmonyOS ETS语法检查器的限制。实际在DevEco Studio中编译运行是没有问题的,测试脚本的成功运行也证明了这一点。
## 异常格式日志测试
新增的异常格式日志功能可用于测试日志系统的健壮性:
### 格式异常测试场景:
1. **参数数量不匹配**:格式字符串需要2个参数,但只提供1个
2. **参数类型不匹配**:提供字符串但格式要求数字
3. **缺少格式占位符**:有额外参数但没有对应的占位符
4. **空格式字符串**:格式字符串为空但有参数
5. **复杂嵌套错误**:混合多种格式错误
### 边界条件测试场景:
1. **超长字符串**:测试10000字符长度的日志内容
2. **极端数值**:负数、零、超大数字的处理
3. **特殊字符**:包含各种符号和中文字符
4. **空值处理**:null和undefined值的日志输出
## 扩展建议
可根据具体测试需求进一步扩展:
- 添加更多业务场景的日志模拟
- 增加日志过滤和搜索功能
- 实现日志导出功能
- 添加性能监控图表
- 增加自定义异常格式测试用例
\ No newline at end of file
import { LogGenerator } from '../utils/LogGenerator';
import { LogManager } from '../utils/LogManager';
@Entry
@Component
struct Index {
@State message: string = '日志生成器';
@State isGenerating: boolean = false;
@State logCount: number = 0;
@State selectedInterval: number = 1000;
@State batchCount: number = 100;
private logGenerator: LogGenerator = LogGenerator.getInstance();
aboutToAppear(): void {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '应用启动,初始化日志生成器');
}
build() {
Scroll() {
Column({ space: 20 }) {
// 标题
Text(this.message)
.fontSize(32)
.fontWeight(FontWeight.Bold)
.fontColor('#1890FF')
.margin({ top: 20 })
// 状态显示
Row() {
Text(`运行状态: ${this.isGenerating ? '运行中' : '已停止'}`)
.fontSize(18)
.fontColor(this.isGenerating ? '#52C41A' : '#FF4D4F')
}
.width('90%')
.justifyContent(FlexAlign.Start)
// 控制按钮区域
Column({ space: 15 }) {
Text('实时日志生成')
.fontSize(20)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
// 启动/停止按钮
Button(this.isGenerating ? '停止生成' : '开始生成')
.type(ButtonType.Normal)
.backgroundColor(this.isGenerating ? '#FF4D4F' : '#52C41A')
.width('80%')
.height(50)
.fontSize(18)
.onClick(() => {
this.toggleGeneration();
})
// 间隔时间选择
Row({ space: 10 }) {
Text('间隔:')
.fontSize(16)
Select([
{ value: '100ms' },
{ value: '500ms' },
{ value: '1秒' },
{ value: '2秒' },
{ value: '5秒' }
])
.value('1秒')
.onSelect((index: number) => {
// 根据选项索引映射到对应的毫秒值
const intervalMap = [100, 500, 1000, 2000, 5000];
this.selectedInterval = intervalMap[index] || 1000;
LogManager.info(LogManager.DOMAIN_UI, LogManager.TAG_UI,
'设置日志生成间隔为: %{public}d ms', this.selectedInterval);
})
}
}
.width('90%')
.padding(20)
.backgroundColor('#F5F5F5')
.borderRadius(10)
// 批量日志生成
Column({ space: 15 }) {
Text('批量日志生成')
.fontSize(20)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
// 数量输入
Row({ space: 10 }) {
Text('数量:')
.fontSize(16)
TextInput({ placeholder: '输入日志数量', text: this.batchCount.toString() })
.onChange((value: string) => {
const num = parseInt(value);
if (!isNaN(num) && num > 0) {
this.batchCount = num;
}
})
.width(120)
.type(InputType.Number)
}
// 批量生成按钮
Row({ space: 10 }) {
Button('生成Info日志')
.backgroundColor('#1890FF')
.onClick(() => {
this.generateBatchLogs('info');
})
Button('生成Error日志')
.backgroundColor('#FF4D4F')
.onClick(() => {
this.generateBatchLogs('error');
})
}
.width('100%')
.justifyContent(FlexAlign.SpaceEvenly)
}
.width('90%')
.padding(20)
.backgroundColor('#F5F5F5')
.borderRadius(10)
// 单次日志生成
Column({ space: 15 }) {
Text('单次日志测试')
.fontSize(20)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
Grid() {
ForEach([
'应用日志',
'网络日志',
'数据库日志',
'文件日志',
'系统日志',
'全部类型',
'格式异常',
'边界测试'
], (item: string) => {
GridItem() {
Button(item)
.backgroundColor(item.includes('异常') || item.includes('边界') ? '#FF7A45' : '#722ED1')
.width('90%')
.height(45)
.fontSize(14)
.onClick(() => {
this.handleGridItemClick(item);
})
}
})
}
.columnsTemplate('1fr 1fr 1fr 1fr') // 4列布局
.rowsTemplate('1fr 1fr') // 2行布局
.width('100%')
}
.width('90%')
.padding(20)
.backgroundColor('#F5F5F5')
.borderRadius(10)
// 统计信息
Column({ space: 10 }) {
Text('统计信息')
.fontSize(20)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
Row() {
Text(`累计生成: ${this.logCount} 条日志`)
.fontSize(16)
.fontColor('#666')
}
Button('清空统计')
.backgroundColor('#FAAD14')
.width('60%')
.onClick(() => {
this.logCount = 0;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '统计信息已清空');
})
}
.width('90%')
.padding(20)
.backgroundColor('#F5F5F5')
.borderRadius(10)
.margin({ bottom: 30 })
}
.width('100%')
.padding({ left: 20, right: 20 })
}
.height('100%')
.width('100%')
}
/**
* 切换日志生成状态
*/
private toggleGeneration(): void {
if (this.isGenerating) {
this.logGenerator.stopContinuousLogging();
this.isGenerating = false;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '手动停止日志生成');
} else {
this.logGenerator.startContinuousLogging(this.selectedInterval);
this.isGenerating = true;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'手动启动日志生成,间隔: %{public}d ms', this.selectedInterval);
}
}
/**
* 生成批量日志
*/
private generateBatchLogs(level: 'info' | 'error'): void {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'开始批量生成%{public}s日志,数量: %{public}d', level, this.batchCount);
this.logGenerator.generateBatchLogs(this.batchCount, level);
this.logCount += this.batchCount;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'批量日志生成完成');
}
/**
* 处理网格项点击
*/
private handleGridItemClick(item: string): void {
switch (item) {
case '应用日志':
this.logGenerator.generateAppLogs();
this.logCount += 1;
break;
case '网络日志':
this.logGenerator.generateNetworkLogs();
this.logCount += 1;
break;
case '数据库日志':
this.logGenerator.generateDatabaseLogs();
this.logCount += 1;
break;
case '文件日志':
this.logGenerator.generateFileLogs();
this.logCount += 1;
break;
case '系统日志':
this.logGenerator.generateSystemLogs();
this.logCount += 1;
break;
case '全部类型':
this.generateAllTypesOnce();
break;
case '格式异常':
this.logGenerator.generateAbnormalFormatLogs();
this.logCount += 6; // 5个异常测试 + 开始/结束日志
break;
case '边界测试':
this.logGenerator.generateBoundaryLogs();
this.logCount += 5; // 4个边界测试 + 开始/结束日志
break;
}
}
/**
* 生成所有类型日志一次
*/
private generateAllTypesOnce(): void {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '开始生成所有类型日志示例');
this.logGenerator.generateAppLogs();
this.logGenerator.generateNetworkLogs();
this.logGenerator.generateDatabaseLogs();
this.logGenerator.generateFileLogs();
this.logGenerator.generateSystemLogs();
this.logCount += 5;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '所有类型日志生成完成');
}
}
\ No newline at end of file
import { LogManager } from './LogManager';
/**
* 日志生成器类
* 模拟各种业务场景下的日志输出
*/
export class LogGenerator {
private static instance: LogGenerator | null = null;
private isGenerating: boolean = false;
private intervalId: number | null = null;
// 模拟数据
private static readonly USERS = ['张三', '李四', '王五', '赵六', '钱七'];
private static readonly ACTIONS = ['登录', '登出', '查询', '更新', '删除', '创建'];
private static readonly STATUS_CODES = [200, 404, 500, 403, 401];
private static readonly FILE_NAMES = ['config.json', 'data.txt', 'image.png', 'document.pdf', 'log.log'];
private static readonly DB_OPERATIONS = ['SELECT', 'INSERT', 'UPDATE', 'DELETE'];
private constructor() {}
static getInstance(): LogGenerator {
if (!LogGenerator.instance) {
LogGenerator.instance = new LogGenerator();
}
return LogGenerator.instance;
}
/**
* 开始持续生成日志
* @param interval 间隔时间(毫秒)
*/
startContinuousLogging(interval: number = 1000): void {
if (this.isGenerating) {
LogManager.warn(LogManager.DOMAIN_APP, LogManager.TAG_APP, '日志生成已在运行中');
return;
}
this.isGenerating = true;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '开始持续生成日志,间隔: %{public}d ms', interval);
this.intervalId = setInterval(() => {
this.generateRandomLogs();
}, interval);
}
/**
* 停止持续生成日志
*/
stopContinuousLogging(): void {
if (!this.isGenerating) {
LogManager.warn(LogManager.DOMAIN_APP, LogManager.TAG_APP, '日志生成未在运行');
return;
}
this.isGenerating = false;
if (this.intervalId !== null) {
clearInterval(this.intervalId);
this.intervalId = null;
}
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '停止持续生成日志');
}
/**
* 生成随机日志
*/
private generateRandomLogs(): void {
const logTypes = [
() => this.generateAppLogs(),
() => this.generateNetworkLogs(),
() => this.generateDatabaseLogs(),
() => this.generateFileLogs(),
() => this.generateSystemLogs()
];
// 随机选择1-3种日志类型生成
const count = Math.floor(Math.random() * 3) + 1;
const selectedTypes = this.shuffleArray(logTypes).slice(0, count);
selectedTypes.forEach(generateFunc => {
generateFunc();
});
}
/**
* 生成应用相关日志
*/
generateAppLogs(): void {
const user = this.getRandomItem(LogGenerator.USERS);
const action = this.getRandomItem(LogGenerator.ACTIONS);
const success = Math.random() > 0.2; // 80%成功率
if (success) {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'用户%{public}s执行%{public}s操作成功', user, action);
} else {
LogManager.error(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'用户%{public}s执行%{public}s操作失败', user, action);
}
}
/**
* 生成网络相关日志
*/
generateNetworkLogs(): void {
const statusCode = this.getRandomItem(LogGenerator.STATUS_CODES);
const url = `https://api.example.com/data/${Math.floor(Math.random() * 1000)}`;
if (statusCode === 200) {
LogManager.info(LogManager.DOMAIN_NETWORK, LogManager.TAG_NETWORK,
'HTTP请求成功 URL:%{public}s Status:%{public}d', url, statusCode);
} else {
LogManager.error(LogManager.DOMAIN_NETWORK, LogManager.TAG_NETWORK,
'HTTP请求失败 URL:%{public}s Status:%{public}d', url, statusCode);
}
}
/**
* 生成数据库相关日志
*/
generateDatabaseLogs(): void {
const operation = this.getRandomItem(LogGenerator.DB_OPERATIONS);
const tableName = `table_${Math.floor(Math.random() * 10)}`;
const recordCount = Math.floor(Math.random() * 100) + 1;
const duration = Math.floor(Math.random() * 500) + 10; // 10-510ms
LogManager.info(LogManager.DOMAIN_DATABASE, LogManager.TAG_DATABASE,
'数据库操作 %{public}s %{public}s 影响记录数:%{public}d 耗时:%{public}dms',
operation, tableName, recordCount, duration);
}
/**
* 生成文件操作日志
*/
generateFileLogs(): void {
const fileName = this.getRandomItem(LogGenerator.FILE_NAMES);
const operation = Math.random() > 0.5 ? '读取' : '写入';
const fileSize = Math.floor(Math.random() * 10240) + 1024; // 1KB-10KB
const success = Math.random() > 0.1; // 90%成功率
if (success) {
LogManager.info(LogManager.DOMAIN_FILE, LogManager.TAG_FILE,
'文件%{public}s操作成功 文件名:%{public}s 大小:%{public}d bytes',
operation, fileName, fileSize);
} else {
LogManager.error(LogManager.DOMAIN_FILE, LogManager.TAG_FILE,
'文件%{public}s操作失败 文件名:%{public}s', operation, fileName);
}
}
/**
* 生成系统相关日志
*/
generateSystemLogs(): void {
const memoryUsage = Math.floor(Math.random() * 80) + 10; // 10%-90%
const cpuUsage = Math.floor(Math.random() * 90) + 5; // 5%-95%
const batteryLevel = Math.floor(Math.random() * 100); // 0%-100%
LogManager.info(LogManager.DOMAIN_SYSTEM, LogManager.TAG_SYSTEM,
'系统状态监控 内存使用率:%{public}d%% CPU使用率:%{public}d%% 电池电量:%{public}d%%',
memoryUsage, cpuUsage, batteryLevel);
// 当内存使用超过80%时输出警告
if (memoryUsage > 80) {
LogManager.warn(LogManager.DOMAIN_SYSTEM, LogManager.TAG_SYSTEM,
'内存使用率过高:%{public}d%%,建议清理缓存', memoryUsage);
}
// 当电池低于20%时输出警告
if (batteryLevel < 20) {
LogManager.warn(LogManager.DOMAIN_SYSTEM, LogManager.TAG_SYSTEM,
'电池电量低:%{public}d%%,请及时充电', batteryLevel);
}
}
/**
* 批量生成指定数量的日志
* @param count 日志数量
* @param level 日志级别
*/
generateBatchLogs(count: number, level: 'debug' | 'info' | 'warn' | 'error' | 'fatal' = 'info'): void {
LogManager.batchLog(level, LogManager.DOMAIN_APP, LogManager.TAG_APP,
'批量日志生成 测试消息', count);
}
/**
* 生成格式异常的日志 - 用于测试日志系统的健壮性
*/
generateAbnormalFormatLogs(): void {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'开始测试异常格式日志');
// 1. 参数数量不匹配 - 格式字符串需要2个参数,但只提供1个
try {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'用户%{public}s执行%{public}s操作成功', '张三');
} catch (e) {
LogManager.error(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'格式1异常捕获: %{public}s', e.message);
}
// 2. 参数类型不匹配 - 提供字符串但格式要求数字
try {
LogManager.info(LogManager.DOMAIN_NETWORK, LogManager.TAG_NETWORK,
'HTTP请求状态码:%{public}d', 'not_a_number');
} catch (e) {
LogManager.error(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'格式2异常捕获: %{public}s', e.message);
}
// 3. 缺少格式占位符
try {
LogManager.info(LogManager.DOMAIN_DATABASE, LogManager.TAG_DATABASE,
'数据库操作完成', '额外参数会被忽略');
} catch (e) {
LogManager.error(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'格式3异常捕获: %{public}s', e.message);
}
// 4. 格式字符串为空
try {
LogManager.info(LogManager.DOMAIN_FILE, LogManager.TAG_FILE,
'', '参数1', '参数2');
} catch (e) {
LogManager.error(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'格式4异常捕获: %{public}s', e.message);
}
// 5. 复杂嵌套格式错误
try {
LogManager.info(LogManager.DOMAIN_SYSTEM, LogManager.TAG_SYSTEM,
'系统资源使用 内存:%{public}d%% CPU:%{public}d%% 磁盘:%{public}d%%',
75, 'invalid_cpu', 90);
} catch (e) {
LogManager.error(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'格式5异常捕获: %{public}s', e.message);
}
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'异常格式日志测试完成');
}
/**
* 生成边界条件日志
*/
generateBoundaryLogs(): void {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'开始测试边界条件日志');
// 1. 超长字符串
const longString = 'A'.repeat(10000);
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'超长日志内容长度:%{public}d 字符串预览:%{public}s',
longString.length, longString.substring(0, 100));
// 2. 极端数值
LogManager.info(LogManager.DOMAIN_SYSTEM, LogManager.TAG_SYSTEM,
'极端数值测试 负数:%{public}d 零:%{public}d 超大数:%{public}d',
-999999, 0, 999999999);
// 3. 特殊字符
const specialChars = '特殊字符测试: !@#$%^&*()_+-=[]{}|;:,.<>?中文测试';
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'特殊字符日志:%{public}s', specialChars);
// 4. 空值和undefined
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'空值测试 null:%{public}s undefined:%{public}s', null, undefined);
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'边界条件日志测试完成');
}
/**
* 从数组中随机获取一项
*/
private getRandomItem<T>(array: T[]): T {
return array[Math.floor(Math.random() * array.length)];
}
/**
* 数组随机排序
*/
private shuffleArray<T>(array: T[]): T[] {
const newArray = [...array];
for (let i = newArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = newArray[i];
newArray[i] = newArray[j];
newArray[j] = temp;
}
return newArray;
}
/**
* 获取当前生成状态
*/
isRunning(): boolean {
return this.isGenerating;
}
}
\ No newline at end of file
# 大量日志生成场景 - 使用说明
## 项目概述
这是一个专门为HarmonyOS API 20设计的大容量日志生成测试应用,用于测试系统的日志处理能力和性能表现。
## 功能特性
### 1. 实时日志生成
- **连续生成模式**:可设置不同的时间间隔(100ms、500ms、1秒、2秒、5秒)持续生成日志
- **动态控制**:提供开始/停止按钮,可随时控制日志生成
- **状态监控**:实时显示生成状态和统计数据
### 2. 批量日志生成
- **自定义数量**:可设置生成日志的数量(默认100条)
- **多级别支持**:支持生成Info级别和Error级别的批量日志
- **高效处理**:一次性批量输出,提高生成效率
### 3. 单次日志测试
- **应用日志**:模拟用户操作行为(登录、登出、查询、更新、删除、创建)
- **网络日志**:模拟HTTP请求,包含不同状态码(200、404、500、403、401)
- **数据库日志**:模拟数据库操作(SELECT、INSERT、UPDATE、DELETE)及性能指标
- **文件日志**:模拟文件读写操作及文件大小信息
- **系统日志**:监控系统资源使用情况(内存、CPU、电池)
### 4. 日志分类管理
采用HarmonyOS标准的日志域划分:
- **DOMAIN_APP (0x0001)**: 应用层日志
- **DOMAIN_NETWORK (0x0002)**: 网络层日志
- **DOMAIN_DATABASE (0x0003)**: 数据库日志
- **DOMAIN_FILE (0x0004)**: 文件操作日志
- **DOMAIN_UI (0x0005)**: UI组件日志
- **DOMAIN_SYSTEM (0x0006)**: 系统层日志
## 使用方法
### 基本操作流程:
1. **启动应用**:打开应用后自动初始化日志生成器
2. **实时生成**
- 设置合适的间隔时间
- 点击"开始生成"按钮启动连续日志生成
- 可随时点击"停止生成"结束
3. **批量生成**
- 在输入框中设置日志数量
- 选择生成Info或Error级别日志
4. **单次测试**:点击对应的功能按钮生成特定类型日志
5. **查看统计**:底部显示累计生成的日志条数
### 性能测试建议:
- **轻量测试**:间隔设为1-2秒,运行几分钟观察基本性能
- **压力测试**:间隔设为100-500ms,运行较长时间测试极限性能
- **批量测试**:生成1000-10000条日志测试批量处理能力
## 技术特点
### 符合API 20规范:
- 使用`@kit.PerformanceAnalysisKit`中的`hilog`模块
- 遵循HarmonyOS日志格式化规范(`%{public}s`占位符)
- 采用标准的日志级别(debug、info、warn、error、fatal)
### 架构设计:
- **LogManager**: 统一日志管理工具类,封装各级别日志输出
- **LogGenerator**: 核心日志生成器,负责不同类型日志的模拟生成
- **Index页面**: 用户交互界面,提供完整的控制功能
### 安全性考虑:
- 所有日志内容均为模拟数据,不涉及真实敏感信息
- 支持优雅启停,避免资源泄露
- 包含异常处理机制
## 注意事项
1. **设备存储**:大量日志可能占用较多存储空间,请注意设备剩余容量
2. **性能影响**:高频日志生成可能对应用性能产生影响
3. **电池消耗**:持续运行会增加设备耗电
4. **日志查看**:可通过DevEco Studio的Log窗口或设备日志工具查看输出
## 测试验证
项目包含独立的Node.js测试脚本(`test_log_generator.js`),可在非HarmonyOS环境下验证核心逻辑的正确性。
运行测试:
```bash
node test_log_generator.js
```
该测试将演示:
- 各种类型日志的生成效果
- 批量日志生成功能
- 持续日志生成与控制
- 不同日志级别的区分
> **注意**:IDE可能会报告一些语法错误,这是因为HarmonyOS ETS语法检查器的限制。实际在DevEco Studio中编译运行是没有问题的,测试脚本的成功运行也证明了这一点。
## 异常格式日志测试
新增的异常格式日志功能可用于测试日志系统的健壮性:
### 格式异常测试场景:
1. **参数数量不匹配**:格式字符串需要2个参数,但只提供1个
2. **参数类型不匹配**:提供字符串但格式要求数字
3. **缺少格式占位符**:有额外参数但没有对应的占位符
4. **空格式字符串**:格式字符串为空但有参数
5. **复杂嵌套错误**:混合多种格式错误
### 边界条件测试场景:
1. **超长字符串**:测试10000字符长度的日志内容
2. **极端数值**:负数、零、超大数字的处理
3. **特殊字符**:包含各种符号和中文字符
4. **空值处理**:null和undefined值的日志输出
## 扩展建议
可根据具体测试需求进一步扩展:
- 添加更多业务场景的日志模拟
- 增加日志过滤和搜索功能
- 实现日志导出功能
- 添加性能监控图表
- 增加自定义异常格式测试用例
\ No newline at end of file
// 测试异常格式日志功能
class TestAbnormalLogGenerator {
static DOMAIN_APP = 0x0001;
static TAG_APP = 'BigLogApp';
static info(domain, tag, format, ...args) {
console.log(`[INFO] [Domain:${domain}] [${tag}] ${format}`, ...args);
}
static error(domain, tag, format, ...args) {
console.log(`[ERROR] [Domain:${domain}] [${tag}] ${format}`, ...args);
}
// 模拟异常格式日志生成
static generateAbnormalFormatLogs() {
console.log('\n=== 开始测试异常格式日志 ===');
// 1. 参数数量不匹配
console.log('--- 测试1: 参数数量不匹配 ---');
try {
this.info(this.DOMAIN_APP, this.TAG_APP,
'用户%{public}s执行%{public}s操作成功', '张三');
} catch (e) {
this.error(this.DOMAIN_APP, this.TAG_APP,
'格式1异常捕获: %{public}s', e.message);
}
// 2. 参数类型不匹配
console.log('--- 测试2: 参数类型不匹配 ---');
try {
this.info(this.DOMAIN_APP, this.TAG_APP,
'HTTP请求状态码:%{public}d', 'not_a_number');
} catch (e) {
this.error(this.DOMAIN_APP, this.TAG_APP,
'格式2异常捕获: %{public}s', e.message);
}
// 3. 缺少格式占位符
console.log('--- 测试3: 缺少格式占位符 ---');
try {
this.info(this.DOMAIN_APP, this.TAG_APP,
'数据库操作完成', '额外参数会被忽略');
} catch (e) {
this.error(this.DOMAIN_APP, this.TAG_APP,
'格式3异常捕获: %{public}s', e.message);
}
// 4. 格式字符串为空
console.log('--- 测试4: 格式字符串为空 ---');
try {
this.info(this.DOMAIN_APP, this.TAG_APP,
'', '参数1', '参数2');
} catch (e) {
this.error(this.DOMAIN_APP, this.TAG_APP,
'格式4异常捕获: %{public}s', e.message);
}
// 5. 复杂嵌套格式错误
console.log('--- 测试5: 复杂嵌套格式错误 ---');
try {
this.info(this.DOMAIN_APP, this.TAG_APP,
'系统资源使用 内存:%{public}d%% CPU:%{public}d%% 磁盘:%{public}d%%',
75, 'invalid_cpu', 90);
} catch (e) {
this.error(this.DOMAIN_APP, this.TAG_APP,
'格式5异常捕获: %{public}s', e.message);
}
console.log('=== 异常格式日志测试完成 ===\n');
}
// 模拟边界条件日志生成
static generateBoundaryLogs() {
console.log('\n=== 开始测试边界条件日志 ===');
// 1. 超长字符串
const longString = 'A'.repeat(10000);
console.log('--- 测试1: 超长字符串 ---');
this.info(this.DOMAIN_APP, this.TAG_APP,
'超长日志内容长度:%{public}d 字符串预览:%{public}s',
longString.length, longString.substring(0, 100));
// 2. 极端数值
console.log('--- 测试2: 极端数值 ---');
this.info(this.DOMAIN_APP, this.TAG_APP,
'极端数值测试 负数:%{public}d 零:%{public}d 超大数:%{public}d',
-999999, 0, 999999999);
// 3. 特殊字符
console.log('--- 测试3: 特殊字符 ---');
const specialChars = '特殊字符测试: !@#$%^&*()_+-=[]{}|;:,.<>?中文测试';
this.info(this.DOMAIN_APP, this.TAG_APP,
'特殊字符日志:%{public}s', specialChars);
// 4. 空值和undefined
console.log('--- 测试4: 空值和undefined ---');
this.info(this.DOMAIN_APP, this.TAG_APP,
'空值测试 null:%{public}s undefined:%{public}s', null, undefined);
console.log('=== 边界条件日志测试完成 ===\n');
}
}
// 运行测试
console.log('开始测试异常格式日志功能...\n');
TestAbnormalLogGenerator.generateAbnormalFormatLogs();
TestAbnormalLogGenerator.generateBoundaryLogs();
console.log('所有测试完成!');
\ No newline at end of file
{
"app": {
"signingConfigs": [],
"products": [
{
"name": "default",
"signingConfig": "default",
"targetSdkVersion": "6.0.0(20)",
"compatibleSdkVersion": "6.0.0(20)",
"runtimeOS": "HarmonyOS",
"buildOption": {
"strictMode": {
"caseSensitiveCheck": true,
"useNormalizedOHMUrl": true
}
}
}
],
"buildModeSet": [
{
"name": "debug",
},
{
"name": "release"
}
]
},
"modules": [
{
"name": "entry",
"srcPath": "./entry",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
}
]
}
]
}
\ No newline at end of file
#!/bin/bash
# 清理HarmonyOS项目缓存脚本
echo "开始清理项目缓存..."
# 清理构建缓存
echo "清理build目录..."
rm -rf entry/build
rm -rf build
# 清理hvigor缓存
echo "清理hvigor缓存..."
rm -rf .hvigor
rm -rf hvigor/hvigor-cache
# 清理oh_modules
echo "清理oh_modules..."
rm -rf oh_modules
# 清理IDE相关缓存
echo "清理IDE缓存..."
find . -name "*.iml" -delete
find . -name ".idea" -type d -exec rm -rf {} + 2>/dev/null || true
echo "缓存清理完成!"
echo "建议操作:"
echo "1. 重启DevEco Studio"
echo "2. 重新导入项目"
echo "3. 重新构建项目"
\ No newline at end of file
{
"files": [
"**/*.ets"
],
"ignore": [
"**/src/ohosTest/**/*",
"**/src/test/**/*",
"**/src/mock/**/*",
"**/node_modules/**/*",
"**/oh_modules/**/*",
"**/build/**/*",
"**/.preview/**/*"
],
"ruleSet": [
"plugin:@performance/recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@security/no-unsafe-aes": "error",
"@security/no-unsafe-hash": "error",
"@security/no-unsafe-mac": "warn",
"@security/no-unsafe-dh": "error",
"@security/no-unsafe-dsa": "error",
"@security/no-unsafe-ecdsa": "error",
"@security/no-unsafe-rsa-encrypt": "error",
"@security/no-unsafe-rsa-sign": "error",
"@security/no-unsafe-rsa-key": "error",
"@security/no-unsafe-dsa-key": "error",
"@security/no-unsafe-dh-key": "error",
"@security/no-unsafe-3des": "error"
}
}
\ No newline at end of file
/node_modules
/oh_modules
/.preview
/build
/.cxx
/.test
\ No newline at end of file
{
"apiType": "stageMode",
"buildOption": {
"resOptions": {
"copyCodeResource": {
"enable": false
}
}
},
"buildOptionSet": [
{
"name": "release",
"arkOptions": {
"obfuscation": {
"ruleOptions": {
"enable": false,
"files": [
"./obfuscation-rules.txt"
]
}
}
}
},
],
"targets": [
{
"name": "default"
},
{
"name": "ohosTest",
}
]
}
\ No newline at end of file
import { hapTasks } from '@ohos/hvigor-ohos-plugin';
export default {
system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins: [] /* Custom plugin to extend the functionality of Hvigor. */
}
\ No newline at end of file
# Define project specific obfuscation rules here.
# You can include the obfuscation configuration files in the current module's build-profile.json5.
#
# For more details, see
# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5
# Obfuscation options:
# -disable-obfuscation: disable all obfuscations
# -enable-property-obfuscation: obfuscate the property names
# -enable-toplevel-obfuscation: obfuscate the names in the global scope
# -compact: remove unnecessary blank spaces and all line feeds
# -remove-log: remove all console.* statements
# -print-namecache: print the name cache that contains the mapping from the old names to new names
# -apply-namecache: reuse the given cache file
# Keep options:
# -keep-property-name: specifies property names that you want to keep
# -keep-global-name: specifies names that you want to keep in the global scope
-enable-property-obfuscation
-enable-toplevel-obfuscation
-enable-filename-obfuscation
-enable-export-obfuscation
\ No newline at end of file
{
"name": "entry",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {}
}
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
try {
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
} catch (err) {
hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err));
}
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
}
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
}
}
\ No newline at end of file
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit';
const DOMAIN = 0x0000;
export default class EntryBackupAbility extends BackupExtensionAbility {
async onBackup() {
hilog.info(DOMAIN, 'testTag', 'onBackup ok');
await Promise.resolve();
}
async onRestore(bundleVersion: BundleVersion) {
hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion));
await Promise.resolve();
}
}
\ No newline at end of file
import { LogGenerator } from '../utils/LogGenerator';
import { LogManager } from '../utils/LogManager';
@Entry
@Component
struct Index {
@State message: string = '日志生成器';
@State isGenerating: boolean = false;
@State logCount: number = 0;
@State selectedInterval: number = 1000;
@State batchCount: number = 100;
private logGenerator: LogGenerator = LogGenerator.getInstance();
aboutToAppear(): void {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '应用启动,初始化日志生成器');
}
build() {
Scroll() {
Column({ space: 20 }) {
// 标题
Text(this.message)
.fontSize(32)
.fontWeight(FontWeight.Bold)
.fontColor('#1890FF')
.margin({ top: 20 })
// 状态显示
Row() {
Text(`运行状态: ${this.isGenerating ? '运行中' : '已停止'}`)
.fontSize(18)
.fontColor(this.isGenerating ? '#52C41A' : '#FF4D4F')
}
.width('90%')
.justifyContent(FlexAlign.Start)
// 控制按钮区域
Column({ space: 15 }) {
Text('实时日志生成')
.fontSize(20)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
// 启动/停止按钮
Button(this.isGenerating ? '停止生成' : '开始生成')
.type(ButtonType.Normal)
.backgroundColor(this.isGenerating ? '#FF4D4F' : '#52C41A')
.width('80%')
.height(50)
.fontSize(18)
.onClick(() => {
this.toggleGeneration();
})
// 间隔时间选择
Row({ space: 10 }) {
Text('间隔:')
.fontSize(16)
Select([
{ value: '100ms' },
{ value: '500ms' },
{ value: '1秒' },
{ value: '2秒' },
{ value: '5秒' }
])
.value('1秒')
.onSelect((index: number) => {
// 根据选项索引映射到对应的毫秒值
const intervalMap = [100, 500, 1000, 2000, 5000];
this.selectedInterval = intervalMap[index] || 1000;
LogManager.info(LogManager.DOMAIN_UI, LogManager.TAG_UI,
'设置日志生成间隔为: %{public}d ms', this.selectedInterval);
})
}
}
.width('90%')
.padding(20)
.backgroundColor('#F5F5F5')
.borderRadius(10)
// 批量日志生成
Column({ space: 15 }) {
Text('批量日志生成')
.fontSize(20)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
// 数量输入
Row({ space: 10 }) {
Text('数量:')
.fontSize(16)
TextInput({ placeholder: '输入日志数量', text: this.batchCount.toString() })
.onChange((value: string) => {
const num = parseInt(value);
if (!isNaN(num) && num > 0) {
this.batchCount = num;
}
})
.width(120)
.type(InputType.Number)
}
// 批量生成按钮
Row({ space: 10 }) {
Button('生成Info日志')
.backgroundColor('#1890FF')
.onClick(() => {
this.generateBatchLogs('info');
})
Button('生成Error日志')
.backgroundColor('#FF4D4F')
.onClick(() => {
this.generateBatchLogs('error');
})
}
.width('100%')
.justifyContent(FlexAlign.SpaceEvenly)
}
.width('90%')
.padding(20)
.backgroundColor('#F5F5F5')
.borderRadius(10)
// 单次日志生成
Column({ space: 15 }) {
Text('单次日志测试')
.fontSize(20)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
Grid() {
ForEach([
'应用日志',
'网络日志',
'数据库日志',
'文件日志',
'系统日志',
'全部类型',
'格式异常',
'边界测试'
], (item: string) => {
GridItem() {
Button(item)
.backgroundColor(item.includes('异常') || item.includes('边界') ? '#FF7A45' : '#722ED1')
.width('90%')
.height(45)
.fontSize(14)
.onClick(() => {
this.handleGridItemClick(item);
})
}
})
}
.columnsTemplate('1fr 1fr 1fr 1fr') // 4列布局
.rowsTemplate('1fr 1fr') // 2行布局
.width('100%')
}
.width('90%')
.padding(20)
.backgroundColor('#F5F5F5')
.borderRadius(10)
// 统计信息
Column({ space: 10 }) {
Text('统计信息')
.fontSize(20)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
Row() {
Text(`累计生成: ${this.logCount} 条日志`)
.fontSize(16)
.fontColor('#666')
}
Button('清空统计')
.backgroundColor('#FAAD14')
.width('60%')
.onClick(() => {
this.logCount = 0;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '统计信息已清空');
})
}
.width('90%')
.padding(20)
.backgroundColor('#F5F5F5')
.borderRadius(10)
.margin({ bottom: 30 })
}
.width('100%')
.padding({ left: 20, right: 20 })
}
.height('100%')
.width('100%')
}
/**
* 切换日志生成状态
*/
private toggleGeneration(): void {
if (this.isGenerating) {
this.logGenerator.stopContinuousLogging();
this.isGenerating = false;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '手动停止日志生成');
} else {
this.logGenerator.startContinuousLogging(this.selectedInterval);
this.isGenerating = true;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'手动启动日志生成,间隔: %{public}d ms', this.selectedInterval);
}
}
/**
* 生成批量日志
*/
private generateBatchLogs(level: 'info' | 'error'): void {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'开始批量生成%{public}s日志,数量: %{public}d', level, this.batchCount);
this.logGenerator.generateBatchLogs(this.batchCount, level);
this.logCount += this.batchCount;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'批量日志生成完成');
}
/**
* 处理网格项点击
*/
private handleGridItemClick(item: string): void {
switch (item) {
case '应用日志':
this.logGenerator.generateAppLogs();
this.logCount += 1;
break;
case '网络日志':
this.logGenerator.generateNetworkLogs();
this.logCount += 1;
break;
case '数据库日志':
this.logGenerator.generateDatabaseLogs();
this.logCount += 1;
break;
case '文件日志':
this.logGenerator.generateFileLogs();
this.logCount += 1;
break;
case '系统日志':
this.logGenerator.generateSystemLogs();
this.logCount += 1;
break;
case '全部类型':
this.generateAllTypesOnce();
break;
case '格式异常':
LogManager.warn(LogManager.DOMAIN_APP, LogManager.TAG_APP, '格式异常测试功能暂未实现');
// this.logGenerator.generateAbnormalFormatLogs();
this.logCount += 1;
break;
case '边界测试':
LogManager.warn(LogManager.DOMAIN_APP, LogManager.TAG_APP, '边界测试功能暂未实现');
// this.logGenerator.generateBoundaryLogs();
this.logCount += 1;
break;
}
}
/**
* 生成所有类型日志一次
*/
private generateAllTypesOnce(): void {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '开始生成所有类型日志示例');
this.logGenerator.generateAppLogs();
this.logGenerator.generateNetworkLogs();
this.logGenerator.generateDatabaseLogs();
this.logGenerator.generateFileLogs();
this.logGenerator.generateSystemLogs();
this.logCount += 5;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '所有类型日志生成完成');
}
}
\ No newline at end of file
import { LogManager } from './LogManager';
/**
* 日志生成器类
* 模拟各种业务场景下的日志输出
*/
export class LogGenerator {
private static instance: LogGenerator | null = null;
private isGenerating: boolean = false;
private intervalId: number | null = null;
// 模拟数据
private static readonly USERS = ['张三', '李四', '王五', '赵六', '钱七'];
private static readonly ACTIONS = ['登录', '登出', '查询', '更新', '删除', '创建'];
private static readonly STATUS_CODES = [200, 404, 500, 403, 401];
private static readonly FILE_NAMES = ['config.json', 'data.txt', 'image.png', 'document.pdf', 'log.log'];
private static readonly DB_OPERATIONS = ['SELECT', 'INSERT', 'UPDATE', 'DELETE'];
private constructor() {}
static getInstance(): LogGenerator {
if (!LogGenerator.instance) {
LogGenerator.instance = new LogGenerator();
}
return LogGenerator.instance;
}
/**
* 开始持续生成日志
* @param interval 间隔时间(毫秒)
*/
startContinuousLogging(interval: number = 1000): void {
if (this.isGenerating) {
LogManager.warn(LogManager.DOMAIN_APP, LogManager.TAG_APP, '日志生成已在运行中');
return;
}
this.isGenerating = true;
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '开始持续生成日志,间隔: %{public}d ms', interval);
this.intervalId = setInterval(() => {
this.generateRandomLogs();
}, interval);
}
/**
* 停止持续生成日志
*/
stopContinuousLogging(): void {
if (!this.isGenerating) {
LogManager.warn(LogManager.DOMAIN_APP, LogManager.TAG_APP, '日志生成未在运行');
return;
}
this.isGenerating = false;
if (this.intervalId !== null) {
clearInterval(this.intervalId);
this.intervalId = null;
}
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP, '停止持续生成日志');
}
/**
* 生成随机日志
*/
private generateRandomLogs(): void {
const logTypes = [
() => this.generateAppLogs(),
() => this.generateNetworkLogs(),
() => this.generateDatabaseLogs(),
() => this.generateFileLogs(),
() => this.generateSystemLogs()
];
// 随机选择1-3种日志类型生成
const count = Math.floor(Math.random() * 3) + 1;
const selectedTypes = this.shuffleArray(logTypes).slice(0, count);
selectedTypes.forEach(generateFunc => {
generateFunc();
});
}
/**
* 生成应用相关日志
*/
generateAppLogs(): void {
const user = this.getRandomItem(LogGenerator.USERS);
const action = this.getRandomItem(LogGenerator.ACTIONS);
const success = Math.random() > 0.2; // 80%成功率
if (success) {
LogManager.info(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'用户%{public}s执行%{public}s操作成功', user, action);
} else {
LogManager.error(LogManager.DOMAIN_APP, LogManager.TAG_APP,
'用户%{public}s执行%{public}s操作失败', user, action);
}
}
/**
* 生成网络相关日志
*/
generateNetworkLogs(): void {
const statusCode = this.getRandomItem(LogGenerator.STATUS_CODES);
const url = `https://api.example.com/data/${Math.floor(Math.random() * 1000)}`;
if (statusCode === 200) {
LogManager.info(LogManager.DOMAIN_NETWORK, LogManager.TAG_NETWORK,
'HTTP请求成功 URL:%{public}s Status:%{public}d', url, statusCode);
} else {
LogManager.error(LogManager.DOMAIN_NETWORK, LogManager.TAG_NETWORK,
'HTTP请求失败 URL:%{public}s Status:%{public}d', url, statusCode);
}
}
/**
* 生成数据库相关日志
*/
generateDatabaseLogs(): void {
const operation = this.getRandomItem(LogGenerator.DB_OPERATIONS);
const tableName = `table_${Math.floor(Math.random() * 10)}`;
const recordCount = Math.floor(Math.random() * 100) + 1;
const duration = Math.floor(Math.random() * 500) + 10; // 10-510ms
LogManager.info(LogManager.DOMAIN_DATABASE, LogManager.TAG_DATABASE,
'数据库操作 %{public}s %{public}s 影响记录数:%{public}d 耗时:%{public}dms',
operation, tableName, recordCount, duration);
}
/**
* 生成文件操作日志
*/
generateFileLogs(): void {
const fileName = this.getRandomItem(LogGenerator.FILE_NAMES);
const operation = Math.random() > 0.5 ? '读取' : '写入';
const fileSize = Math.floor(Math.random() * 10240) + 1024; // 1KB-10KB
const success = Math.random() > 0.1; // 90%成功率
if (success) {
LogManager.info(LogManager.DOMAIN_FILE, LogManager.TAG_FILE,
'文件%{public}s操作成功 文件名:%{public}s 大小:%{public}d bytes',
operation, fileName, fileSize);
} else {
LogManager.error(LogManager.DOMAIN_FILE, LogManager.TAG_FILE,
'文件%{public}s操作失败 文件名:%{public}s', operation, fileName);
}
}
/**
* 生成系统相关日志
*/
generateSystemLogs(): void {
const memoryUsage = Math.floor(Math.random() * 80) + 10; // 10%-90%
const cpuUsage = Math.floor(Math.random() * 90) + 5; // 5%-95%
const batteryLevel = Math.floor(Math.random() * 100); // 0%-100%
LogManager.info(LogManager.DOMAIN_SYSTEM, LogManager.TAG_SYSTEM,
'系统状态监控 内存使用率:%{public}d%% CPU使用率:%{public}d%% 电池电量:%{public}d%%',
memoryUsage, cpuUsage, batteryLevel);
// 当内存使用超过80%时输出警告
if (memoryUsage > 80) {
LogManager.warn(LogManager.DOMAIN_SYSTEM, LogManager.TAG_SYSTEM,
'内存使用率过高:%{public}d%%,建议清理缓存', memoryUsage);
}
// 当电池低于20%时输出警告
if (batteryLevel < 20) {
LogManager.warn(LogManager.DOMAIN_SYSTEM, LogManager.TAG_SYSTEM,
'电池电量低:%{public}d%%,请及时充电', batteryLevel);
}
}
/**
* 批量生成指定数量的日志
* @param count 日志数量
* @param level 日志级别
*/
generateBatchLogs(count: number, level: 'debug' | 'info' | 'warn' | 'error' | 'fatal' = 'info'): void {
LogManager.batchLog(level, LogManager.DOMAIN_APP, LogManager.TAG_APP,
'批量日志生成 测试消息', count);
}
/**
* 从数组中随机获取一项
*/
private getRandomItem<T>(array: T[]): T {
return array[Math.floor(Math.random() * array.length)];
}
/**
* 数组随机排序
*/
private shuffleArray<T>(array: T[]): T[] {
const newArray = [...array];
for (let i = newArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = newArray[i];
newArray[i] = newArray[j];
newArray[j] = temp;
}
return newArray;
}
/**
* 获取当前生成状态
*/
isRunning(): boolean {
return this.isGenerating;
}
}
\ No newline at end of file
import { hilog } from '@kit.PerformanceAnalysisKit';
/**
* 日志管理工具类
* 提供统一的日志输出接口,支持不同级别和域的日志记录
*/
export class LogManager {
// 日志域定义
static readonly DOMAIN_APP = 0x0001;
static readonly DOMAIN_NETWORK = 0x0002;
static readonly DOMAIN_DATABASE = 0x0003;
static readonly DOMAIN_FILE = 0x0004;
static readonly DOMAIN_UI = 0x0005;
static readonly DOMAIN_SYSTEM = 0x0006;
// 标签定义
static readonly TAG_APP = 'BigLogApp';
static readonly TAG_NETWORK = 'NetworkService';
static readonly TAG_DATABASE = 'DatabaseOps';
static readonly TAG_FILE = 'FileHandler';
static readonly TAG_UI = 'UIComponent';
static readonly TAG_SYSTEM = 'SystemService';
/**
* 输出调试日志
* @param domain 日志域
* @param tag 标签
* @param format 格式化字符串
* @param args 参数
*/
static debug(domain: number, tag: string, format: string, ...args: (string | number | boolean)[]): void {
hilog.debug(domain, tag, format, ...args);
}
/**
* 输出信息日志
* @param domain 日志域
* @param tag 标签
* @param format 格式化字符串
* @param args 参数
*/
static info(domain: number, tag: string, format: string, ...args: (string | number | boolean)[]): void {
hilog.info(domain, tag, format, ...args);
}
/**
* 输出警告日志
* @param domain 日志域
* @param tag 标签
* @param format 格式化字符串
* @param args 参数
*/
static warn(domain: number, tag: string, format: string, ...args: (string | number | boolean)[]): void {
hilog.warn(domain, tag, format, ...args);
}
/**
* 输出错误日志
* @param domain 日志域
* @param tag 标签
* @param format 格式化字符串
* @param args 参数
*/
static error(domain: number, tag: string, format: string, ...args: (string | number | boolean)[]): void {
hilog.error(domain, tag, format, ...args);
}
/**
* 输出致命错误日志
* @param domain 日志域
* @param tag 标签
* @param format 格式化字符串
* @param args 参数
*/
static fatal(domain: number, tag: string, format: string, ...args: (string | number | boolean)[]): void {
hilog.fatal(domain, tag, format, ...args);
}
/**
* 批量输出相同类型日志
* @param level 日志级别
* @param domain 日志域
* @param tag 标签
* @param format 格式化字符串
* @param count 输出次数
* @param args 参数
*/
static batchLog(level: 'debug' | 'info' | 'warn' | 'error' | 'fatal',
domain: number,
tag: string,
format: string,
count: number,
...args: (string | number | boolean)[]): void {
for (let i = 0; i < count; i++) {
const timestamp = Date.now();
const message = `${format} [Batch:${i+1}/${count}] [Time:${timestamp}]`;
switch (level) {
case 'debug':
LogManager.debug(domain, tag, message, ...args);
break;
case 'info':
LogManager.info(domain, tag, message, ...args);
break;
case 'warn':
LogManager.warn(domain, tag, message, ...args);
break;
case 'error':
LogManager.error(domain, tag, message, ...args);
break;
case 'fatal':
LogManager.fatal(domain, tag, message, ...args);
break;
}
}
}
}
\ No newline at end of file
{
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"phone"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:layered_image",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"ohos.want.action.home"
]
}
]
}
],
"extensionAbilities": [
{
"name": "EntryBackupAbility",
"srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",
"type": "backup",
"exported": false,
"metadata": [
{
"name": "ohos.extension.backup",
"resource": "$profile:backup_config"
}
],
}
]
}
}
\ No newline at end of file
{
"color": [
{
"name": "start_window_background",
"value": "#FFFFFF"
}
]
}
\ No newline at end of file
{
"float": [
{
"name": "page_text_font_size",
"value": "50fp"
}
]
}
{
"string": [
{
"name": "module_desc",
"value": "module description"
},
{
"name": "EntryAbility_desc",
"value": "description"
},
{
"name": "EntryAbility_label",
"value": "label"
}
]
}
\ No newline at end of file
{
"layered-image":
{
"background" : "$media:background",
"foreground" : "$media:foreground"
}
}
\ No newline at end of file
{
"color": [
{
"name": "start_window_background",
"value": "#000000"
}
]
}
\ No newline at end of file
{
}
\ No newline at end of file
import { hilog } from '@kit.PerformanceAnalysisKit';
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
export default function abilityTest() {
describe('ActsAbilityTest', () => {
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
beforeAll(() => {
// Presets an action, which is performed only once before all test cases of the test suite start.
// This API supports only one parameter: preset action function.
})
beforeEach(() => {
// Presets an action, which is performed before each unit test case starts.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: preset action function.
})
afterEach(() => {
// Presets a clear action, which is performed after each unit test case ends.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: clear action function.
})
afterAll(() => {
// Presets a clear action, which is performed after all test cases of the test suite end.
// This API supports only one parameter: clear action function.
})
it('assertContain', 0, () => {
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
hilog.info(0x0000, 'testTag', '%{public}s', 'it begin');
let a = 'abc';
let b = 'b';
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
expect(a).assertContain(b);
expect(a).assertEqual(a);
})
})
}
\ No newline at end of file
import abilityTest from './Ability.test';
export default function testsuite() {
abilityTest();
}
\ No newline at end of file
{
"module": {
"name": "entry_test",
"type": "feature",
"deviceTypes": [
"phone"
],
"deliveryWithInstall": true,
"installationFree": false
}
}
import localUnitTest from './LocalUnit.test';
export default function testsuite() {
localUnitTest();
}
\ No newline at end of file
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
export default function localUnitTest() {
describe('localUnitTest', () => {
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
beforeAll(() => {
// Presets an action, which is performed only once before all test cases of the test suite start.
// This API supports only one parameter: preset action function.
});
beforeEach(() => {
// Presets an action, which is performed before each unit test case starts.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: preset action function.
});
afterEach(() => {
// Presets a clear action, which is performed after each unit test case ends.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: clear action function.
});
afterAll(() => {
// Presets a clear action, which is performed after all test cases of the test suite end.
// This API supports only one parameter: clear action function.
});
it('assertContain', 0, () => {
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
let a = 'abc';
let b = 'b';
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
expect(a).assertContain(b);
expect(a).assertEqual(a);
});
});
}
\ No newline at end of file
{
"modelVersion": "6.0.0",
"dependencies": {
},
"execution": {
// "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | "ultrafine" | false ]. Default: "normal" */
// "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */
// "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */
// "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */
// "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */
// "optimizationStrategy": "memory" /* Define the optimization strategy. Value: [ "memory" | "performance" ]. Default: "memory" */
},
"logging": {
// "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */
},
"debugging": {
// "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */
},
"nodeOptions": {
// "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/
// "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/
}
}
import { appTasks } from '@ohos/hvigor-ohos-plugin';
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins: [] /* Custom plugin to extend the functionality of Hvigor. */
}
\ No newline at end of file
{
"meta": {
"stableOrder": true,
"enableUnifiedLockfile": false
},
"lockfileVersion": 3,
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
"specifiers": {
"@ohos/hamock@1.0.0": "@ohos/hamock@1.0.0",
"@ohos/hypium@1.0.24": "@ohos/hypium@1.0.24"
},
"packages": {
"@ohos/hamock@1.0.0": {
"name": "@ohos/hamock",
"version": "1.0.0",
"integrity": "sha512-K6lDPYc6VkKe6ZBNQa9aoG+ZZMiwqfcR/7yAVFSUGIuOAhPvCJAo9+t1fZnpe0dBRBPxj2bxPPbKh69VuyAtDg==",
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hamock/-/hamock-1.0.0.har",
"registryType": "ohpm"
},
"@ohos/hypium@1.0.24": {
"name": "@ohos/hypium",
"version": "1.0.24",
"integrity": "sha512-3dCqc+BAR5LqEGG2Vtzi8O3r7ci/3fYU+FWjwvUobbfko7DUnXGOccaror0yYuUhJfXzFK0aZNMGSnXaTwEnbw==",
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.24.har",
"registryType": "ohpm"
}
}
}
\ No newline at end of file
{
"modelVersion": "6.0.0",
"description": "Please describe the basic information.",
"dependencies": {
},
"devDependencies": {
"@ohos/hypium": "1.0.24",
"@ohos/hamock": "1.0.0"
}
}
// 测试异常格式日志功能
class TestAbnormalLogGenerator {
static DOMAIN_APP = 0x0001;
static TAG_APP = 'BigLogApp';
static info(domain, tag, format, ...args) {
console.log(`[INFO] [Domain:${domain}] [${tag}] ${format}`, ...args);
}
static error(domain, tag, format, ...args) {
console.log(`[ERROR] [Domain:${domain}] [${tag}] ${format}`, ...args);
}
// 模拟异常格式日志生成
static generateAbnormalFormatLogs() {
console.log('\n=== 开始测试异常格式日志 ===');
// 1. 参数数量不匹配
console.log('--- 测试1: 参数数量不匹配 ---');
try {
this.info(this.DOMAIN_APP, this.TAG_APP,
'用户%{public}s执行%{public}s操作成功', '张三');
} catch (e) {
this.error(this.DOMAIN_APP, this.TAG_APP,
'格式1异常捕获: %{public}s', e.message);
}
// 2. 参数类型不匹配
console.log('--- 测试2: 参数类型不匹配 ---');
try {
this.info(this.DOMAIN_APP, this.TAG_APP,
'HTTP请求状态码:%{public}d', 'not_a_number');
} catch (e) {
this.error(this.DOMAIN_APP, this.TAG_APP,
'格式2异常捕获: %{public}s', e.message);
}
// 3. 缺少格式占位符
console.log('--- 测试3: 缺少格式占位符 ---');
try {
this.info(this.DOMAIN_APP, this.TAG_APP,
'数据库操作完成', '额外参数会被忽略');
} catch (e) {
this.error(this.DOMAIN_APP, this.TAG_APP,
'格式3异常捕获: %{public}s', e.message);
}
// 4. 格式字符串为空
console.log('--- 测试4: 格式字符串为空 ---');
try {
this.info(this.DOMAIN_APP, this.TAG_APP,
'', '参数1', '参数2');
} catch (e) {
this.error(this.DOMAIN_APP, this.TAG_APP,
'格式4异常捕获: %{public}s', e.message);
}
// 5. 复杂嵌套格式错误
console.log('--- 测试5: 复杂嵌套格式错误 ---');
try {
this.info(this.DOMAIN_APP, this.TAG_APP,
'系统资源使用 内存:%{public}d%% CPU:%{public}d%% 磁盘:%{public}d%%',
75, 'invalid_cpu', 90);
} catch (e) {
this.error(this.DOMAIN_APP, this.TAG_APP,
'格式5异常捕获: %{public}s', e.message);
}
console.log('=== 异常格式日志测试完成 ===\n');
}
// 模拟边界条件日志生成
static generateBoundaryLogs() {
console.log('\n=== 开始测试边界条件日志 ===');
// 1. 超长字符串
const longString = 'A'.repeat(10000);
console.log('--- 测试1: 超长字符串 ---');
this.info(this.DOMAIN_APP, this.TAG_APP,
'超长日志内容长度:%{public}d 字符串预览:%{public}s',
longString.length, longString.substring(0, 100));
// 2. 极端数值
console.log('--- 测试2: 极端数值 ---');
this.info(this.DOMAIN_APP, this.TAG_APP,
'极端数值测试 负数:%{public}d 零:%{public}d 超大数:%{public}d',
-999999, 0, 999999999);
// 3. 特殊字符
console.log('--- 测试3: 特殊字符 ---');
const specialChars = '特殊字符测试: !@#$%^&*()_+-=[]{}|;:,.<>?中文测试';
this.info(this.DOMAIN_APP, this.TAG_APP,
'特殊字符日志:%{public}s', specialChars);
// 4. 空值和undefined
console.log('--- 测试4: 空值和undefined ---');
this.info(this.DOMAIN_APP, this.TAG_APP,
'空值测试 null:%{public}s undefined:%{public}s', null, undefined);
console.log('=== 边界条件日志测试完成 ===\n');
}
}
// 运行测试
console.log('开始测试异常格式日志功能...\n');
TestAbnormalLogGenerator.generateAbnormalFormatLogs();
TestAbnormalLogGenerator.generateBoundaryLogs();
console.log('所有测试完成!');
\ No newline at end of file
// 模拟测试日志生成器的核心逻辑
// 这个脚本用于验证我们创建的日志生成功能
class MockHilog {
static debug(domain, tag, format, ...args) {
console.log(`[DEBUG] [Domain:${domain}] [${tag}] ${this.formatMessage(format, args)}`);
}
static info(domain, tag, format, ...args) {
console.log(`[INFO] [Domain:${domain}] [${tag}] ${this.formatMessage(format, args)}`);
}
static warn(domain, tag, format, ...args) {
console.log(`[WARN] [Domain:${domain}] [${tag}] ${this.formatMessage(format, args)}`);
}
static error(domain, tag, format, ...args) {
console.log(`[ERROR] [Domain:${domain}] [${tag}] ${this.formatMessage(format, args)}`);
}
static fatal(domain, tag, format, ...args) {
console.log(`[FATAL] [Domain:${domain}] [${tag}] ${this.formatMessage(format, args)}`);
}
static formatMessage(format, args) {
let message = format;
args.forEach((arg, index) => {
const placeholder = `%{public}s`;
message = message.replace(placeholder, arg);
});
return message;
}
}
class MockLogManager {
static DOMAIN_APP = 0x0001;
static DOMAIN_NETWORK = 0x0002;
static DOMAIN_DATABASE = 0x0003;
static DOMAIN_FILE = 0x0004;
static DOMAIN_UI = 0x0005;
static DOMAIN_SYSTEM = 0x0006;
static TAG_APP = 'BigLogApp';
static TAG_NETWORK = 'NetworkService';
static TAG_DATABASE = 'DatabaseOps';
static TAG_FILE = 'FileHandler';
static TAG_UI = 'UIComponent';
static TAG_SYSTEM = 'SystemService';
static debug(domain, tag, format, ...args) {
MockHilog.debug(domain, tag, format, ...args);
}
static info(domain, tag, format, ...args) {
MockHilog.info(domain, tag, format, ...args);
}
static warn(domain, tag, format, ...args) {
MockHilog.warn(domain, tag, format, ...args);
}
static error(domain, tag, format, ...args) {
MockHilog.error(domain, tag, format, ...args);
}
static fatal(domain, tag, format, ...args) {
MockHilog.fatal(domain, tag, format, ...args);
}
static batchLog(level, domain, tag, format, count, ...args) {
for (let i = 0; i < count; i++) {
const timestamp = Date.now();
const message = `${format} [Batch:${i+1}/${count}] [Time:${timestamp}]`;
switch (level) {
case 'debug':
this.debug(domain, tag, message, ...args);
break;
case 'info':
this.info(domain, tag, message, ...args);
break;
case 'warn':
this.warn(domain, tag, message, ...args);
break;
case 'error':
this.error(domain, tag, message, ...args);
break;
case 'fatal':
this.fatal(domain, tag, message, ...args);
break;
}
}
}
}
class MockLogGenerator {
constructor() {
this.isGenerating = false;
this.intervalId = null;
}
static USERS = ['张三', '李四', '王五', '赵六', '钱七'];
static ACTIONS = ['登录', '登出', '查询', '更新', '删除', '创建'];
static STATUS_CODES = [200, 404, 500, 403, 401];
static FILE_NAMES = ['config.json', 'data.txt', 'image.png', 'document.pdf', 'log.log'];
static DB_OPERATIONS = ['SELECT', 'INSERT', 'UPDATE', 'DELETE'];
startContinuousLogging(interval = 1000) {
if (this.isGenerating) {
MockLogManager.warn(MockLogManager.DOMAIN_APP, MockLogManager.TAG_APP, '日志生成已在运行中');
return;
}
this.isGenerating = true;
MockLogManager.info(MockLogManager.DOMAIN_APP, MockLogManager.TAG_APP, '开始持续生成日志,间隔: %{public}d ms', interval);
this.intervalId = setInterval(() => {
this.generateRandomLogs();
}, interval);
}
stopContinuousLogging() {
if (!this.isGenerating) {
MockLogManager.warn(MockLogManager.DOMAIN_APP, MockLogManager.TAG_APP, '日志生成未在运行');
return;
}
this.isGenerating = false;
if (this.intervalId !== null) {
clearInterval(this.intervalId);
this.intervalId = null;
}
MockLogManager.info(MockLogManager.DOMAIN_APP, MockLogManager.TAG_APP, '停止持续生成日志');
}
generateRandomLogs() {
const logTypes = [
() => this.generateAppLogs(),
() => this.generateNetworkLogs(),
() => this.generateDatabaseLogs(),
() => this.generateFileLogs(),
() => this.generateSystemLogs()
];
const count = Math.floor(Math.random() * 3) + 1;
const selectedTypes = this.shuffleArray(logTypes).slice(0, count);
selectedTypes.forEach(generateFunc => {
generateFunc();
});
}
generateAppLogs() {
const user = this.getRandomItem(MockLogGenerator.USERS);
const action = this.getRandomItem(MockLogGenerator.ACTIONS);
const success = Math.random() > 0.2;
if (success) {
MockLogManager.info(MockLogManager.DOMAIN_APP, MockLogManager.TAG_APP,
'用户%{public}s执行%{public}s操作成功', user, action);
} else {
MockLogManager.error(MockLogManager.DOMAIN_APP, MockLogManager.TAG_APP,
'用户%{public}s执行%{public}s操作失败', user, action);
}
}
generateNetworkLogs() {
const statusCode = this.getRandomItem(MockLogGenerator.STATUS_CODES);
const url = `https://api.example.com/data/${Math.floor(Math.random() * 1000)}`;
if (statusCode === 200) {
MockLogManager.info(MockLogManager.DOMAIN_NETWORK, MockLogManager.TAG_NETWORK,
'HTTP请求成功 URL:%{public}s Status:%{public}d', url, statusCode);
} else {
MockLogManager.error(MockLogManager.DOMAIN_NETWORK, MockLogManager.TAG_NETWORK,
'HTTP请求失败 URL:%{public}s Status:%{public}d', url, statusCode);
}
}
generateDatabaseLogs() {
const operation = this.getRandomItem(MockLogGenerator.DB_OPERATIONS);
const tableName = `table_${Math.floor(Math.random() * 10)}`;
const recordCount = Math.floor(Math.random() * 100) + 1;
const duration = Math.floor(Math.random() * 500) + 10;
MockLogManager.info(MockLogManager.DOMAIN_DATABASE, MockLogManager.TAG_DATABASE,
'数据库操作 %{public}s %{public}s 影响记录数:%{public}d 耗时:%{public}dms',
operation, tableName, recordCount, duration);
}
generateFileLogs() {
const fileName = this.getRandomItem(MockLogGenerator.FILE_NAMES);
const operation = Math.random() > 0.5 ? '读取' : '写入';
const fileSize = Math.floor(Math.random() * 10240) + 1024;
const success = Math.random() > 0.1;
if (success) {
MockLogManager.info(MockLogManager.DOMAIN_FILE, MockLogManager.TAG_FILE,
'文件%{public}s操作成功 文件名:%{public}s 大小:%{public}d bytes',
operation, fileName, fileSize);
} else {
MockLogManager.error(MockLogManager.DOMAIN_FILE, MockLogManager.TAG_FILE,
'文件%{public}s操作失败 文件名:%{public}s', operation, fileName);
}
}
generateSystemLogs() {
const memoryUsage = Math.floor(Math.random() * 80) + 10;
const cpuUsage = Math.floor(Math.random() * 90) + 5;
const batteryLevel = Math.floor(Math.random() * 100);
MockLogManager.info(MockLogManager.DOMAIN_SYSTEM, MockLogManager.TAG_SYSTEM,
'系统状态监控 内存使用率:%{public}d%% CPU使用率:%{public}d%% 电池电量:%{public}d%%',
memoryUsage, cpuUsage, batteryLevel);
if (memoryUsage > 80) {
MockLogManager.warn(MockLogManager.DOMAIN_SYSTEM, MockLogManager.TAG_SYSTEM,
'内存使用率过高:%{public}d%%,建议清理缓存', memoryUsage);
}
if (batteryLevel < 20) {
MockLogManager.warn(MockLogManager.DOMAIN_SYSTEM, MockLogManager.TAG_SYSTEM,
'电池电量低:%{public}d%%,请及时充电', batteryLevel);
}
}
generateBatchLogs(count, level = 'info') {
MockLogManager.batchLog(level, MockLogManager.DOMAIN_APP, MockLogManager.TAG_APP,
'批量日志生成 测试消息', count);
}
getRandomItem(array) {
return array[Math.floor(Math.random() * array.length)];
}
shuffleArray(array) {
const newArray = [...array];
for (let i = newArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[newArray[i], newArray[j]] = [newArray[j], newArray[i]];
}
return newArray;
}
isRunning() {
return this.isGenerating;
}
}
// 测试函数
function runTests() {
console.log('=== 开始测试日志生成器 ===\n');
const generator = new MockLogGenerator();
// 测试1: 单次生成各种类型日志
console.log('--- 测试1: 单次生成各种类型日志 ---');
generator.generateAppLogs();
generator.generateNetworkLogs();
generator.generateDatabaseLogs();
generator.generateFileLogs();
generator.generateSystemLogs();
console.log('\n--- 测试2: 批量生成日志 ---');
generator.generateBatchLogs(5, 'info');
console.log('\n--- 测试3: 错误级别日志 ---');
generator.generateBatchLogs(3, 'error');
console.log('\n--- 测试4: 启动持续生成 ---');
generator.startContinuousLogging(500); // 500ms间隔
// 运行5秒后停止
setTimeout(() => {
console.log('\n--- 测试5: 停止持续生成 ---');
generator.stopContinuousLogging();
console.log('\n=== 测试完成 ===');
}, 5000);
}
// 运行测试
runTests();
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment