Log API
1. Startup Configuration
| Property | Value | Default | Required | Description |
|---|---|---|---|---|
| appID | string | None | Yes | Application ID |
| uploadAddrHttp | string | None | Yes | Data upload address for HTTP. One of uploadAddrHttp or uploadAddrHttps must be configured. HTTP pages must configure uploadAddrHttp |
| uploadAddrHttps | string | None | Yes | Data upload address for HTTPS. One of uploadAddrHttp or uploadAddrHttps must be configured. HTTPS pages must configure uploadAddrHttps |
| uploadPeriod | string | 15 | No | Reporting frequency in seconds |
| enable | boolean | true | No | Controls SDK activation, enabled by default |
| debug | boolean | false | No | Whether to enable logger output, debug mode, disabled by default |
| consoleCollector | ['log','info','debug','warn','error'] | [] | No | Configures whether to collect log data from console.log \ info \ debug \ warn \ error, disabled by default |
| netCollector | boolean | false | No | Configures whether to collect network error data, disabled by default |
| errorCollector | boolean | false | No | Configures whether to collect JS error data, disabled by default |
Note: If Bonree JS-SDK is used in the page and it cannot be guaranteed that JS-SDK initializes before Log-SDK, do not configure netCollector and errorCollector, as this may cause missing error or network data.
For web applications embedding BonreeLog, initialize and configure the SDK through the start method:
window.BRLog && window.BRLog.start({
appID: "<#AppID#>",
uploadAddrHttp: "http://<#LogUpload Address#>",
uploadAddrHttps: "https://<#LogUpload Address#>",
consoleCollector: ['log','info','debug','warn','error']
})
If BonreeLog is introduced into the project via NPM, initialize and configure it through the BrLogStart method:
import {BRLogStart} from 'bonree-log-jsagent'
......
BrLogStart({
appID: '<#AppID#>',
uploadAddrHttp: 'http://<#LogUpload Address#>',
uploadAddrHttps: 'https://<#LogUpload Address#>',
consoleCollector: ['log','info','debug','warn','error']
})
AppID and LogUpload Address can be obtained from the platform or by contacting technical support.
2. Custom Function Interfaces
Custom Logs
After BonreeLog is successfully initialized, call the relevant APIs on the logger object to directly send log data of different levels to the platform. BonreeLog supports four levels of logs: debug, info, warn, error, with increasing severity.
logger.debug(message, error)
logger.info(message, error)
logger.warn(message, error)
logger.error(message, error)
- Interface Description
| Parameter | Type | Description |
|---|---|---|
| message | String | Log data. Required parameter |
| error | Error | Error type. Optional parameter |
- Example
try {
...
throw new Error('Wrong behavior')
...
} catch (error) {
window.BRLog && window.BRLog.logger.error('Error occurred', error)
}
Fully Custom Logs
After BonreeLog is successfully initialized, use the API to directly send custom logs to the platform. Supports custom log levels (debug, info, warn, error), custom log data via message, Error objects, and custom attributes. The attribute parameter only supports objects in {key:value,...} format, and the value should be of string type.
logger.log(level, message, error, attribute)
- Interface Description
| Parameter | Type | Description |
|---|---|---|
| level | String | Log level. Only supports one of the four log levels: debug, info, warn, error. Required parameter |
| message | String | Log data. Required parameter |
| error | Error | Event label. Optional parameter |
| attribute | String | Additional information. Optional parameter |
- Example
try {
...
throw new Error('Minor misbehavior')
...
} catch (error) {
window.BRLog && window.BRLog.logger.log('info', 'Minor misbehavior occurs', error, {JSON: 'JSON parsing failed'})
}
Filter Logs
After BonreeLog is successfully initialized, use the API to set the minimum log level to be recorded. Log data below this level will be filtered. Log levels are: debug < info < warn < error.
setLevel(level)
- Interface Description
| Parameter | Type | Description |
|---|---|---|
| level | String | Log level. Only supports one of the four log levels: debug, info, warn, error. Required parameter |
-
Example
After successfully calling the method in the example, debug-level logs in subsequent log data reported to the platform will be filtered. Info, warn, and error-level log data will be reported normally.
window.BRLog && window.BRLog.logger.setLevel('info')
Set Log Prefix
After BonreeLog is successfully initialized, use the API to set the log prefix.
setPrefix(prefix)
- Interface Description
| Parameter | Type | Description |
|---|---|---|
| prefix | String | Log prefix. Required parameter |
-
Example
After successfully calling the method in the example, subsequent log data reported to the platform will include the "bonree" prefix.
window.BRLog && window.BRLog.setPrefix('bonree')
Add Attribute
After BonreeLog is successfully initialized, use the API to add global log attributes.
addAttribute(key, value)
- Interface Description
| Parameter | Type | Description |
|---|---|---|
| key | String | Attribute name. Required parameter |
| value | String | Attribute value. Required parameter |
- Example
window.BRLog && window.BRLog.addAttribute('original', 'logger')
Remove Attribute
After BonreeLog is successfully initialized, use the API to remove added global log attributes.
removeAttribute(key)
- Interface Description
| Parameter | Type | Description |
|---|---|---|
| key | String | Attribute name. Required parameter |
- Example
window.BRLog && window.BRLog.removeAttribute('original')