README.md
1# HiLog<a name="EN-US_TOPIC_0000001115694144"></a>
2
3- [Introduction](#section11660541593)
4- [Directory Structure](#section161941989596)
5- [Constraints](#section119744591305)
6- [Usage](#section06487425716)
7 - [Available APIs](#section1551164914237)
8 - [Usage Guidelines](#section129654513264)
9
10- [Repositories Involved](#section177639411669)
11
12## Introduction<a name="section11660541593"></a>
13
14HiLog is the module of OpenHarmony that logs information such as user operations and system running status for the system framework, services, and applications.
15
16**Figure 1** Architecture of the HiLog module<a name="fig4460722185514"></a>
17
18
19![](figures/en-us_image_0000001115534242.png)
20
21User-space processes write logs to the ring buffer of hilogd through the log API. hilogd sends logs to the console for viewing or writes logs to the flash at the request of the hilog.
22
23The details are as follows:
24
25- **hilogd**: log service in the user space
26
271. hilogd is a resident service and is enabled by default when the system is started.
282. User-space processes call the log APIs to transfer the formatted log content to hilogd and store the content in its ring buffer.
29
30- **hilog**: command line tool for viewing logs
31
321. hilog reads the log content from the ring buffer of hilogd, filters the content if required, and then prints the content to the standard output.
33
34Supported functions:
35
36- Formatting of parameter privacy identifiers \(See the following example for details.\)
37- Flow control on excess log processes
38- Flow control on excess domains \(subsystems or modules\) involved in log printing
39- Log compression and flushing
40
41## Directory Structure<a name="section161941989596"></a>
42
43```
44/base/hiviewdfx/hilog
45├── frameworks # Framework code
46│ └── native # HiLog Native implementation code
47├── interfaces # APIs
48│ └── native # External C/C++ APIs
49│ └── innerkits # Header files opened to internal subsystems
50│ └── kits # Header files opened to applications
51│ └── js # External js APIs
52├── services
53│ └── hilogd # HiLog resident service implementation
54│ └── hilogtool # HiLog tool implementation
55```
56
57## Constraints<a name="section119744591305"></a>
58
59Clang 8.0.0 or later is required.
60
61## Usage<a name="section06487425716"></a>
62
63### Available APIs<a name="section1551164914237"></a>
64
65**Table 1** Major APIs of HiLog
66
67<a name="table5489165165714"></a>
68<table><thead align="left"><tr id="row12490195195718"><th class="cellrowborder" valign="top" width="14.09%" id="mcps1.2.4.1.1"><p id="p862411212488"><a name="p862411212488"></a><a name="p862411212488"></a><strong id="b68190161218"><a name="b68190161218"></a><a name="b68190161218"></a>Class</strong></p>
69</th>
70<th class="cellrowborder" valign="top" width="33.98%" id="mcps1.2.4.1.2"><p id="p10859172921116"><a name="p10859172921116"></a><a name="p10859172921116"></a>Method</p>
71</th>
72<th class="cellrowborder" valign="top" width="51.93%" id="mcps1.2.4.1.3"><p id="p104901150576"><a name="p104901150576"></a><a name="p104901150576"></a>Description</p>
73</th>
74</tr>
75</thead>
76<tbody><tr id="row34901758577"><td class="cellrowborder" valign="top" width="14.09%" headers="mcps1.2.4.1.1 "><p id="p16501115918497"><a name="p16501115918497"></a><a name="p16501115918497"></a>HiLogLabel</p>
77</td>
78<td class="cellrowborder" valign="top" width="33.98%" headers="mcps1.2.4.1.2 "><p id="p1550175974917"><a name="p1550175974917"></a><a name="p1550175974917"></a>HiLogLabel(int type, int domain, String tag)</p>
79</td>
80<td class="cellrowborder" valign="top" width="51.93%" headers="mcps1.2.4.1.3 "><p id="p135021859104915"><a name="p135021859104915"></a><a name="p135021859104915"></a>Constructs a log label, including the log type, domain, and tag.</p>
81</td>
82</tr>
83<tr id="row868117162916"><td class="cellrowborder" valign="top" width="14.09%" headers="mcps1.2.4.1.1 "><p id="p1215914385013"><a name="p1215914385013"></a><a name="p1215914385013"></a>HiLog</p>
84</td>
85<td class="cellrowborder" valign="top" width="33.98%" headers="mcps1.2.4.1.2 "><p id="p1415914345011"><a name="p1415914345011"></a><a name="p1415914345011"></a>Info(const HiLogLabel &label, const char *fmt, ...)</p>
86</td>
87<td class="cellrowborder" valign="top" width="51.93%" headers="mcps1.2.4.1.3 "><p id="p1274814305510"><a name="p1274814305510"></a><a name="p1274814305510"></a>Prints logs of the info level.</p>
88</td>
89</tr>
90</tbody>
91</table>
92
93### Usage Guidelines<a name="section129654513264"></a>
94
951. View logs.
96
97Run the **hilog | grep "MY\_TAG"** command in shell mode.
98
99```
100Output in debug mode:
10101-26 11:01:06.870 1051 1051 W 00201/test: Failed to visit [https://gitee.com/openharmony](https://gitee.com/openharmony), reason:503.
102Output in release mode:
10301-26 11:01:06.870 1051 1051 W 00201/test: Failed to visit <private>, reason:503.
104```
105
106## Repositories Involved<a name="section177639411669"></a>
107
108[DFX subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/dfx.md)
109
110[hiviewdfx\_hiview](https://gitee.com/openharmony/hiviewdfx_hiview/blob/master/README.md)
111
112**hiviewdfx\_hilog**
113
114[hiviewdfx\_hiappevent](https://gitee.com/openharmony/hiviewdfx_hiappevent/blob/master/README.md)
115
116[hiviewdfx\_hisysevent](https://gitee.com/openharmony/hiviewdfx_hisysevent/blob/master/README.md)
117
118[hiviewdfx\_faultloggerd](https://gitee.com/openharmony/hiviewdfx_faultloggerd/blob/master/README.md)
119
120[hiviewdfx\_hilog\_lite](https://gitee.com/openharmony/hiviewdfx_hilog_lite/blob/master/README.md)
121
122[hiviewdfx\_hievent\_lite](https://gitee.com/openharmony/hiviewdfx_hievent_lite/blob/master/README.md)
123
124[hiviewdfx\_hiview\_lite](https://gitee.com/openharmony/hiviewdfx_hiview_lite/blob/master/README.md)
125
126
README_zh.md
1# HiLog组件<a name="ZH-CN_TOPIC_0000001115694144"></a>
2
3- [HiLog组件<a name="ZH-CN_TOPIC_0000001115694144"></a>](#hilog组件)
4 - [简介<a name="section11660541593"></a>](#简介)
5 - [目录<a name="section161941989596"></a>](#目录)
6 - [约束<a name="section119744591305"></a>](#约束)
7 - [说明<a name="section06487425716"></a>](#说明)
8 - [接口说明<a name="section1551164914237"></a>](#接口说明)
9 - [使用说明<a name="section129654513264"></a>](#使用说明)
10 - [涉及仓<a name="section177639411669"></a>](#涉及仓)
11
12- [涉及仓](#section177639411669)
13
14## 简介<a name="section11660541593"></a>
15
16HiLog是OpenHarmony日志系统,提供给系统框架、服务、以及应用打印日志,记录用户操作、系统运行状态等。
17
18**图 1** HiLog架构图<a name="fig4460722185514"></a>
19
20
21![](figures/zh-cn_image_0000001115534242.png)
22
23用户态Process通过日志接口将日志内容写入hilogd buffer中,用户态的hilog工具支持将日志输出到控制台(console)进行查看,同时也支持通过hilog工具给hilogd发送命令将日志落盘。
24
25下述主要任务的详细内容:
26
27- hilogd是流水日志的用户态服务。
28
291. 此功能是常驻服务,在研发版本系统启动时默认启动。
302. 当用户态模块调用日志接口,将格式化好的日志内容传输给该任务,并将其存储在一个环形缓冲区中 。
31
32- hilog日志查看命令行工具
33
341. 从hilogd读取ringbuffer内容,输出到标准输出,可支持日志过滤。
35
36支持特性:
37
38- 支持参数隐私标识格式化(详见下面举例)。
39- 支持对超标日志打印进程流控。
40- 支持对超标日志打印domain\(标识子系统/模块\)流控。
41- 支持流压缩落盘。
42
43## 目录<a name="section161941989596"></a>
44
45```
46/base/hiviewdfx/hilog
47├── frameworks # 框架代码
48│ └── native # HiLog native实现代码
49├── interfaces # 接口
50│ └── native # 对外C/C++接口
51│ └── innerkits # 对内部子系统暴露的头文件
52│ └── kits # 对应用暴露的头文件
53│ └── js # 对外js接口
54├── services
55│ └── hilogd # 日志常驻服务实现
56│ └── hilogtool # 日志工具实现
57```
58
59## 约束<a name="section119744591305"></a>
60
61依赖 Clang 编译器\(**Clang** 8.0.0 \)及以上。
62
63## 说明<a name="section06487425716"></a>
64
65### 接口说明<a name="section1551164914237"></a>
66
67**表 1** 主要API说明
68
69<a name="table5489165165714"></a>
70<table><thead align="left"><tr id="row12490195195718"><th class="cellrowborder" valign="top" width="14.09%" id="mcps1.2.4.1.1"><p id="p862411212488"><a name="p862411212488"></a><a name="p862411212488"></a>类</p>
71</th>
72<th class="cellrowborder" valign="top" width="33.98%" id="mcps1.2.4.1.2"><p id="p10859172921116"><a name="p10859172921116"></a><a name="p10859172921116"></a>方法</p>
73</th>
74<th class="cellrowborder" valign="top" width="51.93%" id="mcps1.2.4.1.3"><p id="p104901150576"><a name="p104901150576"></a><a name="p104901150576"></a>描述</p>
75</th>
76</tr>
77</thead>
78<tbody><tr id="row34901758577"><td class="cellrowborder" valign="top" width="14.09%" headers="mcps1.2.4.1.1 "><p id="p16501115918497"><a name="p16501115918497"></a><a name="p16501115918497"></a>HiLogLabel</p>
79</td>
80<td class="cellrowborder" valign="top" width="33.98%" headers="mcps1.2.4.1.2 "><p id="p1550175974917"><a name="p1550175974917"></a><a name="p1550175974917"></a>HiLogLabel(int type, int domain, String tag)</p>
81</td>
82<td class="cellrowborder" valign="top" width="51.93%" headers="mcps1.2.4.1.3 "><p id="p135021859104915"><a name="p135021859104915"></a><a name="p135021859104915"></a>构造日志标签,包括:日志类型、领域标识、日志tag。</p>
83</td>
84</tr>
85<tr id="row868117162916"><td class="cellrowborder" valign="top" width="14.09%" headers="mcps1.2.4.1.1 "><p id="p1215914385013"><a name="p1215914385013"></a><a name="p1215914385013"></a>HiLog</p>
86</td>
87<td class="cellrowborder" valign="top" width="33.98%" headers="mcps1.2.4.1.2 "><p id="p1415914345011"><a name="p1415914345011"></a><a name="p1415914345011"></a>Info(const HiLogLabel &label, const char *fmt, ...)</p>
88</td>
89<td class="cellrowborder" valign="top" width="51.93%" headers="mcps1.2.4.1.3 "><p id="p1274814305510"><a name="p1274814305510"></a><a name="p1274814305510"></a>info级别日志打印接口。</p>
90</td>
91</tr>
92</tbody>
93</table>
94
95### 使用说明<a name="section129654513264"></a>
96
971. 查看日志
98
99通过shell , 执行命令 hilog | grep “MY\_TAG”。
100
101```
102Debug模式输出:
10301-26 11:01:06.870 1051 1051 W 00201/test: Failed to visit [https://gitee.com/openharmony](https://gitee.com/openharmony), reason:503.
104release模式输出:
10501-26 11:01:06.870 1051 1051 W 00201/test: Failed to visit <private>, reason:503.
106```
1072. hilog命令行使用说明
108
109| 短选项 | 长选项 | 参数 | 说明 |
110|----------------------------------------------------------------------------------------------|----------------------|---------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|
111| -h | --help | | 帮助命令 |
112| 缺省 | 缺省 | | 阻塞读日志,不退出 |
113| -x | --exit | | 非阻塞读日志,读完退出 |
114| -g | | | 查询buffer的大小,配合-t指定某一类型使用,默认app和core |
115| -G | --buffer-size | \<size> | 设置指定\<type>日志类型缓冲区的大小,配合-t指定某一类型使用,默认app和core, 可使用B/K/M/G为单位 |
116| -r | | | 清除buffer日志,配合-t指定某一类型使用,默认app和core |
117| -p | --privacy | <on/off> | 支持系统调试时日志隐私开关控制 |
118| | | on | 打开隐私开关,显示\<private> |
119| | | off | 关闭隐私开关,显示明文 |
120| -k | | <on/off> | Kernel日志读取开关控制 |
121| | | on | 打开读取kernel日志 |
122| | | off | 关闭读取kernel日志 |
123| -s | --statistics | | 查询统计信息,需配合-t或-D使用 |
124| -S | | | 清除统计信息,需配合-t或-D使用 |
125| -Q | | \<control-type> | 流控缺省配额开关控制 |
126| | | pidon | 进程流控开关打开 |
127| | | pidoff | 进程流控开关关闭 |
128| | | domainon | domain流控开关打开 |
129| | | domainoff | domain流控开关关闭 |
130| -L | --level | \<level> | 指定级别的日志,示例:-L D/I/W/E/F |
131| -t | --type | \<type> | 指定类型的日志,示例:-t app core init |
132| -D | --domain | \<domain> | 指定domain |
133| -T | --Tag | \<tag> | 指定tag |
134| -a | --head | \<n> | 只显示前\<n>行日志 |
135| -z | --tail | \<n> | 只显示后\<n>行日志 |
136| -P | --pid | \<pid> | 标识不同的pid |
137| -e | --regex | \<expr> | 只打印日志消息与\<expr>匹配的行,其中\<expr>是一个正则表达式 |
138| -f | --filename | \<filename> | 设置落盘的文件名 |
139| -l | --length | \<length> | 设置落盘的文件大小,需要大于等于64K |
140| -n | --number | \<number> | 设置落盘文件的个数 |
141| -j | --jobid | \<jobid> | 设置落盘任务的ID |
142| -w | --write | \<control> | 落盘任务控制 |
143| | | query | 落盘任务查询 |
144| | | start | 落盘任务开始,命令行参数为文件名、单文件大小、落盘算法、rotate文件数目. |
145| | | stop | 落盘任务停止 |
146| -m | --stream | \<algorithm> | 落盘方式控制 |
147| | | none | 无压缩方式落盘 |
148| | | zlib | zlib压缩算法落盘,落盘文件为.gz |
149| | | zstd | zstd压缩算法落盘,落盘文件为.zst |
150| -v | --format | \<format> | |
151| | | time | 显示本地时间 |
152| | | color | 显示不同级别显示不同颜色,参数缺省级别颜色模式处理(按黑白方式) |
153| | | epoch | 显示相对1970时间 |
154| | | monotonic | 显示相对启动时间 |
155| | | usec | 显示微秒精度时间 |
156| | | nsec | 显示纳秒精度时间 |
157| | | year | 显示将年份添加到显示的时间 |
158| | | zone | 显示将本地时区添加到显示的时间 |
159| -b | --baselevel | \<loglevel> | 设置可打印日志的最低等级:D(DEBUG)/I(INFO)/W(WARN)/E(ERROR)/F(FATAL) |
160
161```
162示例:hilog -G 2M -t core
163解释:设置buffer大小为2M,日志类型为core。
164示例:hilog -g -t app
165解释:查询日志类型为app的buffer大小。
166示例:hilog -w start -f "hilog" -l 66k -m zlib -n 10
167解释:执行名字为hilog的落盘任务,10个落盘文件进行轮转,单个文件落盘大小为66k,采用zlib压缩算法。
168type、level、domain、tag支持排除查询,排除查询可以使用以"^"开头的参数和分隔符","."来完成
169示例:hilog -t ^core,app 排除core和app类型的日志,可以与其他参数一起使用。
170示例:hilog -t app core 打印core和app类型的日志,可以与其他参数一起使用。
171```
172
173## 涉及仓<a name="section177639411669"></a>
174
175[DFX子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/DFX%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
176
177[hiviewdfx\_hiview](https://gitee.com/openharmony/hiviewdfx_hiview/blob/master/README_zh.md)
178
179**hiviewdfx\_hilog**
180
181[hiviewdfx\_hiappevent](https://gitee.com/openharmony/hiviewdfx_hiappevent/blob/master/README_zh.md)
182
183[hiviewdfx\_hisysevent](https://gitee.com/openharmony/hiviewdfx_hisysevent/blob/master/README_zh.md)
184
185[hiviewdfx\_faultloggerd](https://gitee.com/openharmony/hiviewdfx_faultloggerd/blob/master/README_zh.md)
186
187[hiviewdfx\_hilog\_lite](https://gitee.com/openharmony/hiviewdfx_hilog_lite/blob/master/README_zh.md)
188
189[hiviewdfx\_hievent\_lite](https://gitee.com/openharmony/hiviewdfx_hievent_lite/blob/master/README_zh.md)
190
191[hiviewdfx\_hiview\_lite](https://gitee.com/openharmony/hiviewdfx_hiview_lite/blob/master/README_zh.md)
192
193