Write logs

Updated at:

Use the put_logs method to send data to Simple Log Service.

Prerequisites

Parameter description

def put_logs(self, request):

Request parameters

Parameter

Type

Required

Description

request

PutLogsRequest

Yes

The put_logs request parameter class.

PutLogsRequest parameters

Parameter

Type

Required

Description

project

String

Yes

The name of the project. The project in Simple Log Service is used to isolate the resources of different users and control access to specific resources. See Manage projects.

logstore

String

Yes

The name of the logstore. The logstore in Simple Log Service is used to collect, store, and query logs. See Manage Logstores.

topic

String

No

The topic of logs. The default value is an empty string. For more information, see Log topics.

source

String

No

The source of the log. The default value is the IP address of the host where the producer is located.

logitems

List

Yes

The log content to send.

hashKey

String

No

The hash value that determines which shard receives the data.

logtags

List

No

A list of key-value pair tags. For the structure, see LogTag.

[('tag_key_1', 'tag_value_1'), ('tag_key_2', 'tag_value_2')]

compress

bool

No

Whether to compress log data.

Response parameters

For response parameter details, see PutLogs.

Sample code

from aliyun.log import LogClient
from aliyun.log import PutLogsRequest
from aliyun.log import LogItem
import time

import os

# This example obtains the AccessKey ID and AccessKey secret from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')

# The endpoint for Simple Log Service.
endpoint = "cn-hangzhou.log.aliyuncs.com"

client = LogClient(endpoint, access_key_id, access_key_secret)

project = "slsproject-1"
logstore = "logstore-1"
topic = "nginx"
source = ""

# Log content
contents = [
    ("user_id", "12345"),
    ("message", "Error occurred"),
    ("level", "ERROR")
]

# Create a LogItem instance.
log_item = LogItem()
log_item.set_contents(contents)

# Create a list of LogItem objects.
logitems = [log_item]

# Specifies whether to compress logs.
compress = True

# The hash value that determines the shard to which the data is sent.
hashKey = "40000000000000000000000000000000"

# Log tags
logtags = [("tag_key_1", "tag_value_1"), ("tag_key_2", "tag_value_2")]


def main():
    # Construct a PutLogsRequest object.
    request = PutLogsRequest(project, logstore, topic, source, logitems, hashKey, compress, logtags)

    # Upload logs using the put_logs method.
    res = client.put_logs(request)
    res.log_print()


if __name__ == '__main__':
    main()

Sample response

PutLogsResponse:
headers: {'Server': 'AliyunSLS', 'Content-Length': '0', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Mon, 25 Nov 2024 03:27:12 GMT', 'x-log-time': '1732505232', 'x-log-requestid': '6743EE90137BEA564161****'}

Process finished with exit code 0