跳到主要内容

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. 返回结果

字段类型说明
RequestIdString请求唯一标识

searchClsLog

1. 接口描述

接口功能:查询指定条件的 CLS 日志

接口声明:app.log.searchClsLog(params): Promise<ISearchClsLogResponse>

2. 输入参数

字段必填类型说明
queryStringString查询语句,详情参考。支持 module:databasemodule:rdb AND eventType:MysqlSlowQuery 等语法
StartTimeString查询起始时间,格式:YYYY-MM-DD HH:mm:ss
EndTimeString查询结束时间,格式:YYYY-MM-DD HH:mm:ss
LimitNumber单次返回最大日志条数,最大值为 100
ContextString翻页游标,来自上次返回的 Context 字段
SortString排序方式:asc(升序)或 desc(降序),默认 desc
UseLuceneBoolean是否使用 Lucene 语法,默认 false
serviceString服务类型:tcb(云开发,默认)或 tcbr(云托管)

queryString 支持的 module 过滤

module 值说明支持的 eventType
database云数据库(文档型)MongoSlowQuery
rdb云数据库(SQL 型)MysqlFreezeMysqlRecoverMysqlSlowQuery
model数据模型
workflow审批流
auth用户权限
app低代码应用AppProdPubAppProdDel
llm大模型通过 logType:llm-tracelog 过滤

3. 返回结果

字段类型说明
RequestIdString请求唯一标识
ContextString翻页游标,用于下次查询传入
ListOverBoolean是否已返回全部日志
ResultsIClsLogObject[]日志列表

IClsLogObject

字段类型说明
TopicIdString日志属于的 topic ID
TopicNameString日志主题名称
TimestampString日志时间
ContentString日志内容
FileNameString采集路径
SourceString日志来源设备

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. 输入参数

字段必填类型说明
EnvIdString环境 ID
StartTimeString查询起始时间,格式 YYYY-MM-DD HH:mm:ss
EndTimeString查询结束时间,格式 YYYY-MM-DD HH:mm:ss
QueryStringString查询语句,详情参考
LimitString单次要返回的日志条数,最大为 100
SortBoolean按时间排序 asc(升序)或 desc(降序),默认 desc
UseLuceneBoolean是否使用 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();