IoT Core 日志 SDK (Java)
更新时间:2021-07-16
安装
环境准备
- 运行环境 Java SDK工具包可在 jdk1.6、jdk1.7、jdk1.8环境下运行。
-
鉴权和认证 要使用百度智能云产品,您需要拥有一个百度智能云账号和一个有效的 AK(Access Key ID)、SK(Secret Access Key)用来进行签名认证。
可以通过如下步骤获得并了解您的AK/SK信息:
下载和安装
使用Maven安装
在 Maven 中添加 iotcore-log-sdk 的依赖:
Plain Text
1<dependency>
2 <groupId>com.baidu.iot</groupId>
3 <artifactId>iotcore-log-sdk</artifactId>
4 <version>1.0.3</version>
5</dependency>
附此 SDK 的 Github 开源地址:https://github.com/baidu/iotcore-sdk-java
快速入门
配置并创建IotCoreLogger
Plain Text
1String iotCoreId = "yourIoTCoreId"; // 天工平台创建的iotcore
2String username = "yourIoTCoreId/yourDeviceKey"; // 天工平台创建的设备用户名或签名
3char[] password = "yourDeviceSecret".toCharArray(); // 天工平台创建的设备密码或签名密钥
4
5// 获取mqtt连接信息配置,可选tcp、tls等多种配置方式,详见MqttConfigFactory实现
6MqttConfig mqttConfig = MqttConfigFactory.genPlainMqttConfig(iotCoreId, username, password);
7
8// 创建IotCoreLogger配置信息
9Config config = Config.builder()
10 .iotCoreId(iotCoreId)
11 .mqttConfig(mqttConfig)
12 .logLevel(LogLevel.INFO)
13 .includeLowerLevel(true)
14 .clientPoolSize(3)
15 .build();
16// 创建IotCoreLogger实例,用于接收日志
17IotCoreLoggerRegister register = new IotCoreLoggerRegister();
18IotCoreLogger logger = register.registerLogger(config).blockingGet();
Config参数说明
- iotCoreId: 天工平台创建的iotcore id。
- groupKey: 日志接收者所属的group,同一group中的IotCoreLogger共享所有的日志信息,默认为每次不同的随机值。可参考kafka consumer group进行理解。
- logLevel: 接收日志的级别,可选ERROR、WARN、INFO、DEBUG,级别由低至高。
- includeLowerLevel: 所选日志级别是否包含低级,如选择接收INFO级别日志,默认包含WARN及ERROR级别日志。
- deviceKeys: 执行设备名称列表,只接收特定设备的运行日志。
- clientPoolSize: 内部创建的mqtt client数量,最大100。可根据所接收的日志规模及速率适量选取,每个client最低支持100 qps的日志。
- mqttConfig: 内部mqtt client使用相关配置,使用百度云创建的日志设备身份信息进行创建。
接收日志
日志接收提供rx风格的订阅接口,使用示例如下:
Plain Text
1logger.receive().subscribeWith(new DisposableObserver<LogEntry>() {
2 @Override
3 public void onNext(@NonNull LogEntry logEntry) {
4 // handle logEntry
5 System.out.println(logEntry);
6 }
7
8 @Override
9 public void onError(@NonNull Throwable e) {
10 // handle error
11 e.printStackTrace();
12 dispose();
13 }
14
15 @Override
16 public void onComplete() {
17 dispose();
18 }
19});
日志解析
sdk提供的日志LogEntry为proto中定义的原始日志格式,如需进一步的解析details中key值的含义,可参考iotcore-logger-standalone中的解析方式。
#零代码快速使用 为了进一步降低iotcore-log-sdk的理解及使用难度,提供了iotcore-logger-standalone模块,可以直接下载运行。运行参数如下:
Plain Text
1Usage: <main class> [options]
2 Options:
3 --clientCount
4 Total test client count
5 Default: 3
6 --deviceKeys
7 Logs only from the specific devices would arrive
8 Default: null
9 --help
10 Show help
11 --includeLowerLevel
12 Whether the log level should include the lower levels, such as INFO leven would include WARN and ERROR
13 Default: true
14 * --iotCoreId
15 Receive logs from this iotCore
16 --level
17 Level of the received logs: ERROR, WARN, INFO, DEBUG
18 Default: INFO
19 * --password
20 Mqtt client password
21 --uri
22 Mqtt broker uri
23 * --username
24 Mqtt client username
快速使用示例:
Plain Text
1nohup java -jar iotcore-logger-standalone.jar --iotCoreId axxxxxx --username axxxxxx/test --password hubKQZRMYaQXDgDe > logsdk.log 2>&1 &