Skip to content

Log Configuration

This article describes the HarmonyOS Log initialization configuration.

Initialization Configuration

import { FTSDK, FTLoggerConfig, Status } from '@guancecloud/ft_sdk/Index';

const logConfig = new FTLoggerConfig()
  .setSamplingRate(1.0)
  .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
setSamplingRate number No Log sampling rate, range [0.0, 1.0], default 1.0
setEnableCustomLog boolean No Whether to enable custom logs, default false
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, trace, as well as hilog.debug, info, warn, error, 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. This feature requires ft-sdk 0.1.17 or later and @cloudcare/hvigor-ohos-plugin 0.1.1 or later
setEnableLinkRumData boolean No Whether to link RUM data, default false
setLogLevelFilters Array<Status | string | { name: string }> No Log level filter, optional values: ERROR, WARN, INFO, DEBUG
addGlobalContext Dictionary No Add Log global attributes. Supports key,value. See here for adding rules
addGlobalContext key: string, value: object No Add Log global attributes. See here for adding rules
setLogCacheLimitCount number No Log data cache count limit, default 5000, minimum 1000
setLogCacheDiscardStrategy LogCacheDiscard No Sets the log discard rule after the log cache reaches the limit. Defaults to LogCacheDiscard.DISCARD. DISCARD discards appended data, while DISCARD_OLDEST discards old data

Console Log Filtering

The second parameter of setEnableConsoleLog(true, prefix) is used to filter automatically collected console.* and hilog.* logs:

  • When a normal string is passed, the compatible behavior is retained: only logs whose message starts with the string are collected. If not passed, no filtering is performed.
  • When a string whose first non-whitespace character is { is passed, it is treated as an allow rule according to the DataKit rule syntax. Logs matching the rule enter the SDK queue; unmatched logs are discarded before enqueueing. Multiple rule blocks are ORed together.

The rule can use message and log_source (console or hilog); hilog can also use tag and domain. Log levels remain independently controlled by setLogLevelFilters(...).

const logConfig = new FTLoggerConfig().setEnableConsoleLog(true,
  "{ log_source = 'hilog' and tag = 'Network' and message match ['^HTTP'] }");

Logger Logging

Before using FTLogger to report custom logs, you need to enable setEnableCustomLog(true). A single log entry can be up to 30 KB; anything beyond that is truncated.

Usage

/**
 * Reports a single custom log.
 *
 * @param content Log content.
 * @param status Log level. Accepts `Status` or a level string.
 * @param property Additional attributes (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';

// Report a single log.
FTLogger.logBackground('登录成功', Status.INFO);

// Pass additional attributes.
const property = new Map<string, object>();
property.set('user_id', new String('user-001'));
FTLogger.logBackground('支付完成', Status.INFO, property);

Log Levels

Method Meaning
Status.TRACE Trace
Status.DEBUG Debug
Status.INFO Info
Status.WARN Warning
Status.ERROR Error
Status.FATAL Fatal

Recommendations

  • setLogLevelFilters(...) controls the reporting level of FTLogger business logs. It is independent of setSdkLogLevel(...), which controls SDK internal diagnostic logs.
  • setEnableConsoleLog(...) and setEnableCustomLog(...) are independent of each other: the former is used to automatically collect console.* and hilog.* logs and requires installing and configuring the Hvigor Plugin; the latter is used to manually report custom logs via FTLogger and does not require installing the plugin.
  • During debugging, you can use setEnableCustomLog(true), setDebug(true), and the SDK Debug Logs together to verify the collection pipeline.
  • If you want to analyze logs together with user behavior, page, and resource data, it is recommended to also enable setEnableLinkRumData(true).
  • If the log volume is large, you can control the reporting volume through the sampling rate and level filtering.

Feedback

Is this page helpful?