错误日志,慢日志的查询
初始化
参数获取可参照准备工作中华为云SDK认证方式。
Copied!
String authUrl = "****************";
String user = "****************";
String password = "****************";
String projectId = "****************";
String userDomainId = "****************";
Config config = Config.newConfig().withLanguage("zh-cn").withSSLVerificationDisabled();
OSClient.OSClientV3 osclient = OSFactory.builderV3().withConfig(config).endpoint(authUrl)
.credentials(user, password, Identifier.byId(userDomainId))
.scopeToProject(Identifier.byId(projectId)).authenticate();
InstanceManageService instanceManageService = osclient.rds().instanceManage();
查询数据库错误日志
分页查询数据库最近的2000条错误日志信息
Copied!
public static void ListErrorLogTest(InstanceManageService instanceManageService, String instanceId) {
Map<String, String> filteringParams = new HashMap<String, String>();
filteringParams.put("offset", "1");
filteringParams.put("limit", "10");
filteringParams.put("start_date", "2019-09-30T10:41:14+0800");
filteringParams.put("end_date", "2019-10-16T10:41:14+0800");
try {
if (instanceManageService == null) {
System.out.println("[Error information]Get authenticate is null!");
return;
}
ErrorLogResponse rep = instanceManageService.listErrorLog(filteringParams, instanceId);
if (rep.getTotalRecord() != 0) {
System.out.println(rep.getErrorLogList());
for (ErrorLogList errorLogList : rep.getErrorLogList()) {
System.out.println(errorLogList.getContent());
}
} else {
System.out.println("Get ErrorLogList number is 0 !");
}
} catch (ServerResponseException e) {
System.out.println(e.getStatusCode());
}
}
响应样例
Copied!
{
"error_log_list": [{
"time": "2018-12-04T14:24:42",
"level": "ERROR",
"content": "Slave I/O for channel '': error connecting to master 'rdsRepl@172.**.30.111:3306' - retry-time: 60 retries: 1, Error_code: 203"
}, {
"time": "2018-12-04T14:24:42",
"level": "ERROR",
"content": "Slave I/O for channel '': error connecting to master 'rdsRepl@172.**.11.111:8081' - retry-time: 60 retries: 1, Error_code: 203"
}],
"total_record": 2
}
说明
:根据日志级别等查询请参考API Explorer。
查询数据库慢日志
分页查询数据库最近的2000条慢日志信息
Copied!
public static void ListSlowLogTest(InstanceManageService instanceManageService, String instanceId) {
Map<String, String> filteringParams = new HashMap<String, String>();
filteringParams.put("offset", "1");
filteringParams.put("limit", "10");
filteringParams.put("start_date", "2019-09-30T10:41:14+0800");
filteringParams.put("end_date", "2019-10-16T10:41:14+0800");
try {
if (instanceManageService == null) {
System.out.println("[Error information]Get authenticate is null!");
return;
}
SlowLogListResponse rep = instanceManageService.listSlowLog(filteringParams, instanceId);
if (rep.getTotalRecord() != 0) {
System.out.println(rep.getSlowLogList());
for (SlowLogList slowLogList : rep.getSlowLogList()) {
System.out.println(slowLogList.getDatabase());
}
} else {
System.out.println("Get SlowLogList number is 0 !");
}
} catch (ServerResponseException e) {
System.out.println(e.getStatusCode());
}
}
响应样例
Copied!
{
"total_record": 1,
"slow_log_list": [
{
"count": "1",
"time": "1.04899 s",
"lock_time": "0.00003 s",
"rows_sent": "0",
"rows_examined": "0",
"database": "mysql",
"users": "root",
"query_sample": "INSERT INTO time_zone_name (Name, Time_zone_id) VALUES (N, @time_zone_id);",
"type": "INSERT",
"start_time": "2018-08-06T10:41:14",
"client_ip": "192.*.*.1"
}
]
}
说明
:
版本说明
本示例基于华为云SDK V3.0版本开发。
功能介绍
云数据库RDS服务的日志管理功能支持查看数据库级别的日志,包括数据库运行的错误信息,以及运行较慢的SQL查询语句,有助于您分析系统中存在的问题。下面您可以通过本示例了解如何查询错误日志或慢日志。
准备工作
错误日志,慢日志的查询
初始化
参数获取可参照准备工作中华为云SDK认证方式。
// endpoint url String authUrl = "****************"; String user = "****************"; String password = "****************"; String projectId = "****************"; String userDomainId = "****************"; Config config = Config.newConfig().withLanguage("zh-cn").withSSLVerificationDisabled(); OSClient.OSClientV3 osclient = OSFactory.builderV3().withConfig(config).endpoint(authUrl) .credentials(user, password, Identifier.byId(userDomainId)) .scopeToProject(Identifier.byId(projectId)).authenticate(); InstanceManageService instanceManageService = osclient.rds().instanceManage();
查询数据库错误日志
分页查询数据库最近的2000条错误日志信息
public static void ListErrorLogTest(InstanceManageService instanceManageService, String instanceId) { Map<String, String> filteringParams = new HashMap<String, String>(); filteringParams.put("offset", "1"); filteringParams.put("limit", "10"); filteringParams.put("start_date", "2019-09-30T10:41:14+0800"); filteringParams.put("end_date", "2019-10-16T10:41:14+0800"); try { if (instanceManageService == null) { System.out.println("[Error information]Get authenticate is null!"); return; } ErrorLogResponse rep = instanceManageService.listErrorLog(filteringParams, instanceId); if (rep.getTotalRecord() != 0) { System.out.println(rep.getErrorLogList()); for (ErrorLogList errorLogList : rep.getErrorLogList()) { System.out.println(errorLogList.getContent()); } } else { System.out.println("Get ErrorLogList number is 0 !"); } } catch (ServerResponseException e) { System.out.println(e.getStatusCode()); } }
响应样例
说明
:根据日志级别等查询请参考API Explorer。查询数据库慢日志
分页查询数据库最近的2000条慢日志信息
public static void ListSlowLogTest(InstanceManageService instanceManageService, String instanceId) { Map<String, String> filteringParams = new HashMap<String, String>(); filteringParams.put("offset", "1"); filteringParams.put("limit", "10"); filteringParams.put("start_date", "2019-09-30T10:41:14+0800"); filteringParams.put("end_date", "2019-10-16T10:41:14+0800"); try { if (instanceManageService == null) { System.out.println("[Error information]Get authenticate is null!"); return; } SlowLogListResponse rep = instanceManageService.listSlowLog(filteringParams, instanceId); if (rep.getTotalRecord() != 0) { System.out.println(rep.getSlowLogList()); for (SlowLogList slowLogList : rep.getSlowLogList()) { System.out.println(slowLogList.getDatabase()); } } else { System.out.println("Get SlowLogList number is 0 !"); } } catch (ServerResponseException e) { System.out.println(e.getStatusCode()); } }
响应样例
说明
:参考
更多信息请参考云数据库 RDS。