Skip to main content
Version: 3.7.0

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)
ParameterDescriptionParameter ConstraintsFailure Result
configAddressLog Config AddressString length > 0 and ≤ 2083, otherwise interface call failsAgent 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)
ParameterDescriptionParameter ConstraintsFailure Result
openTrackingDataReporting SwitchData collection mode [only effective when set at startup], YES: Data sent to remote and stored locally, NO: Data not sent to remote, not stored locallyAgent 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)
ParameterDescriptionParameter ConstraintsFailure Result
versionCustom Version NumberString length > 0 and ≤ 256, otherwise interface call failsCurrent 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)
ParameterDescriptionParameter ConstraintsFailure Result
enableLogcat SwitchString length > 0 and ≤ 256, otherwise interface call failsCurrent 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)
ParameterDescriptionParameter ConstraintsFailure Result
nameLogger NameString length > 0 and ≤ 256First 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)
ParameterDescriptionParameter Constraints
levelLog PriorityEnum
  • 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)
ParameterDescriptionParameter Constraints
isPrintWhether to Outputboolean
  • 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)
ParameterDescriptionParameter Constraints
prePrefixString 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)
ParameterDescriptionParameter Constraints
keyAdditional Attribute NameString length > 0 and ≤ 256
valueAdditional Attribute ValueString 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)
ParameterDescriptionParameter Constraints
logLog Content
throwableException 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)
ParameterDescriptionParameter Constraints
logLog Content
throwableException 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)
ParameterDescriptionParameter Constraints
logLog Content
throwableException 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)
ParameterDescriptionParameter Constraints
logLog Content
throwableException 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)
ParameterDescriptionParameter Constraints
logLog Content
throwableException 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)
ParameterDescriptionParameter Constraints
levelLog LevelString length > 0 and ≤ 256
logLog ContentString length > 0 and ≤ 256
throwableException Information
attributesAdditional InformationCount ≤ 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)
ParameterDescriptionParameter Constraints
keyAdditional Attribute NameString length > 0 and ≤ 256
valueAdditional Attribute ValueString 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)
ParameterDescriptionParameter Constraints
keyAdditional Attribute NameString length > 0 and ≤ 256
  • Example
logger.removeAttributeByKey("version_code");