Introduction to HarmonyOS Next Logging Tools
In HarmonyOS Next development, logs are the primary means for debugging and problem-solving. Whether using hilog or console, logs can ultimately be output to the log module of DevEco Studio:
Here, you can filter by app process, log level, and content, and perform convenient searches. However, in scenarios with frequent log updates, we may want to output logs to files. While Android provides the logcat command to export logs to the console or files, HarmonyOS Next offers a similar tool: hilog.
Introduction to hilog Tool
The HiLog logging system is provided for system frameworks, services, and applications to print logs, record user operations, system status, etc. Developers can query log information via the hilog command line.
After installing DevEco Studio IDE or the HarmonyOS SDK and configuring the hdc command tool in the environment variables, you can use hdc to enter the device shell:
hdc shell
Once in the shell, execute hilog commands:
If logs refresh too quickly (less efficient than DevEco Studio's log tool), use hilog with parameters.
hilog Command Parameters
Short Option | Long Option | Parameter | Description |
---|---|---|---|
-h | --help | Help command. | |
Default | Default | Blocking log reading (does not exit). | |
-x | --exit | Non-blocking log reading (exits after reading). | |
-g | Query buffer size (use with -t to specify a type; defaults to app and core). | ||
-G | --buffer-size | Set the buffer size for the specified (use with -t; defaults to app and core; units: B/K/M; range: 64K-16M). | |
-r | Clear buffer logs (use with -t; defaults to app and core). | ||
-k | Kernel log reading control. | ||
on | Enable kernel log reading. | ||
off | Disable kernel log reading. | ||
-s | --statistics | Query statistics (requires -t or -D). | |
-S | Clear statistics (requires -t or -D). | ||
-Q | Flow control quota switch. | ||
pidon | Enable process flow control. | ||
pidoff | Disable process flow control. | ||
domainon | Enable domain flow control. | ||
domainoff | Disable domain flow control. | ||
-L | --level | Specify log levels (e.g., -L D/I/W/E/F). | |
-t | --type | Specify log types (e.g., -t app/core/init/only_prerelease; app: app logs, core: system logs, init: boot logs, only_prerelease: logs for pre-release systems, irrelevant to app developers). | |
-D | --domain | Specify a domain. | |
-T | --tag | Specify a tag. | |
-a | --head | Display only the first lines of logs. | |
-z | --tail | Display only the last lines of logs. | |
-P | --pid | Identify different PIDs. | |
-e | --regex | Print only lines matching the regular expression . | |
-f | --filename | Set the disk file name. | |
-l | --length | Set the disk file size (must be ≥64K). | |
-n | --number | Set the number of disk files. | |
-j | --jobid | Set the disk task ID. | |
-w | --write | Disk task control. | |
query | Query disk tasks. | ||
start | Start a disk task (command line parameters: filename, single file size, disk algorithm, number of rotate files). | ||
stop | Stop the disk task. | ||
refresh | Refresh buffer logs to disk files. | ||
clear | Delete existing disk log files. | ||
-m | --stream | Disk storage algorithm control. | |
none | Disk storage without compression. | ||
zlib | Disk storage with zlib compression (file extension: .gz). | ||
zstd | Disk storage with zstd compression (file extension: .zst). | ||
-v | --format | ||
time | Display local time. | ||
color | Display different levels in different colors (default: black and white). | ||
epoch | Display time relative to 1970. | ||
monotonic | Display time relative to boot. | ||
usec | Display time with microsecond precision. | ||
nsec | Display time with nanosecond precision. | ||
year | Add the year to the displayed time. | ||
zone | Add the local time zone to the displayed time. | ||
wrap | When logs wrap, new lines do not add prefixes like timestamps. | ||
-b | --baselevel | Set the minimum log level (D(DEBUG)/I(INFO)/W(WARN)/E(ERROR)/F(FATAL)). |
Parameter Configuration Practices
Clear Buffer Logs
Logs are typically cached. To avoid interference from previous logs when starting an app, clear the cache:
$ hilog -r
Log type core,app,only_prerelease buffer clear successfully
Kernel Log Reading Control
Kernel logs are usually unnecessary during app debugging. Use hilog -k on/off
to enable/disable them:
$ hilog -k on
Set hilogd storing kmsg log on successfully
$
$ hilog -k off
Set hilogd storing kmsg log off successfully
View Specified Level Logs
hilog -L D/I/W/E/F
View Specified Type Logs
hilog -t app
View Specified Domain Logs
hilog -D 01B06
View Specified TAG Logs
hilog -T tag
View Specified Process Logs
hilog -P pid
View Logs Matching Regex Keywords
hilog -e start
View and Set Disk Tasks
hilog -w control
- Query current tasks:
hilog -w query
- Start hilog disk task with 1000 files:
hilog -w start -n 1000
- Start kmsglog disk task with 100 files:
hilog -w start -n 100 -t kmsg
- Stop current disk task:
hilog -w stop
- Start kmsglog disk task with settings (compression: zlib/zstd/none):
hilog -w start -t kmsg -f kmsglog -l 2M -n 100 -m zlib
Examples:
$ hilog -w query
Persist task query failed
No running persistent task [CODE: -63]
$
$ hilog -w start -n 1000
Persist task [jobid:1][fileNum:1000][fileSize:4194304] start successfully
$
$ hilog -w start -n 100 -t kmsg
Persist task [jobid:2][fileNum:100][fileSize:4194304] start successfully
$
$ hilog -w stop
Persist task [jobid:1] stop successfully
Persist task [jobid:2] stop successfully
$
$ hilog -w start -t kmsg -f kmsglog -l 2M -n 100 -m zlib
Persist task [jobid:2][fileNum:100][fileSize:2097152] start successfully
Set Log Display Format
hilog -v time/color/epoch/monotonic/usec/nsec/year/zone/wrap
This format setting facilitates console log viewing.
Set Minimum Printable Log Level
hilog -b D/I/W/E/F
If logs are not displayed, this setting may be the cause.
Introduction to hilogtool
Hilog logs are stored as encoded binary gz files. After exporting from the device's /data/log/hilog
path, they cannot be directly unzipped and require parsing with the hilogtool to convert to plaintext:
The hilogtool is located in the SDK's hms/toolchains
directory and used as follows:
-
hilogtool parse -i xxx -o xxx -d xxx
-
hilogtool parse –-input xxx –-output xxx --dict xxx
hilogtool Parsing Command Parameters
Option | Description | Example |
---|---|---|
-i/–-input inputDir | Specify the input path (scans all hilog files in the directory for parsing); defaults to the current directory. | Parse all hilog files in D:\temp\hilog:hilogtool parse -i D:\temp\hilog Parse current directory: hilogtool parse -i .
|
-i/–-input inputFile | Parse a single hilog file. | Parse a specific file:hilogtool parse -i D:\temp\data\log\hilog.706.20230711-071816.gz hilogtool parse -i .\hilog.706.20230711-071816.gz
|
-o/–-output outputDir | Specify the output path for parsed logs; defaults to the input file's directory. | Parse current directory logs to D:\temp:hilogtool parse -i .\ -o D:\temp
|
-d/--dict dictFile | Specify the path to the data dictionary; defaults to matching the latest dictionary (format: hilog_dict.20230908-142200.zip) in the current directory. | Parse hilog files in D:\temp\hilog with the dictionary in the same directory:hilogtool parse -i D:\temp\hilog -d D:\temp\hilog\hilog_dict.20230908-142200.zip
|
The data dictionary (format: hilog_dict.2024xxxx-xxxxxx.zip) is automatically generated at device boot in /data/log/hilog
. Missing the dictionary may cause parsing failures; restarting the device regenerates it.
Reference Links
Summary
Hilog and hilogtool help developers understand and debug applications by providing a way to view past events and errors. However, these tools are specific to Huawei's HarmonyOS platform and may not apply to other operating systems.
If developing for HarmonyOS and needing to record and analyze logs, understanding hilog and hilogtool is essential. For other platforms, seek analogous tools and methods for log management.
Top comments (0)