当前位置:网站首页>Hongmeng hilog log printing tips
Hongmeng hilog log printing tips
2022-06-10 01:48:00 【Hua Weiyun】
Preface :
Hello everyone , There is no time to update the article for you , I don't know how long it will take . Recently, I still continue to learn about Hongmeng development . Usually developing Android or java as well as flutter You can use the log printing at the corresponding end to debug the code . Today, I'd like to share some tips on how to print Hongmeng's internal log .
## preparation :
1 Install Hongmeng development environment You can see my previous article
Initial experience of Huawei Hongmeng system development :[https://www.jianshu.com/p/f94c847c7fdc]
Log printing overview
HarmonyOS Provides HiLog Log system , So that the application can follow the specified type 、 Specify the level 、 Specify the format string to output the log content , Help developers understand the running status of applications , Better debugging programs .
The interface for outputting logs is provided by HiLog Class provides . Before outputting the log , You need to call HiLog Auxiliary class HiLogLabel Define log labels .
Define log labels
Use HiLogLabel(int type, int domain, String tag) Define log labels , This includes log types 、 Business areas and TAG. Examples of use :
static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG"); - Parameters type: Used to specify the type of output log .HiLog Currently, only one log type is provided in , That is, the application log type LOG_APP.
- Parameters domain: Used to specify the business domain corresponding to the output log , The value range is 0x0~0xFFFFF, Developers can customize as needed .
- Parameters tag: Used to specify the log id , Can be any string , It is recommended to identify the class or business behavior where the call is located .
Developers can customize the parameters according to the domain and tag To filter and find logs .
Output log
HiLog It defines DEBUG、INFO、WARN、ERROR、FATAL Five log levels , Corresponding methods are provided to output different levels of logs , As shown in the following table .
Parameters label: Defined HiLogLabel label .
Parameters format: Format string , Formatted output for logs . Multiple parameters can be set in the format string , For example, the format string is “Failed to visit %s.”,“%s” The parameter type is string Variable parameter identification of , The specific value is args In the definition of .
Each parameter needs to be added with a privacy id , It is divided into {public} or {private}, The default is {private}.{public} Indicates that the log print results are visible ;{private} Indicates that the log print result is not visible , The output is <private>.
Parameters args: It can be for 0 Two or more parameters , Is the parameter list corresponding to the parameter type in the format string . Number of parameters 、 The type must correspond to the identity in the format string one by one .
HiLog Source code
package ohos.hiviewdfx;public final class HiLog { public static final int DEBUG = 3; public static final int ERROR = 6; public static final int FATAL = 7; public static final int INFO = 4; public static final int LOG_APP = 0; public static final int WARN = 5; HiLog() { throw new RuntimeException("Stub!"); } public static int debug(HiLogLabel label, String format, Object... args) { throw new RuntimeException("Stub!"); } public static int info(HiLogLabel label, String format, Object... args) { throw new RuntimeException("Stub!"); } public static int warn(HiLogLabel label, String format, Object... args) { throw new RuntimeException("Stub!"); } public static int error(HiLogLabel label, String format, Object... args) { throw new RuntimeException("Stub!"); } public static int fatal(HiLogLabel label, String format, Object... args) { throw new RuntimeException("Stub!"); } public static boolean isLoggable(int domain, String tag, int level) { throw new RuntimeException("Stub!"); } public static String getStackTrace(Throwable tr) { throw new RuntimeException("Stub!"); }}We used observation Hilog Source code Hilog Class provides the opposite method Yes
etc. 5 A static method
To output a WARN Level information as an example , Sample code :
HiLog.warn(LABEL, "Failed to visit %{private}s, reason:%{public}d.", url, errno);This line of code indicates that a log label is output label Warning message , The format string is :“Failed to visit %{private}s, reason:%{public}d.”. Including variable parameters url The format of is a private string ,errno Is a public integer number .
View log information
DevEco Studio Provides HiLog Window to view log information , Developers can set up devices 、 process 、 Log level and search keywords to filter log information . The search function supports the use of regular expressions , Developers can search custom business domain values and TAG To filter log information .
As shown in the example , After selecting the equipment and process according to the actual situation , Search for business domain values “00201” Screening , Get the corresponding log information
![6865547-ac84ffc7fec2db77[1].png](https://bbs-img.huaweicloud.com/blogs/img/20220609/1654758920419816917.png)
Results output :
01-20 16:08:36.908 23597-23597/com.example.myapplication W 00201/MY_TAG: Failed to visit <private>, reason:503- W Indicates that the log level is WARN.
- 00201/MY_TAG For developers in HiLogLabel As defined in .
- In the log content url It is a private parameter and does not display the specific content , Show only <private>.errno Is a public parameter , Displays the actual value 503.
Log printing instance
- 1 Build a new project , stay “Project” Window click “entry > src > main > java > App package name > slice”, Open the “MainAbilitySlice.java” file , We are MainAbilitySlice Of onStart Method to print a log
package com.example.hms_logdemo.slice;import com.example.hms_logdemo.HiLogUtils;import com.example.hms_logdemo.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;public class MainAbilitySlice extends AbilitySlice { // Define log labels private static final HiLogLabel LABEL = new HiLogLabel(ohos.hiviewdfx.HiLog.LOG_APP, 0x00201, "HMS_TAG"); @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); HiLog.info(LABEL, "test"); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}- 2 Run the project , Click the button on the application interface .
- 3 stay DevEco Studio The bottom of , Switch to “HiLog” window , Set the log filter conditions .
Select the current device and process , Log level selection Info, The search content is set to “00201” Or set the search content to "HMS_TAG". At this time, the window only displays the qualified logs , The effect is shown in the figure .
The effect is as shown in the picture :

Log tool class
In actual development, we will write a tool class for the official log Make a proper package Edge banding we call And print log debugging
package com.example.hms_logdemo;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;public class HiLogUtils { public static final String TAG="HMS_TAG"; private static final HiLogLabel LABEL = new HiLogLabel(ohos.hiviewdfx.HiLog.LOG_APP, 0x00201, TAG); public static boolean flag=false; public static void d(String msg){ if(!flag){ HiLog.debug(LABEL, msg); } } public static void e(String msg){ if(!flag){ HiLog.error(LABEL, msg); } } public static void w(String msg){ if(!flag){ HiLog.warn(LABEL, msg); } } public static void i (String msg){ if(!flag){ HiLog.info(LABEL, msg); } } public static oid f(String msg){ if(!flag){ HiLog.fatal(LABEL, msg); } }}Specific call
package com.example.hms_logdemo.slice;import com.example.hms_logdemo.HiLogUtils;import com.example.hms_logdemo.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); HiLog.info(LABEL, "test"); HiLogUtils.i("info"); HiLogUtils.e("test"); HiLogUtils.d("debug"); HiLogUtils.f("fatal"); HiLogUtils.w("warn"); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}Output effect

So far This is the end of Hongmeng's log printing skills .
The final summary
In fact, when we develop in actual combat , Because in config.json Configure key value pairs in the configuration file by reading Configuration file refers to To determine whether the log is enabled . our app On the go release It should be necessary to turn off our active printing log All we need to do is config.json Turn off the switch in the . The blog knowledge points in this issue are relatively simple Students should be able to understand and learn to use . Official documents also say I also do some sorting and add some personal opinions . Finally, I hope my article can help you to solve the problem , In the future, I will contribute more useful code to share with you . If you think the article is good , Please pay attention and star, Thank you here
边栏推荐
- [email protected] Project training
- Beyond Compare 3密钥序列号分享及密钥被撤销的解决办法
- Graduated in 985, failed to start a business at the age of 35, returned at the age of 36, and was laid off at the age of 40. What should middle-aged couples do if they are unemployed?
- 软件工程期末复习
- zmq通信
- SSM框架整合-搭建简单账号登录系统
- 【LeetCode】64. 最小路径和
- What are the reasons for frequent channel drops in easycvr cascaded video convergence platform?
- [program life] "stage summary" - at a loss
- From these papers in 2022, we can see the trend of recommended system sequence modeling
猜你喜欢
![[neural network] (22) convmixer code reproduction, network analysis, complete tensorflow code attached](/img/59/3ea6747bad1c280ea1d43176037102.png)
[neural network] (22) convmixer code reproduction, network analysis, complete tensorflow code attached

【FPGA】day16-FIFO实现uart协议

Using GPU accelerated training model in keras; Install CUDA; cudnn; cudnn_ cnn_ infer64_ 8.dll is not in path; device_ lib. list_ local_ Devices has no GPU; hang up

【ACL 2022】Hallucinated but Factual! Inspecting the Factuality of Hallucinations

Free batch import software for generating sitemap maps

比你想象中更强大的 reduce 以及对敲码的思考

Thingsboard tutorial (19): overview of rule nodes

有机金属多孔材料MOF(Fe)包载喜树碱,藤黄酸、吲哚菁绿|RMOF-3包载紫杉醇定制

IDEA 版 Postman问世,亲测好用

国内现货白银有哪些好技术:常见指标的简单用法
随机推荐
Node red series (26): use the dashboard node to develop common table search layouts
Introduction to cross platform multimedia rendering engine OPR
How the computer system modifies the picture format
Application of discrete time integration in Simulink simulation
[leetcode] edit distance
为什么芯片设计也需要「匠人精神」?
LeetCode 700:二叉搜索树中的搜索
Allan方差定義與計算方法簡介
实验三 字符类型及其操作(新)
Locust:微服务性能测试利器
Why does chip design also need "Craftsmanship"?
Inftnews | the future of NFT in Web3 economy
Associative array & regular expression
【LeetCode】437.路径总和III
【FPGA】day16-FIFO实现uart协议
From these papers in 2022, we can see the trend of recommended system sequence modeling
Locust: a powerful tool for microservice performance testing
【LeetCode】338. Bit count
Leetcode 98: validate binary search tree
【LeetCode】105. 从前序与中序遍历序列构造二叉树