This content originally appeared on DEV Community and was authored by lovehmos
HarmonyOS HiLog: Real-World Logging Tips & Pitfalls
1. Preface
Recently, while developing HarmonyOS applications, I realized that log debugging is truly indispensable in the development process. I remember when I first encountered HiLog, I thought it was just like console.log, but after going live, I found that too many logs affected performance, and I almost printed sensitive information in the production logs—got a "lesson" from the security team. Later, after digging deeper into HiLog, I discovered its log levels, privacy protection, and formatting features are very powerful. Using it well can greatly improve development and operations efficiency.
Once, an online user reported an occasional bug, and our team spent two days trying to locate it. In the end, it was HiLog's leveled logs and key process tracking that helped us pinpoint the root cause among thousands of log entries. At that moment, I truly felt: "Good logs, easy troubleshooting!"
Supported Platforms: Phone, PC, 2in1, Tablet, Wearable
API Support: Supported from API version 7, with some interfaces supporting more features from API version 11/15/20
2. Typical Use Cases
- Debugging: Output detailed debug information to help locate issues during development.
- Business Process Tracing: Record key business nodes to restore user actions and business flows.
- Exception & Warning Monitoring: Capture and output exceptions, errors, and warnings for quick online troubleshooting.
- Performance Analysis: Output performance-related data using log levels and formatting to assist optimization.
- Privacy Compliance: Use privacy flags to protect sensitive information and meet compliance requirements.
3. Module Import
import { hilog } from '@kit.PerformanceAnalysisKit';
4. Main APIs and Parameter Description
4.1 Log Levels (LogLevel)
Name | Value | Description |
---|---|---|
DEBUG | 3 | Detailed process records for analysis and troubleshooting |
INFO | 4 | Key business process, expected abnormal situations |
WARN | 5 | More serious but recoverable unexpected situations |
ERROR | 6 | Errors affecting functionality or user experience |
FATAL | 7 | Critical fatal exceptions, app about to crash |
4.2 Main APIs
-
hilog.isLoggable(domain: number, tag: string, level: LogLevel): boolean
- Check if logs of the specified domain, tag, and level can be printed.
hilog.debug(domain, tag, format, ...args)
hilog.info(domain, tag, format, ...args)
hilog.warn(domain, tag, format, ...args)
hilog.error(domain, tag, format, ...args)
-
hilog.fatal(domain, tag, format, ...args)
- Output logs at different levels.
-
hilog.setMinLogLevel(level: LogLevel)
- Set the minimum log level for the app.
Main Parameter Description
- domain: Log domain identifier (0x0~0xFFFF), recommended to customize within the app.
- tag: Log tag (suggest class/business name, max 31 bytes, avoid Chinese).
- format: Format string, supports parameter types and privacy flags.
- args: Parameter list corresponding to format.
Privacy Flags and Formatting
-
%{public}s
outputs in plain text,%{private}d
outputs as private (default is private, content shown as ). - Supported types: d/i (number/bigint), s (string/bool/null/undefined), o/O (object/undefined/null, API 20+).
5. Code Examples
5.1 Basic Usage
import { hilog } from '@kit.PerformanceAnalysisKit';
const DOMAIN = 0x0001;
const TAG = "Demo";
// Check if INFO level log can be printed
if (hilog.isLoggable(DOMAIN, TAG, hilog.LogLevel.INFO)) {
hilog.info(DOMAIN, TAG, "App started at %{public}s", new Date().toISOString());
}
// Output logs at different levels
hilog.debug(DOMAIN, TAG, "Debug info: %{public}s", "debugging...");
hilog.info(DOMAIN, TAG, "User login: %{public}s", "user123");
hilog.warn(DOMAIN, TAG, "Network unstable, retry count: %{public}d", 2);
hilog.error(DOMAIN, TAG, "Request failed, code: %{public}d", 500);
hilog.fatal(DOMAIN, TAG, "App crashed, error: %{public}s", "NullPointerException");
5.2 Privacy and Formatting
// Private parameters will not be output in plain text
hilog.info(DOMAIN, TAG, "User phone: %{private}s, login at %{public}s", "138****8888", "2025-06-21 11:14");
// Object, null, undefined, boolean, bigint, etc.
let obj = { name: "Jack", age: 22 };
hilog.info(DOMAIN, TAG, "User info: %{public}o", obj);
hilog.info(DOMAIN, TAG, "Null value: %{public}s", null);
hilog.info(DOMAIN, TAG, "Boolean: %{public}s", true);
hilog.info(DOMAIN, TAG, "BigInt: %{public}d", BigInt(1234567890123456789));
5.3 Set Minimum Log Level
hilog.setMinLogLevel(hilog.LogLevel.WARN);
hilog.info(DOMAIN, TAG, "This info log will not be printed");
hilog.error(DOMAIN, TAG, "This error log will be printed");
6. Pitfalls and Developer Experience
6.1 Problems Encountered
-
Log Flooding
- Problem: At first, all logs were set to info, resulting in too many logs and key logs being buried.
- Solution: Use log levels properly—info for important processes, debug for details, error/warn for exceptions.
-
Sensitive Information Leakage
- Problem: Once, I printed a user's phone number directly in the logs and got reminded by the security team.
- Solution: Always use
%{private}
for sensitive content and regularly review log content.
-
Performance Impact
- Problem: Frequent logging in loops caused UI lag.
- Solution: Only keep necessary logs in high-frequency scenarios, avoid debug logs in production.
-
Log Loss
- Problem: Setting the minimum log level too high caused key logs to be missed, making troubleshooting difficult.
- Solution: Set the minimum level to debug during development, adjust as needed for production.
6.2 Optimization Suggestions
-
Domain-based Log Management
- Properly divide domain and tag for easier log search and issue location later.
-
Concise Log Content
- Keep log content concise, avoid useless information.
-
Privacy Compliance
- Always use private flag for user data and sensitive info to prevent leaks.
-
Log Cleanup and Archiving
- Regularly clean up useless logs, archive important ones.
7. Summary
If you use HiLog well, both development efficiency and online operations experience can be greatly improved. My suggestions:
- Develop good habits for log levels and privacy protection
- Always log key processes, exceptions, and performance nodes
- Make your logs "warm"—help yourself troubleshoot and make team collaboration smoother
Once, we had an urgent production release, and thanks to the key logs we had set up earlier, we located and fixed an online bug in half an hour. The team all said: "Good logs, no ops worries!"
8. FAQ
- Logs not output: Check if domain/tag/level are correct, or if setMinLogLevel is too high.
- Chinese garbled/misalignment: Avoid using Chinese characters in tag.
-
Private parameters not shown in plain text: Default is private, use
%{public}
for plain output. - Too many logs affect performance: Only output high-frequency logs when necessary.
9. Reference
Author: Tilling in the World
Email: 1743914721@qq.com
Copyright: This article is original by a CSDN blogger. Please include the original link and this statement when reprinting.
This content originally appeared on DEV Community and was authored by lovehmos

lovehmos | Sciencx (2025-06-30T09:47:49+00:00) HarmonyOS HiLog: Real-World Logging Tips & Pitfalls. Retrieved from https://www.scien.cx/2025/06/30/harmonyos-hilog-real-world-logging-tips-pitfalls/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.