Log4a: Open-Source HarmonyOS Logging Library Boosts Performance & Security
Log4a, an open-source HarmonyOS logging library from One Day Accounting, uses a three-layer architecture with background Worker threads to prevent main-thread blocking, supports encrypted log storage, rolling logs, and server upload, and has been validated across apps with over one million users and ten million downloads.
Log4a was created during the 2024 HarmonyOS Innovation Competition when the HarmonyOS ecosystem lacked mature third-party libraries on the ohpm central repository. The developers of the "One Day Accounting" app needed a logging tool that could handle log collection, issue diagnosis, and performance requirements simultaneously. Drawing on the design principles of the classic log4j framework and adapting them to ArkTS development scenarios, they built Log4a — where the "a" stands for ArkTS.
Three-Layer Architecture for Flexible Log Handling
Log4a adopts a three-layer architecture consisting of LogManager, Logger, and Appender. The Appender layer is responsible for concrete log persistence, allowing developers to choose the log handling method that fits their needs. Out of the box, Log4a provides database log storage, rolling log configuration to prevent unbounded growth, direct log upload to a server for rapid online issue diagnosis, custom log formats, and log encryption for enhanced security.
Background Worker Threads Eliminate Main-Thread Blocking
To address performance pressure from high-frequency log writes, Log4a centrally manages file output threads and uses a Worker mechanism to perform file read/write operations on background threads. This keeps log persistence off the main thread, ensuring the application remains responsive even under heavy logging loads. For scenarios like accounting apps that demand smooth interaction, this optimization is critical — users perceive no lag from the logging system, while logs remain as complete as possible during crashes or forced closes, helping developers locate and fix bugs faster.
Custom Encryption Protects Sensitive Data
Developers often record API details, account status, and business context in logs for debugging. Without protection, log files become privacy leakage risks. Log4a supports customizable encryption, enabling encrypted storage of log content written to files. This preserves diagnostic capability while safeguarding user data and preventing information leaks.
Production-Validated at Scale
Log4a has been integrated into multiple applications covering over one million users, with cumulative downloads exceeding ten million. This demonstrates that it is not merely a "usable" open-source tool but one that has withstood real-world business scenarios. Future work will optimize cross-thread Logger object sharing to better serve complex logging requirements. HarmonyOS developers can integrate Log4a via the OpenHarmony third-party library center by searching for "@pie/log4a" at https://ohpm.openharmony.cn/#/cn/detail/@pie%2Flog4a.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
HarmonyOS Developer Technology
HarmonyOS developers provide key technology analysis, version updates, Codelabs practice, and event information for HarmonyOS. Welcome developers to join the HarmonyOS ecosystem and create infinite possibilities together!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
