openharmony 鸿蒙 js-apis-hiappevent

2025-06-12 浏览 (1)

@ohos.hiAppEvent (Application Event Logging)

The hiAppEvent module provides the application event logging functions, such as writing application events to the event file and managing the event logging configuration.

NOTE

  • The APIs provided by this module are deprecated since API version 9. You are advised to use @ohos.hiviewdfx.hiAppEvent.
  • The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import hiAppEvent from '@ohos.hiAppEvent';

How to Use

Before using application event logging, you need to understand the requirements for related parameters.

Event Name

An event name is a string that contains a maximum of 48 characters, including the dollar sign ($), digits (0 to 9), letters (a to z), and underscore (_). It must start with a letter or dollar sign ($) and end with a digit or letter.

Event Type

An event type is an enumerated value of EventType.

Event Parameter

An event parameter is an object in key-value pair format, where the key is the parameter name and the value is the parameter value. The requirements are as follows:

  • A parameter name is a string that contains a maximum of 32 characters, including the dollar sign ($), digits (0 to 9), letters (a to z), and underscore (_). It must start with a letter or dollar sign ($) and end with a digit or letter.
  • A parameter value can be of the string, number, boolean, or array type.
  • If the parameter value is a string, its maximum length is 8*1024 characters. If this limit is exceeded, excess characters will be discarded.
  • If the parameter value is a number, the value must be within the range of Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER. Otherwise, uncertain values may be generated.
  • If the parameter value is an array, the elements in the array must be of the same type, which can only be string, number, or boolean. In addition, the number of elements must be less than 100. If this limit is exceeded, excess elements will be discarded.
  • The maximum number of parameters is 32. If this limit is exceeded, excess parameters will be discarded.

Event Callback

Event callback can be a callback or promise that carries the return value obtained by invoking the event logging API. You can add the processing of the return value in the event callback as follows:

  • If the return value is 0, the event verification is successful, and the event will be directly written to the event file.
  • If the return value is greater than 0, invalid parameters are present in the event, and the event will be written to the event file after the invalid parameters are ignored.
  • If the return value is smaller than 0, the event parameter verification fails, and the event will not be written to the event file.

hiAppEvent.write

write(eventName: string, eventType: EventType, keyValues: object, callback: AsyncCallback<void>): void

Writes event information to the event file of the current day. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.HiviewDFX.HiAppEvent

Parameters

NameTypeMandatoryDescription
eventNamestringYesEvent name.
eventTypeEventTypeYesEvent type.
keyValuesobjectYesEvent parameters.
callbackAsyncCallback<void>YesEvent callback.

Example

import { BusinessError } from '@ohos.base'

let eventParams: Record<string, number|string> = {
  "int_data": 100,
  "str_data": "strValue",
};
hiAppEvent.write("test_event", hiAppEvent.EventType.FAULT, eventParams, (err: BusinessError) => {
  if (err) {
    // Event writing error: Write the event to the event file after the invalid parameters in the event are ignored, or stop writing the event if the event verification fails.
    console.error(`failed to write event, code=${err.code}`);
    return;
  }
  // Event writing success
  console.log(`success to write event`);
});

hiAppEvent.write

write(eventName: string, eventType: EventType, keyValues: object): Promise<void>

Writes event information to the event file of the current day. This API uses a promise to return the result.

System capability: SystemCapability.HiviewDFX.HiAppEvent

Parameters

NameTypeMandatoryDescription
eventNamestringYesEvent name.
eventTypeEventTypeYesEvent type.
keyValuesobjectYesEvent parameters.

Return value

TypeDescription
Promise<void>Promise used to asynchronously process the callback in the then() and catch() methods when event writing succeeded or failed.

Example

import { BusinessError } from '@ohos.base'

let eventParams: Record<string, number|string> = {
  "int_data": 100,
  "str_data": "strValue",
};
hiAppEvent.write("test_event", hiAppEvent.EventType.FAULT, eventParams).then(() => {
  // Event writing success
  console.log(`success to write event`);
}).catch((err: BusinessError) => {
  // Event writing error: Write the event to the event file after the invalid parameters in the event are ignored, or stop writing the event if the event verification fails.
  console.error(`failed to write event, code=${err.code}`);
});

hiAppEvent.configure

configure(config: ConfigOption): boolean

Configures the application event logging function, such as setting the event logging switch and maximum size of the directory that stores the event logging files.

System capability: SystemCapability.HiviewDFX.HiAppEvent

Parameters

NameTypeMandatoryDescription
configConfigOptionYesConfiguration items for application event logging.

Return value

TypeDescription
booleanReturns true if the configuration is successful; returns false otherwise.

Example

// Set the application event logging switch.
let config1: hiAppEvent.ConfigOption = {
  disable: true,
};
hiAppEvent.configure(config1);

// Configure the maximum size of the directory that stores the event logging files.
let config2: hiAppEvent.ConfigOption = {
  maxStorage: '100M',
};
hiAppEvent.configure(config2);

ConfigOption

Provides the configuration items for application event logging.

System capability: SystemCapability.HiviewDFX.HiAppEvent

NameTypeMandatoryDescription
disablebooleanNoApplication event logging switch. The value true means to disable the application event logging function, and the value false means the opposite.
maxStoragestringNoMaximum size of the event file storage directory. The default value is 10M. If the specified size is exceeded, the oldest event logging files in the directory will be deleted to free up space.

EventType

Enumerates the event types.

System capability: SystemCapability.HiviewDFX.HiAppEvent

NameValueDescription
FAULT1Fault event.
STATISTIC2Statistical event.
SECURITY3Security event.
BEHAVIOR4Behavior event.

Event

Provides constants that define the names of all predefined events.

System capability: SystemCapability.HiviewDFX.HiAppEvent

NameTypeReadableWritableDescription
USER_LOGINstringYesNoUser login event.
USER_LOGOUTstringYesNoUser logout event.
DISTRIBUTED_SERVICE_STARTstringYesNoDistributed service startup event.

Param

Provides constants that define the names of all predefined event parameters.

System capability: SystemCapability.HiviewDFX.HiAppEvent

NameTypeReadableWritableDescription
USER_IDstringYesNoCustom user ID.
DISTRIBUTED_SERVICE_NAMEstringYesNoDistributed service name.
DISTRIBUTED_SERVICE_INSTANCE_IDstringYesNoDistributed service instance ID.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Performance Analysis Kit

harmony 鸿蒙Performance Analysis Kit

harmony 鸿蒙HiAppEvent

harmony 鸿蒙HiAppEvent_AppEventGroup

harmony 鸿蒙HiAppEvent_AppEventInfo

harmony 鸿蒙HiCollie

harmony 鸿蒙HiCollie_DetectionParam

harmony 鸿蒙HiCollie_SetTimerParam

harmony 鸿蒙HiDebug

harmony 鸿蒙HiDebug_MemoryLimit

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/q4Ozlw