Log API
Using Bonree's log-sdk Log collection library, you can send logs from your Android application to Bonree with the following features:
- Send logs to Bonree in JSON format.
- Add attributes to each log sent.
- Add Java exceptions to sent logs.
- Log data is preferentially saved to device files to ensure log integrity.
I. Initialization Configuration Interface
AppID and LogConfig address can be obtained from the platform or by contacting technical support.
The initialization configuration class interfaces are configured through chain calls along with BRLog.withAppID("<#AppID#>").start();. Configuration must be done before calling the start function and after withAppID, multiple configurations can be set simultaneously. Configurations after the start function are invalid.
Set Config Address
Private deployment users need to set the private deployment config address during initialization.
- Interface Description
BRLog withConfigAddress(java.lang.String configAddress)
| Parameter | Description | Parameter Constraints | Failure Result |
|---|---|---|---|
| configAddress | Log Config Address | String length > 0 and ≤ 2083, otherwise interface call fails | Agent stops |
- Example
BRLog.withAppID("<#AppID#>")
.withConfigAddress("<#LogConfig Address#>")
.withOpenTrackingData(true)
.withApplicationVersion("v2.4.0")
.start(this);
Set Log Data Reporting Switch
Set the log data reporting switch through this method.
- Interface Description
withConfigAddress withOpenTrackingData(boolean openTrackingData)
| Parameter | Description | Parameter Constraints | Failure Result |
|---|---|---|---|
| openTrackingData | Reporting Switch | Data collection mode [only effective when set at startup], YES: Data sent to remote and stored locally, NO: Data not sent to remote, not stored locally | Agent stops |
- Example
BRLog.withAppID("<#AppID#>")
.withConfigAddress("<#LogConfig Address#>")
.withOpenTrackingData(true)
.withApplicationVersion("v2.4.0")
.start(this);
Set Custom APP Version
Set a custom device App version number through this method. The SDK will report this version number instead of using the default obtained version number.
- Interface Description
BRLog withAppVersion(java.lang.String version)
| Parameter | Description | Parameter Constraints | Failure Result |
|---|---|---|---|
| version | Custom Version Number | String length > 0 and ≤ 256, otherwise interface call fails | Current setting invalid |
- Example
BRLog.withAppID("<#AppID#>")
.withConfigAddress("<#LogConfig Address#>")
.withOpenTrackingData(true)
.withApplicationVersion("v2.4.0")
.start(this);
Set Log Output to Logcat Switch
Set whether logs need to be printed to Logcat when generated.
- Interface Description
BRLog withLogcatEnable(boolean enable)
| Parameter | Description | Parameter Constraints | Failure Result |
|---|---|---|---|
| enable | Logcat Switch | String length > 0 and ≤ 256, otherwise interface call fails | Current setting invalid |
- Example
BRLog.withAppID("<#AppID#>")
.withConfigAddress("<#LogConfig Address#>")
.withOpenTrackingData(true)
.withApplicationVersion("v2.4.0")
.withLogcatEnable(true)
.start(this);
II. Using Logger
Configure a logger to report log data.
The logger configuration class interfaces are configured through chain calls along with Logger.Builder().build();. Configuration must be done before calling the build function and after Builder(), multiple configurations can be set simultaneously.
Set Logger Name
Set the name of the logger through this method.
- Interface Description
Builder setLoggerName(java.lang.String name)
| Parameter | Description | Parameter Constraints | Failure Result |
|---|---|---|---|
| name | Logger Name | String length > 0 and ≤ 256 | First 256 characters taken |
- Example
Logger logger = new Logger.Builder()
.setLoggerName("ViewLogger")
.setLogLevel(LogLevel.DEBUG)
.setPrintLogcat(true)
.setPrefix("[android]")
.addAttribute("deviceId", "9fe5bd60b1e3fd8fd73793c8f565fa0d")
.build();
Set Logger Log Priority
Set the log priority for this logger. Only log messages with priority equal to or higher than the provided level will be collected and reported.
LogLevel is the log level defined by the SDK.
- Interface Description
Builder setLogLevel(com.bonree.log.LogLevel level)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| level | Log Priority | Enum |
- Example
Logger logger = new Logger.Builder()
.setLoggerName("ViewLogger")
.setLogLevel(LogLevel.DEBUG)
.setPrintLogcat(true)
.setPrefix("[android]")
.addAttribute("deviceId", "9fe5bd60b1e3fd8fd73793c8f565fa0d")
.build();
Set Logger Output to Logcat Switch
Set whether logs need to be printed to Logcat when generated. Logs below the collection level will also be output.
- Interface Description
Builder setPrintLogcat(boolean isPrint)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| isPrint | Whether to Output | boolean |
- Example
Logger logger = new Logger.Builder()
.setLoggerName("ViewLogger")
.setLogLevel(LogLevel.DEBUG)
.setPrintLogcat(true)
.setPrefix("[android]")
.addAttribute("deviceId", "9fe5bd60b1e3fd8fd73793c8f565fa0d")
.build();
Set Logger Prefix
Set the prefix to be added when generating logs through this method.
- Interface Description
Builder setPrefix(String pre)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| pre | Prefix | String length > 0 and ≤ 256 |
- Example
Logger logger = new Logger.Builder()
.setLoggerName("ViewLogger")
.setLogLevel(LogLevel.DEBUG)
.setPrintLogcat(true)
.setPrefix("[android]")
.addAttribute("deviceId", "9fe5bd60b1e3fd8fd73793c8f565fa0d")
.build();
Set Logger Additional Attributes
Set additional attributes for each log collected when generating logs through this method.
- Interface Description
Builder addAttribute(String key, String value)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| key | Additional Attribute Name | String length > 0 and ≤ 256 |
| value | Additional Attribute Value | String length > 0 and ≤ 256 |
- Example
Logger logger = new Logger.Builder()
.setLoggerName("ViewLogger")
.setLogLevel(LogLevel.DEBUG)
.setPrintLogcat(true)
.setPrefix("[android]")
.addAttribute("deviceId", "9fe5bd60b1e3fd8fd73793c8f565fa0d")
.build();
III. Generating Logs
Use one of the following functions to generate a log and send it to Bonree.
Output Method: Verbose
Generate a Verbose level log (with additional information) and report it.
- Interface Description
void verbose(String log)
void verbose(String log, Throwable throwable)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| log | Log Content | |
| throwable | Exception Information |
- Example
try {
logger.verbose("A verbose message");
doSomething();
} catch (Exception e) {
logger.verbose("A verbose message", e);
}
Output Method: Debug
Generate a Debug level log (with additional information) and report it.
- Interface Description
void debug(String log)
void debug(String log, Throwable throwable)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| log | Log Content | |
| throwable | Exception Information |
- Example
try {
logger.debug("A debug message");
doSomething();
} catch (Exception e) {
logger.debug("A debug message", e);
}
Output Method: Info
Generate an Info level log (with additional information) and report it.
- Interface Description
void info(String log)
void info(String log, Throwable throwable)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| log | Log Content | |
| throwable | Exception Information |
- Example
try {
logger.info("A info message");
doSomething();
} catch (Exception e) {
logger.info("A info message", e);
}
Output Method: Warn
Generate a Warn level log (with additional information) and report it.
- Interface Description
void warn(String log)
void warn(String log, Throwable throwable)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| log | Log Content | |
| throwable | Exception Information |
- Example
try {
logger.warn("A warn message");
doSomething();
} catch (Exception e) {
logger.warn("A warn message", e);
}
Output Method: Error
Generate an Error level log (with additional information) and report it.
- Interface Description
void error(String log)
void error(String log, Throwable throwable)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| log | Log Content | |
| throwable | Exception Information |
- Example
try {
logger.error("A error message");
doSomething();
} catch (Exception e) {
logger.error("A error message", e);
}
Generate Logs with Additional Attributes
Generate a log with additional information. Up to 64 additional attributes can be set.
- Interface Description
void log(LogLevel level, String log, Throwable throwable, Map<String, String> attributes)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| level | Log Level | String length > 0 and ≤ 256 |
| log | Log Content | String length > 0 and ≤ 256 |
| throwable | Exception Information | |
| attributes | Additional Information | Count ≤ 64 |
- Example
Map<String, String> attributeMap = new HashMap<>();
attributeMap.put("key", "value");
attributeMap.put("url", url);
logger.log(LogLevel.DEBUG, "custom log", null, attributeMap);
Add Additional Attributes to Logger
Set additional attributes in the logger. Each log generated by this logger will subsequently include these additional attributes. These attributes can also be removed.
- Interface Description
void addAttribute(String key, String value)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| key | Additional Attribute Name | String length > 0 and ≤ 256 |
| value | Additional Attribute Value | String length > 0 and ≤ 256 |
- Example
logger.addAttribute("version_code", versionCode);
Remove Additional Attributes from Logger
Remove additional attributes from the logger. Subsequently generated logs will not include these additional attributes.
- Interface Description
void removeAttributeByKey(String key)
| Parameter | Description | Parameter Constraints |
|---|---|---|
| key | Additional Attribute Name | String length > 0 and ≤ 256 |
- Example
logger.removeAttributeByKey("version_code");