Skip to content

Script Development / Connector Object DFF.CONN / Alibaba Cloud Log Service SLS

The Alibaba Cloud Log Service SLS Connector operation object is used to list Projects and Logstores and query logs.

The parameters of DFF.CONN(...) are as follows:

Parameter Type Required / Default Description
connector_id str Required Connector ID

AccessKey credentials come from the Connector configuration. They need not and should not be passed in Script code.

.list_projects()

Returns the names of Projects that the current credentials are authorized to access, in the format list[str].

Example
1
2
sls = DFF.CONN('aliyun_sls_connector_id')
projects = sls.list_projects()

This method uses the SDK's default pagination parameters. It returns at most 100 Projects per call and does not support passing pagination parameters.

.list_logstores(...)

Lists Logstores under the specified Project. Parameters are as follows:

Parameter Type Required / Default Description
project str Required Project name

The return value format is list[str].

Example
1
logstores = sls.list_logstores('example-project')

This method uses the SDK's default pagination parameters. It returns at most 100 Logstores per call and does not support passing pagination parameters.

.query(...)

Queries logs in the specified Project and Logstore. Parameters are passed directly to the log query interface of aliyun-log-python-sdk:

Parameter Type Required / Default Description
project str Required Project name
logstore str Required Logstore name
from_time int/str Required Query start time; accepts a Unix timestamp in seconds or a readable time string supported by SLS
to_time int/str Required Query end time; accepts a Unix timestamp in seconds or a readable time string supported by SLS
topic str None Limit the log topic
query str None SLS query statement
reverse bool False Whether to return newer logs first
offset int 0 Starting offset of returned results
size int 100 Maximum number of entries to return; -1 means to request all results
power_sql bool False Whether to use enhanced SQL mode
scan bool False Whether to use Scan query mode
forward bool True Pagination direction for Scan queries; True means next page, False means previous page
accurate_query bool True Whether to use accurate query mode
from_time_nano_part int 0 Nanosecond part of the query start time
to_time_nano_part int 0 Nanosecond part of the query end time
Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
sls = DFF.CONN('aliyun_sls_connector_id')

query_args = {
    'project'  : 'example-project',
    'logstore' : 'application',
    'from_time': _DFF_TRIGGER_TIME - 3600,
    'to_time'  : _DFF_TRIGGER_TIME,
    'query'    : 'error',
    'size'     : 100,
}
logs = sls.query(**query_args)

The return value format is list[dict]. Each log entry contains the following fields:

Field Type Description
contents dict Log contents
timestamp int Log time, Unix timestamp in seconds

When an SDK query fails, its exception is propagated directly upward.

Limit Query Range and Return Count

Large-scope log queries may incur high costs. Cron Jobs should use _DFF_TRIGGER_TIME to construct a stable query window and limit the time range and size based on actual needs.