Log Configuration¶
This document describes the HarmonyOS Log initialization configuration.
Sampling field migration (
ft_sdk >= 0.1.18):setLogSamplingRateuses a floating-point percentage from 0 to 100; the legacysetSamplingRateretains the 0 to 1 scale, so multiply the value by 100 during migration. The new field takes precedence over the old field only when it is explicitly set. Invalid values throw an exception and do not change the previous valid value. For versions earlier than this version, continue to use the legacy method; deprecation does not mean removal, and the legacy method retains its original unit and behavior.
Initial Configuration¶
import { FTSDK, FTLoggerConfig, Status } from '@guancecloud/ft_sdk/Index';
const logConfig = new FTLoggerConfig()
.setLogSamplingRate(100)
.setEnableCustomLog(true)
.setEnableConsoleLog(true, '[business]')
.setLogLevelFilters([
Status.ERROR,
Status.WARN,
Status.INFO,
Status.DEBUG
])
.setEnableLinkRumData(true)
.addGlobalContext('log_source', 'business')
.setLogCacheLimitCount(10000);
FTSDK.installLogConfig(logConfig);
| Method | Type | Required | Description |
|---|---|---|---|
setLogSamplingRate |
number |
No | Sets the log sampling rate. Value range: [0,100]. 0 means no collection, and 100 means collect all logs. Default: 100. Decimal values are supported. Supported in ft_sdk 0.1.18 and later. |
setSamplingRate |
number |
No | Deprecated compatibility API. Value range: [0,1]. Default: 1. Use setLogSamplingRate instead. |
setEnableCustomLog |
boolean |
No | Whether to enable custom logs. Default: false. |
setPrintCustomLogToConsole |
boolean |
No | Whether to also print FTLogger.logBackground(...) custom logs to HiLog. Default: false. Requires setEnableCustomLog(true) and SDK setDebug(true). Output is controlled by setSdkLogLevel(...). See Print Custom Logs to HiLog for details. |
setEnableConsoleLog |
enable: boolean, prefix?: string |
No | Whether to report console logs. This configuration depends on the Hvigor Plugin. Default: false. Supports console.log, debug, info, warn, error, and trace, as well as hilog.debug, info, warn, error, and fatal imported from @ohos.hilog or @kit.PerformanceAnalysisKit. Log level mapping: log, info -> info; debug, trace -> debug; warn -> warn; error -> error; fatal -> fatal. prefix can be a normal message prefix or a DataKit collection rule starting with {. See Console Log Filtering for details. This capability requires ft-sdk 0.1.17 or later and @cloudcare/hvigor-ohos-plugin 0.1.1 or later. |
setEnableLinkRumData |
boolean |
No | Whether to associate RUM data. Default: false. |
setLogLevelFilters |
Array<Status \| string \| { name: string }> |
No | Log level filtering. Valid values: ERROR, WARN, INFO, DEBUG. |
addGlobalContext |
Dictionary |
No | Adds global Log attributes. Supports key,value. For addition rules, see here. |
addGlobalContext |
key: string, value: object |
No | Adds global Log attributes. For addition rules, see here. |
setLogCacheLimitCount |
number |
No | Log data cache limit. Default: 5000. Minimum: 1000. |
setLogCacheDiscardStrategy |
LogCacheDiscard |
No | Sets the log discard rule after the limit is reached. Default: LogCacheDiscard.DISCARD. DISCARD discards appended data, and DISCARD_OLDEST discards old data. |
Console Log Filtering¶
The second parameter of setEnableConsoleLog(true, prefix) filters automatically collected console.* and hilog.* logs:
- When you pass a normal string, the compatible behavior is retained: only logs whose messages start with that string are collected. When you do not pass this parameter, no filtering is applied.
- When you pass a string whose first non-whitespace character is
{, it is treated as an allow rule using DataKit rule syntax. Logs that match the rule enter the SDK queue, and logs that do not match are discarded before they are enqueued. Multiple rule blocks are combined with OR.
Rules can use message, log_source (console or hilog); hilog rules can also use tag and domain. Log levels are still controlled independently by setLogLevelFilters(...).
const logConfig = new FTLoggerConfig().setEnableConsoleLog(true,
"{ log_source = 'hilog' and tag = 'Network' and message match ['^HTTP'] }");
Logger Log Printing¶
Before you use FTLogger to report custom logs, enable setEnableCustomLog(true). The maximum content size per log is 30 KB; any excess is truncated.
Usage¶
/**
* Reports a single custom log.
*
* @param content Log content.
* @param status Log level. Can be `Status` or a level string.
* @param property Additional properties (optional).
* @param isSilence Whether to process silently (optional).
*/
FTLogger.logBackground(
content: string,
status: Status | string,
property?: Map<string, object>,
isSilence?: boolean
): void
Code Example¶
import { FTLogger, Status } from '@guancecloud/ft_sdk/Index';
// Upload a single log.
FTLogger.logBackground('登录成功', Status.INFO);
// Pass additional properties.
const property = new Map<string, object>();
property.set('user_id', new String('user-001'));
FTLogger.logBackground('支付完成', Status.INFO, property);
Log Levels¶
| Method | Description |
|---|---|
| Status.TRACE | Trace |
| Status.DEBUG | Debug |
| Status.INFO | Info |
| Status.WARN | Warning |
| Status.ERROR | Error |
| Status.FATAL | Fatal |
Usage Recommendations¶
setLogLevelFilters(...)controls the reporting level ofFTLoggerbusiness logs and is independent ofsetSdkLogLevel(...)for SDK internal diagnostic logs.setEnableConsoleLog(...)andsetEnableCustomLog(...)are independent: the former automatically collectsconsole.*andhilog.*logs and requires installing and configuring the Hvigor Plugin; the latter is used byFTLoggerto manually report custom logs and does not require the plugin.- During debugging, you can combine
setEnableCustomLog(true),setDebug(true), and SDK debug logs to verify the collection pipeline. - If you want to analyze logs together with user behavior, page, and resource data, enable
setEnableLinkRumData(true)as well. - If log volume is high, control the reported volume through the sampling rate and level filtering.