Skip to main content

CLS Log

Querying and managing the CLS (Log Service) of the CloudBase environment; accessed via app.log.

version tip

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​

FieldTypeDescription
RequestIdStringUnique 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​

FieldRequiredTypeDescription
queryStringYesStringQuery statement. For details, refer to here. Supports syntax such as module:database, module:rdb AND eventType:MysqlSlowQuery
StartTimeRequiredStringQuery start time, format: YYYY-MM-DD HH:mm:ss
EndTimeRequiredStringQuery end time, format: YYYY-MM-DD HH:mm:ss
LimitRequiredNumberMaximum number of logs returned per request, with a maximum value of 100
ContextNoStringPagination cursor from the Context field returned last time
SortNoStringSort order: asc (ascending) or desc (descending), default desc
UseLuceneNoBooleanwhether to use Lucene syntax, default false
serviceNoStringService type: tcb (CloudBase, default) or tcbr (Cloud Run)

module Filtering Supported in queryString

module ValueDescriptionSupported eventType
databaseCloud Database (Document-based)MongoSlowQuery
rdbCloud Database (SQL-based)MysqlFreeze; MysqlRecover; MysqlSlowQuery
modelData Model—
workflowApproval Workflow—
authUser Permissions—
appLow-code ApplicationAppProdPub; AppProdDel
llmLarge Language ModelFiltered via logType:llm-tracelog

3. Return Results​

FieldTypeDescription
RequestIdStringUnique identifier of the request
ContextStringPagination cursor for passing in the next query
ListOverBooleanwhether all logs have been returned
ResultsIClsLogObject[]Log List

IClsLogObject

FieldTypeDescription
TopicIdStringtopic ID of the logs
TopicNameStringlog topic name
TimestampStringLog time
ContentStringLog Content
FileNameStringCollection path
SourceStringLog 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​

FieldRequiredTypeDescription
EnvIdYesStringEnvironment ID
StartTimeRequiredStringQuery start time, format: YYYY-MM-DD HH:mm:ss
EndTimeRequiredStringQuery end time, format: YYYY-MM-DD HH:mm:ss
QueryStringRequiredStringQuery statement. For details, refer to here
LimitRequiredStringNumber of log entries to return per request, up to 100
SortNoBooleanSort by time: asc (ascending) or desc (descending), default desc
UseLuceneNoBooleanwhether 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();