CLS 日志
查询和管理云开发环境的 CLS(日志服务)。通过 app.log 访问。
版本提示
自 v5.0.0 起支持 app.log 直接调用方式。v5.0.0 之前的版本请使用 commonService 方式。
checkLogServiceEnabled
1. 接口描述
接口功能:检查当前环境的 CLS 日志服务是否已开通
接口声明:app.log.checkLogServiceEnabled(): Promise<boolean>
2. 输入参数
无
3. 返回结果
true 表示已开通,false 表示未开通或开通中。
4. 示例代码
import CloudBase from '@cloudbase/manager-node'
const app = CloudBase.init({
secretId: 'Your SecretId',
secretKey: 'Your SecretKey',
envId: 'Your envId'
})
async function test() {
const enabled = await app.log.checkLogServiceEnabled()
if (!enabled) {
console.log('日志服务未开通,正在开通...')
await app.log.createLogService()
}
console.log('日志服务状态:已开通')
}
test()
createLogService
1. 接口描述
接口功能:为当前环境开通 CLS 日志服务
接口声明:app.log.createLogService(): Promise<Object>
2. 输入参数
无
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| RequestId | String | 请求唯一标识 |
searchClsLog
1. 接口描述
接口功能:查询指定条件的 CLS 日志
接口声明:app.log.searchClsLog(params): Promise<ISearchClsLogResponse>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| queryString | 是 | String | 查询语句,详情参考。支持 module:database、module:rdb AND eventType:MysqlSlowQuery 等语法 |
| StartTime | 是 | String | 查询起始时间,格式:YYYY-MM-DD HH:mm:ss |
| EndTime | 是 | String | 查询结束时间,格式:YYYY-MM-DD HH:mm:ss |
| Limit | 是 | Number | 单次返回最大日志条数,最大值为 100 |
| Context | 否 | String | 翻页游标,来自上次返回的 Context 字段 |
| Sort | 否 | String | 排序方式:asc(升序)或 desc(降序),默认 desc |
| UseLucene | 否 | Boolean | 是否使用 Lucene 语法,默认 false |
| service | 否 | String | 服务类型:tcb(云开发,默认)或 tcbr(云托管) |
queryString 支持的 module 过滤
| module 值 | 说明 | 支持的 eventType |
|---|---|---|
database | 云数据库(文档型) | MongoSlowQuery |
rdb | 云数据库(SQL 型) | MysqlFreeze、MysqlRecover、MysqlSlowQuery |
model | 数据模型 | — |
workflow | 审批流 | — |
auth | 用户权限 | — |
app | 低代码应用 | AppProdPub、AppProdDel |
llm | 大模型 | 通过 logType:llm-tracelog 过滤 |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| RequestId | String | 请求唯一标识 |
| Context | String | 翻页游标,用于下次查询传入 |
| ListOver | Boolean | 是否已返回全部日志 |
| Results | IClsLogObject[] | 日志列表 |
IClsLogObject
| 字段 | 类型 | 说明 |
|---|---|---|
| TopicId | String | 日志属于的 topic ID |
| TopicName | String | 日志主题名称 |
| Timestamp | String | 日志时间 |
| Content | String | 日志内容 |
| FileName | String | 采集路径 |
| Source | String | 日志来源设备 |
4. 示例代码
import CloudBase from '@cloudbase/manager-node'
const app = CloudBase.init({
secretId: 'Your SecretId',
secretKey: 'Your SecretKey',
envId: 'Your envId'
})
async function test() {
const now = new Date()
const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000)
const formatDate = (d) => d.toISOString().replace('T', ' ').slice(0, 19)
const res = await app.log.searchClsLog({
queryString: 'module:database AND eventType:MongoSlowQuery',
StartTime: formatDate(oneHourAgo),
EndTime: formatDate(now),
Limit: 100,
Sort: 'desc'
})
for (const log of res.Results || []) {
console.log(`[${log.Timestamp}] ${log.Content}`)
}
}
test()
commonService 兼容方式
以下方式通过 commonService 调用,与 app.log 方式功能等价,同样受支持。适用于需要兼容 v5.0.0 之前版本的场景。
⚠️ 本接口从 3.0.0 版本后,commonService 作为方法使用,请求参数为
(service?:string, version?:string),属于不兼容变更
1. 接口描述
接口功能:查询指定条件的 CLS 日志
接口声明:manager.commonService().call({ Action: 'SearchClsLog', Param: {}}): Promise<Object>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| EnvId | 是 | String | 环境 ID |
| StartTime | 是 | String | 查询起始时间,格式 YYYY-MM-DD HH:mm:ss |
| EndTime | 是 | String | 查询结束时间,格式 YYYY-MM-DD HH:mm:ss |
| QueryString | 是 | String | 查询语句,详情参考 |
| Limit | 是 | String | 单次要返回的日志条数,最大为 100 |
| Sort | 否 | Boolean | 按时间排序 asc(升序)或 desc(降序),默认 desc |
| UseLucene | 否 | Boolean | 是否使用 Lucene 语法,默认为 false |
3. 示例代码
import CloudBase from "@cloudbase/manager-node";
const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId"
});
async function test() {
const clsLogRes = await manager.commonService().call({
Action: "SearchClsLog",
Param: {
EnvId: "your-env-id",
StartTime: "2024-01-01 00:00:00",
EndTime: "2024-01-02 00:00:00",
Limit: 100,
Sort: "desc",
QueryString: `request_id: xxx`
}
});
console.log("clsLogRes", clsLogRes);
}
test();