• Home
Name Date Size #Lines LOC

..--

AppScope/06-May-2025-3633

entry/06-May-2025-727661

hvigor/06-May-2025-2422

log/06-May-2025-473368

screenshots/device/06-May-2025-

.gitignoreD06-May-2025145 1211

README.mdD06-May-20251.5 KiB8057

README_zh.mdD06-May-20252.3 KiB6143

build-profile.json5D06-May-20251.1 KiB4745

hvigorfile.tsD06-May-2025763 171

hvigorwD06-May-20252.1 KiB6228

hvigorw.batD06-May-20252 KiB7256

oh-package.json5D06-May-2025812 2725

ohosTest.mdD06-May-2025381 86

README.md

1# Log
2
3## Introduction
4
5This sample shows how to use log APIs to print log information and save the information to the application installation directory.
6
7## APIs
8
9```
10    import logger from '@ohos/log'
11    import { LogLevel } from '@ohos/log'
12    import { Configure }  from '@ohos/log'
13```
14
15## Usage Instruction
16
171. Configure log printing parameters.
18
19```
20  Configure = {
21    cheese: {
22        types: string[],
23        filename: string
24    }
25    defaults: {
26        appenders: string,
27        level: LogLevel
28    }
29  }
30  // If types is set to file, logs are saved to the file named by filename. appenders indicates the log tag, and level indicates the lowest log level.
31```
32
332. Initialize a **logger** instance.
34
35```
36   logger.setConfigure(configure: Configure)
37   // configure indicates the parameter configuration.
38```
39
403. Print log information of the debug level.
41
42```
43   logger.debug(value)
44   // value indicates the log content.
45```
46
474. Print log information of the info level.
48
49```
50   logger.info(value)
51   // value indicates the log content.
52```
53
545. Print log information of the warn level.
55
56```
57   logger.warn(value)
58   // value indicates the log content.
59```
60
616. Print log information of the error level.
62
63```
64   logger.error(value)
65   // value indicates the log content.
66```
67
687. Print log information of the fatal level.
69
70```
71   logger.fatal(value, bool)
72   // value indicates the log content.
73```
74
75## Constraints
76
771. This sample requires API version 9 or later.
78
792. This sample requires DevEco Studio 3.1 Canary1 (Build Version: 3.1.0.100) to compile and run.
80

README_zh.md

1# 日志打印
2
3### 介绍
4
5本示例使用[hilog日志系统](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-performance-analysis-kit/js-apis-hilog.md) ,提供日志打印类,使应用/服务可以按照指定级别、标识和格式字符串输出日志内容,帮助开发者了解应用/服务的运行状态,更好地调试程序。
6
7### 效果预览
8|首页|
9|---------|
10|![image](screenshots/device/index.png)|
11
12使用说明:
13
141.进入应用会自动生成一个空的日志文件。
15
162.点击log按钮即可输出日志,并将日志生成到日志文件当中。
17
18### 工程目录
19```
20entry/src/main/ets/
21|---pages
22|   |---Index.ets                           // 首页
23log/src/main/ets/
24|---components
25|   |---mainpage
26|   |   |---Configure.ets                   // 日志打印配置项
27|   |   |---Logger.ets                      // 日志打印类
28|   |   |---LoggerModel.ts                  // 封装日志打印类
29|   |   |---LogLevel.ts                     // 日志等级枚举值
30```
31
32### 具体实现
33+ 日志输出功能封装在Logger,源码参考:[Logger.ets](log/src/main/ets/components/mainpage/Logger.ets):
34    + 日志输出:Logger类根据Configure的types参数将日志分为三个类型,其中file类型会将日志写入本地文件,console类型调用ConsoleLoggerStrategy类输出,hilog类型调用HilogLoggerStrategy类输出;
35    + 日志文件写入本地:FileLoggerStrategy类使用[@ohos.file.fs](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-file-fs.md) 将日志写入本地文件中,本示例只是展示了文件写入文件操作的使用方法,在实战场景中,建议把耗时操作放入子线程中运行。
36
37### 相关权限
38
39不涉及。
40
41### 依赖
42
43不涉及。
44
45### 约束与限制
46
471.本示例已适配API version 9版本SDK,版本号:3.2.11.9;
48
492.本示例需要使用DevEco Studio 3.1 Beta2 (Build Version: 3.1.0.400 构建 2023年4月7日)及以上版本才可编译运行。
50
51### 下载
52如需单独下载本工程,执行如下命令:
53```
54git init
55git config core.sparsecheckout true
56echo code/BasicFeature/DFX/Logger/ > .git/info/sparse-checkout
57git remote add origin https://gitee.com/openharmony/applications_app_samples.git
58git pull origin master
59```
60
61