# ohos_logan **Repository Path**: cbbgs/ohos_logan ## Basic Information - **Project Name**: ohos_logan - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-26 - **Last Updated**: 2026-02-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Logan ## Introduction > Logan is an open-source front-end log system by Meituan. This project is the OpenHarmony/HarmonyOS SDK version of Logan. It provides log collection, storage, encryption, and upload capabilities, supporting high-performance mmap writing and AES encrypted storage. ## Installation ```shell ohpm install @ohos/logan ``` For more information about OpenHarmony ohpm environment configuration, please refer to [How to Install OpenHarmony ohpm Packages](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.md). ## Usage ### Import Components ```typescript import { Logan, LoganConfig, SendLogCallback } from '@ohos/logan'; ``` ### Initialization ```typescript import { common } from '@kit.AbilityKit'; // Get application context const context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // Configure encryption key (16 bytes) const encryptKey16: Uint8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); const encryptIv16: Uint8Array = new Uint8Array([16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); // Build configuration const config: LoganConfig = LoganConfig.Builder() .setCachePath(context.cacheDir + '/logan_cache') // mmap cache directory .setPath(context.filesDir + '/logan_files') // Log file storage directory .setMaxFile(10) // Max 10MB per file .setDay(7) // Retain logs for 7 days .setEncryptKey16(encryptKey16) // AES encryption key .setEncryptIV16(encryptIv16) // AES initialization vector .build(); // Initialize Logan Logan.init(config); ``` ### Write Logs ```typescript // Write log with type (custom integer) Logan.w('This is a log message', 1); Logan.w('User clicked the button', 2); Logan.w('Network request completed', 3); ``` ### Flush Logs to File ```typescript // Immediately flush logs from memory to disk Logan.f(); ``` ### Get Log File Information ```typescript // Get date and size of all log files const filesInfo: Record | null = await Logan.getAllFilesInfo(); if (filesInfo) { for (const date in filesInfo) { console.info(`Date: ${date}, Size: ${filesInfo[date]} bytes`); } } ``` ### Send Logs (Simplified) ```typescript const callback: SendLogCallback = { onLogSendCompleted: (statusCode: number, data: ArrayBuffer): void => { if (statusCode >= 200 && statusCode < 300) { console.info('Log upload successful'); } else { console.error('Log upload failed, status code: ' + statusCode); } } }; Logan.s1( 'https://your-server.com/logan/upload', // Upload URL '2026-02-05', // Log date 'your-app-id', // App ID 'user-union-id', // User ID 'device-id', // Device ID '1.0.0', // Build version '1.0.0', // App version callback // Callback ); ``` ### Send Logs (Custom Headers) ```typescript const headers: Record = { 'appId': 'your-app-id', 'deviceId': 'device-id', 'customHeader': 'customValue' }; Logan.s2( 'https://your-server.com/logan/upload', '2026-02-05', headers, callback ); ``` ### Enable Debug Mode ```typescript // Enable debug mode for more log output Logan.setDebug(true); ``` ### Listen to Log Status ```typescript import { OnLoganProtocolStatus } from '@ohos/logan'; const statusListener: OnLoganProtocolStatus = { loganProtocolStatus: (cmd: string, code: number): void => { console.info(`Logan status callback - Command: ${cmd}, Status code: ${code}`); } }; Logan.setOnLoganProtocolStatus(statusListener); ``` ## API Reference ### Logan Core API | Method | Parameters | Return | Description | | ------ | ---------- | ------ | ----------- | | `init(config)` | `LoganConfig` | `void` | Initialize log system | | `w(log, type)` | `string, number` | `void` | Write a log entry | | `f()` | - | `void` | Flush logs to disk | | `s(dates, runnable)` | `string[], SendLogRunnable` | `void` | Send logs in batch | | `s1(url, date, ...)` | multiple | `void` | Simplified log sending | | `s2(url, date, headers, callback)` | multiple | `void` | Send with custom headers | | `getAllFilesInfo()` | - | `Promise \| null>` | Get log file information | | `setDebug(debug)` | `boolean` | `void` | Set debug mode | | `setOnLoganProtocolStatus(listener)` | `OnLoganProtocolStatus` | `void` | Set status listener | ### LoganConfig.Builder Methods | Method | Parameters | Default | Description | | ------ | ---------- | ------- | ----------- | | `setCachePath(path)` | `string` | - | mmap cache directory (required) | | `setPath(path)` | `string` | - | Log file storage directory (required) | | `setMaxFile(mb)` | `number` | `10` | Max size per log file (MB) | | `setDay(days)` | `number` | `7` | Log retention days | | `setEncryptKey16(key)` | `Uint8Array` | - | AES 16-byte encryption key (required) | | `setEncryptIV16(iv)` | `Uint8Array` | - | AES 16-byte initialization vector (required) | | `setMinSDCard(mb)` | `number` | `50` | Minimum remaining storage space (MB) | | `build()` | - | `LoganConfig` | Build configuration object | ### SendLogCallback Interface | Method | Parameters | Description | | ------ | ---------- | ----------- | | `onLogSendCompleted(statusCode, data)` | `number, ArrayBuffer` | Log send completion callback | ### OnLoganProtocolStatus Interface | Method | Parameters | Description | | ------ | ---------- | ----------- | | `loganProtocolStatus(cmd, code)` | `string, number` | Log operation status callback | ## Technical Features - **AES Encryption**: Log content is encrypted with AES for sensitive data protection - **High-Performance mmap Writing**: Uses memory mapping to avoid frequent I/O and improve write performance - **Worker Thread Isolation**: Log writing runs in a separate thread without blocking the main thread - **Cross-Platform Consistency**: Behavior consistent with Android Logan SDK - **Native C++ Implementation**: Core logic implemented in C++ and bridged via N-API ## Constraints Verified on the following versions: - DevEco Studio NEXT Developer Beta3: (5.0.3.530), SDK: API12 (5.0.0.35(SP3)) ## Directory Structure ``` |---- ohos_logan | |---- entry # Sample code folder | |---- library # Logan library folder | | |---- src | | | └── main | | | |---- cpp # C++ Native code | | | | |---- logan # Logan bridge layer | | | | | |---- include/logan_ohos.h | | | | | └── src/logan_ohos.cpp | | | | |---- thirdparty/Logan # Logan C core library | | | | └── CMakeLists.txt | | | └── ets | | | |---- logan # ArkTS implementation | | | | |---- Logan.ets # Main entry class | | | | |---- LoganConfig.ets # Configuration class | | | | |---- CLoganProtocol.ets # C layer protocol | | | | |---- LoganControlCenter.ets # Control center | | | | |---- LoganThread.ets # Thread management | | | | |---- SendLogRunnable.ets # Send task base class | | | | |---- SendLogDefaultRunnable.ets # Default send implementation | | | | └── ... # Other utility classes | | | └── workers | | | └── LoganWorker.ets # Worker thread | | |---- Index.ets # External interface | |---- README.md # English documentation | |---- README_zh.md # Chinese documentation ``` ## Contributing If you encounter any issues during use, feel free to submit an [Issue](). Of course, we also welcome [PRs]() to contribute. ## License This project is licensed under the [MIT License](./LICENSE). Feel free to enjoy and participate in open source.