• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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├── services
54│   └── hilogd          # 日志常驻服务实现
55│   └── hilogtool       # 日志工具实现
56```
57
58## 约束<a name="section119744591305"></a>
59
60依赖 Clang 编译器\(**Clang**  8.0.0 \)及以上。
61
62## 说明<a name="section06487425716"></a>
63
64### 接口说明<a name="section1551164914237"></a>
65
66**表 1**  主要API说明
67
68<a name="table5489165165714"></a>
69<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>
70</th>
71<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>
72</th>
73<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>
74</th>
75</tr>
76</thead>
77<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>
78</td>
79<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>
80</td>
81<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>
82</td>
83</tr>
84<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>
85</td>
86<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 &amp;label, const char *fmt, ...)</p>
87</td>
88<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>
89</td>
90</tr>
91</tbody>
92</table>
93
94### 使用说明<a name="section129654513264"></a>
95
961. 查看日志
97
98通过shell , 执行命令 hilog  | grep “MY\_TAG”。
99
100```
101Debug模式输出:
10201-26 11:01:06.870 1051 1051 W 00201/test: Failed to visit [https://gitee.com/openharmony](https://gitee.com/openharmony), reason:503.
103release模式输出:
10401-26 11:01:06.870 1051 1051 W 00201/test: Failed to visit <private>, reason:503.
105```
1062. hilog命令行使用说明
107
108|     短选项                                                                                   |     长选项           |     参数                                                                                                                              |     说明                                                                                                     |
109|----------------------------------------------------------------------------------------------|----------------------|---------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|
110|     -h                                                                                       |     --help           |                                                                                                                                       |     帮助命令                                                                                                 |
111|     缺省                                                                                     |     缺省             |                                                                                                                                       |     阻塞读日志,不退出                                                                                       |
112|     -x                                                                                       |     --exit           |                                                                                                                                       |     非阻塞读日志,读完退出                                                                                   |
113|     -g                                                                                       |                      |                                                                                                                                       |     查询buffer的大小,配合-t指定某一类型使用,默认app和core                                                |
114|     -G                                                                                       |     --buffer-size    |     \<size>                                                                                                                            |     设置指定\<type>日志类型缓冲区的大小,配合-t指定某一类型使用,默认app和core,     可使用B/K/M/G为单位    |
115|     -r                                                                                       |                      |                                                                                                                                       |     清除buffer日志,配合-t指定某一类型使用,默认app和core                                                     |
116|     -p                                                                                       |     --privacy        |     <on/off>                                                                                                                          |     支持系统调试时日志隐私开关控制                                                                         |
117|                                                                                              |                      |     on                                                                                                                                |     打开隐私开关,显示\<private>                                                                              |
118|                                                                                              |                      |     off                                                                                                                               |     关闭隐私开关,显示明文                                                                                   |
119|     -k                                                                                       |                      |     <on/off>                                                                                                                          |     Kernel日志读取开关控制                                                                                   |
120|                                                                                              |                      |     on                                                                                                                                |     打开读取kernel日志                                                                                       |
121|                                                                                              |                      |     off                                                                                                                               |     关闭读取kernel日志                                                                                       |
122|     -s                                                                                       |     --statistics     |                                                                                                                                       |     查询统计信息,需配合-t或-D使用                                                                           |
123|     -S                                                                                       |                      |                                                                                                                                       |     清除统计信息,需配合-t或-D使用                                                                           |
124|     -Q                                                                                       |                      |     \<control-type>                                                                                                                    |     流控缺省配额开关控制                                                                                     |
125|                                                                                              |                      |     pidon                                                                                                                             |     进程流控开关打开                                                                                         |
126|                                                                                              |                      |     pidoff                                                                                                                            |     进程流控开关关闭                                                                                         |
127|                                                                                              |                      |     domainon                                                                                                                          |     domain流控开关打开                                                                                       |
128|                                                                                              |                      |     domainoff                                                                                                                         |     domain流控开关关闭                                                                                       |
129|     -L                                                                                       |     --level          |     \<level>                                                                                                                           |     指定级别的日志,示例:-L D/I/W/E/F                                                                       |
130|     -t                                                                                       |     --type           |     \<type>                                                                                                                            |     指定类型的日志,示例:-t app core init                                                                   |
131|     -D                                                                                       |     --domain         |     \<domain>                                                                                                                          |     指定domain                                                                                               |
132|     -T                                                                                       |     --Tag            |     \<tag>                                                                                                                             |     指定tag                                                                                                  |
133|     -a                                                                                       |     --head           |     \<n>                                                                                                                               |     只显示前\<n>行日志                                                                                        |
134|     -z                                                                                       |     --tail           |     \<n>                                                                                                                               |     只显示后\<n>行日志                                                                                        |
135|     -P                                                                                       |     --pid            |     \<pid>                                                                                                                             |     标识不同的pid                                                                                            |
136|     -e                                                                                       |     --regex          |     \<expr>                                                                                                                            |     只打印日志消息与\<expr>匹配的行,其中\<expr>是一个正则表达式                                               |
137|     -f                                                                                       |     --filename       |     \<filename>                                                                                                                        |     设置落盘的文件名                                                                                         |
138|     -l                                                                                       |     --length         |     \<length>                                                                                                                          |     设置落盘的文件大小,需要大于等于64K                                                                          |
139|     -n                                                                                       |     --number         |     \<number>                                                                                                                          |     设置落盘文件的个数                                                                                       |
140|     -j                                                                                       |     --jobid          |     \<jobid>                                                                                                                           |     设置落盘任务的ID                                                                                         |
141|     -w                                                                                       |     --write          |     \<control>                                                                                                                         |     落盘任务控制                                                                                             |
142|                                                                                              |                      |     query                                                                                                                             |     落盘任务查询                                                                                             |
143|                                                                                              |                      |     start                                                                                                                             |     落盘任务开始,命令行参数为文件名、单文件大小、落盘算法、rotate文件数目.                                  |
144|                                                                                              |                      |     stop                                                                                                                              |     落盘任务停止                                                                                             |
145|     -m                                                                                       |     --stream         |     \<algorithm>                                                                                                                       |     落盘方式控制                                                                                             |
146|                                                                                              |                      |     none                                                                                                                              |     无压缩方式落盘                                                                                           |
147|                                                                                              |                      |     zlib                                                                                                                              |     zlib压缩算法落盘,落盘文件为.gz                                                                          |
148|                                                                                              |                      |     zstd                                                                                                                              |     zstd压缩算法落盘,落盘文件为.zst                                                                         |
149|     -v                                                                                       |     --format         |     \<format>                                                                                                                          |                                                                                                              |
150|                                                                                              |                      |     time                                                                                                                              |     显示本地时间                                                                                             |
151|                                                                                              |                      |     color                                                                                                                             |     显示不同级别显示不同颜色,参数缺省级别颜色模式处理(按黑白方式)                                         |
152|                                                                                              |                      |     epoch                                                                                                                             |     显示相对1970时间                                                                                         |
153|                                                                                              |                      |     monotonic                                                                                                                         |     显示相对启动时间                                                                                         |
154|                                                                                              |                      |     usec                                                                                                                              |     显示微秒精度时间                                                                                         |
155|                                                                                              |                      |     nsec                                                                                                                              |     显示纳秒精度时间                                                                                         |
156|                                                                                              |                      |     year                                                                                                                              |     显示将年份添加到显示的时间                                                                               |
157|                                                                                              |                      |     zone                                                                                                                              |     显示将本地时区添加到显示的时间                                                                           |
158|     -b                                                                                       |     --baselevel      |     \<loglevel>                                                                                                                        |     设置可打印日志的最低等级:D(DEBUG)/I(INFO)/W(WARN)/E(ERROR)/F(FATAL)                                     |
159
160```
161示例:hilog -G 2M -t core
162解释:设置buffer大小为2M,日志类型为core。
163示例:hilog -g -t app
164解释:查询日志类型为app的buffer大小。
165示例:hilog -w start -f "hilog" -l 66k -m zlib -n 10
166解释:执行名字为hilog的落盘任务,10个落盘文件进行轮转,单个文件落盘大小为66k,采用zlib压缩算法。
167type、level、domain、tag支持排除查询,排除查询可以使用以"^"开头的参数和分隔符","."来完成
168示例:hilog -t ^core,app 排除core和app类型的日志,可以与其他参数一起使用。
169示例:hilog -t app core 打印core和app类型的日志,可以与其他参数一起使用。
170```
171
172## 涉及仓<a name="section177639411669"></a>
173
174[DFX子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/DFX%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
175
176[hiviewdfx\_hiview](https://gitee.com/openharmony/hiviewdfx_hiview/blob/master/README_zh.md)
177
178**hiviewdfx\_hilog**
179
180[hiviewdfx\_hiappevent](https://gitee.com/openharmony/hiviewdfx_hiappevent/blob/master/README_zh.md)
181
182[hiviewdfx\_hisysevent](https://gitee.com/openharmony/hiviewdfx_hisysevent/blob/master/README_zh.md)
183
184[hiviewdfx\_faultloggerd](https://gitee.com/openharmony/hiviewdfx_faultloggerd/blob/master/README_zh.md)
185
186[hiviewdfx\_hilog\_lite](https://gitee.com/openharmony/hiviewdfx_hilog_lite/blob/master/README_zh.md)
187
188[hiviewdfx\_hievent\_lite](https://gitee.com/openharmony/hiviewdfx_hievent_lite/blob/master/README_zh.md)
189
190[hiviewdfx\_hiview\_lite](https://gitee.com/openharmony/hiviewdfx_hiview_lite/blob/master/README_zh.md)
191
192