ArkTS Logger for HarmonyOS: Inspired by Flutter's Logger Package

Introduction
In HarmonyOS application development, an efficient logging system
is crucial for debugging and troubleshooting. While the native hilog
interface provides comprehensive functionality, it presents several practical limitations:
Complex Data: Requires manual serialization of objects and collection types
Log Filtering: Lacks capability for runtime dynamic log level adjustment
Local Storage: No built-in log file management or rotation mechanism
Cloud Reporting: Missing ready-to-use cloud logging solution
During my experience developing Flutter Apps, I worked with Flutter's logger
library whose elegant design left a deep impression. Inspired by this, I developed a similar logging solution for HarmonyOS (logger) featuring:
Support for multiple data types including primitives, objects, Classes, Maps, Sets with JSON serialization
Customizable logging behaviors like dynamic filtering, local file I/O (with automatic periodic cleanup), and cloud reporting
Compatibility with HarmonyOS Next API12+
Stack trace information output
Preparation
Register an OHPM Account, Visit the OpenHarmony Package Manager to create your OHPM account.
Go to [Profile → Security Management] to add your OHPM public key:
# ssh-keygen creates public/private key pair - NOTE: passphrase MUST NOT be omitted!!! # -------------------------------------------------------------- ssh-keygen -m PEM -t RSA -b 4096 -f ~/.ssh_ohpm/id_rsa_publish Generating public/private RSA key pair. Enter passphrase (empty for no passphrase):
Go to [Profile] Copy the publish code and configure publish_id on your local device
# your_publish_id: the publishing code you copied # ---------------------------------------------- ohpm config set publish_id your_publish_id
Development Explanation
API | Purpose | Detailed Description |
Filter | Log filtering | Controls which logs should be recorded, supports log level filtering (DEBUG/INFO/WARN/ERROR) etc. |
Output | Log output stream management | Defines final log output destinations and behaviors, such as console, local files, network reporting, multiple output source combinations |
Printer | Log content formatting | Handles log visual presentation, including text formatting (colors/indentation/borders), structured output, stack tracing |
Logger | Log trigger entry point | Provides developer-facing logging interface with multiple level methods (debug, info, warn, error etc.) |
API | Inherits From | Purpose | Detailed Description |
SafeFilter | Filter | Basic log filtering | Fundamental log level filtering (DEBUG, INFO, WARN, ERROR) |
SafeWriteFileOutput | Output | Filesystem log output | Supports automatic log directory creation (daily), log file rotation, exception recovery mechanism, and uses AsyncMutex to optimize high-frequency write/read operations |
SafeConsoleOutput | Output | Console log output | Compatible with DevEco Studio console, supports color differentiation and highlighting |
SafeMultiOutput | Output | Multi-destination output | Supports multiple output sources with configurable output weights |
SafeSimplePrinter | Printer | Minimalist log format | Single-line text output without decorations (no timestamps/borders), ideal for high-frequency debug logs |
SafePrettyPrinter | Printer | Beautified formatted output | Multi-line formatted output with borders, auto-aligned key-value pairs, supports stack trace display and complex object tree visualization |
SafeHybridPrinter | Printer | Hybrid strategy printer | Uses different printing strategies for different log levels with customizable level mapping |
SafeLogger | Logger | Developer interface | Provides logging API for developers with multiple level methods (debug, info, warn, error, etc.) |
Common APIs
Category | Method | Description | Return Type |
Custom Options | reset | Customize log configuration | Promise<void> |
Logging | debug | Log DEBUG level message | Promise<void> |
info | Log INFO level message | Promise<void> | |
warn | Log WARN level message | Promise<void> | |
error | Log ERROR level message | Promise<void> | |
fatal | Log FATAL level (highest) message | Promise<void> | |
File Reading | readAsRequestFile | Read latest log file (full path) | `Promise<IRequestFile |
readAsRequestFiles | Read recent log files (full paths) | `Promise<IRequestFile[] | |
readAsArrayBuffer | Read latest log file (binary data) | `Promise<IArrayBuffer | |
readAsArrayBuffers | Read recent log files (binary data) | `Promise<IArrayBuffer[] | |
readAsString | Read latest log file (text content) | `Promise<IString | |
readAsStrings | Read recent log files (text content) | `Promise<IString[] | |
Event Listeners | addWriteListener | Register log write event listener | void |
removeWriteListener | Remove log write event listener | void | |
Log Cleanup | clearHistory | Clear expired log files | Promise<void> |
clearAll | Clear all log files (including current) | Promise<void> | |
Log Inspection | isEmpty | Check if log files are empty (non-existent) | Promise<boolean> |
Source code / Repository
Github logger
OpenHarmony logger
Welcome to give a Star 👍
Subscribe to my newsletter
Read articles from linpengteng directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

linpengteng
linpengteng
Programmer (Harmony、Flutter、Web、Go)