CLS Log
Querying and managing the CLS (Log Service) of the TCB environment; accessed via app.log.
Since v5.0.0, the direct invocation method via app.log is supported. For versions prior to v5.0.0, please use the commonService method.
checkLogServiceEnabled
1. API Description
API feature: Check whether the CLS log service in the current environment has been activated.
API declaration: app.log.checkLogServiceEnabled(): Promise<boolean>
2. Input Parameters
N/A
3. Return Results
true indicates that it has been activated, false indicates that it is not activated or in the process of activation.
4. Sample Code
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('Log service not activated, activating...')
await app.log.createLogService()
}
console.log('Log service status: activated')
}
test()
createLogService
1. API Description
API feature: Activate the CLS log service for the current environment.
API declaration: app.log.createLogService(): Promise<Object>
2. Input Parameters
N/A
3. Return Results
| Field | Type | Description |
|---|---|---|
| RequestId | String | Unique identifier of the request |
searchClsLog
1. API Description
API feature: Query CLS logs with specified conditions.
API declaration: app.log.searchClsLog(params): Promise<ISearchClsLogResponse>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| queryString | Yes | String | Query statement. For details, refer to here. Supports syntax such as module:database, module:rdb AND eventType:MysqlSlowQuery |
| StartTime | Required | String | Query start time, format: YYYY-MM-DD HH:mm:ss |
| EndTime | Required | String | Query end time, format: YYYY-MM-DD HH:mm:ss |
| Limit | Required | Number | Maximum number of logs returned per request, with a maximum value of 100 |
| Context | No | String | Pagination cursor from the Context field returned last time |
| Sort | No | String | Sort order: asc (ascending) or desc (descending), default desc |
| UseLucene | No | Boolean | whether to use Lucene syntax, default false |
| service | No | String | Service type: tcb (TCB, default) or tcbr (Cloud Run) |
module Filtering Supported in queryString
| module Value | Description | Supported eventType |
|---|---|---|
database | Cloud Database (Document-based) | MongoSlowQuery |
rdb | Cloud Database (SQL-based) | MysqlFreeze; MysqlRecover; MysqlSlowQuery |
model | Data Model | — |
workflow | Approval Workflow | — |
auth | User Permissions | — |
app | Low-code Application | AppProdPub; AppProdDel |
llm | Large Language Model | Filtered via logType:llm-tracelog |
3. Return Results
| Field | Type | Description |
|---|---|---|
| RequestId | String | Unique identifier of the request |
| Context | String | Pagination cursor for passing in the next query |
| ListOver | Boolean | whether all logs have been returned |
| Results | IClsLogObject[] | Log List |
IClsLogObject
| Field | Type | Description |
|---|---|---|
| TopicId | String | topic ID of the logs |
| TopicName | String | log topic name |
| Timestamp | String | Log time |
| Content | String | Log Content |
| FileName | String | Collection path |
| Source | String | Log Source Device |
4. Sample Code
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 Compatibility Method
The following method, invoked via commonService, is functionally equivalent to the app.log approach and is equally supported. It is applicable to scenarios requiring compatibility with versions prior to v5.0.0.
⚠️ Starting from version 3.0.0, commonService is used as a method in this API with request parameters
(service?: string, version?: string), which is an incompatible change.
1. API Description
API feature: Query CLS logs with specified conditions.
API declaration: manager.commonService().call({ Action: 'SearchClsLog', Param: {}}): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| EnvId | Yes | String | Environment ID |
| StartTime | Required | String | Query start time, format: YYYY-MM-DD HH:mm:ss |
| EndTime | Required | String | Query end time, format: YYYY-MM-DD HH:mm:ss |
| QueryString | Required | String | Query statement. For details, refer to here |
| Limit | Required | String | Number of log entries to return per request, up to 100 |
| Sort | No | Boolean | Sort by time: asc (ascending) or desc (descending), default desc |
| UseLucene | No | Boolean | whether to use Lucene syntax, default false |
3. Sample Code
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();