You can use the following statements in the program to output a log:
print
logging module
For instance, by executing the following code, you can query the output content in the function log.
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def main_handler(event, context):
logger.info('got event{}'.format(event))
print("got event{}".format(event))
return'Hello World!'
Log Query
All current function logs will be shipped to Tencent Cloud Log Service (CLS). You can configure the log shipping for function logs. For more details, see Log Shipping Configuration.
You can query the function execution logs through the log query interface of the cloud function or the query interface of the log service. For detailed log query methods, see Log Retrieval Tutorial.
Description
Function logs are delivered to the LogSet log set and LogTopic log topic in CLS, both of which can be queried through the function configuration.
Custom Log Fields
The output of a simple print or logger method in the function codes will be recorded in the SCF_Message field when being uploaded to CLS. For more information about CLS fields, see Configuring index.
Currently, SCF supports adding custom fields to logs that are uploaded to CLS. The custom fields allow you to output business fields and relevant data to logs, and track them using the log search feature of CLS.
Note
If you need to query the key value of a custom field such as SCF_CustomKey: SCF, add a key-value index to the log topic for SCF log delivery as instructed in Configuring Indexes.
To avoid function log query failures caused by misuse of the index configuration, the default destination topic for SCF log delivery (prefixed with SCF_LogTopic_) does not support modifying the index configuration. You need to set the destination topic to custom delivery first and then update the log topic's index configuration.
After the index configuration is modified for a log topic, it will take effect only for newly written data.
Output
If a function outputs a single-line log in JSON, the log will be parsed and uploaded to CLS in the field:value format. Only the first layer will be parsed, and the remaining nested structure will be recorded as values.
You can run the following code to test:
# -*- coding: utf8 -*-
import json
defmain_handler(event, context):
print(json.dumps({"key1":"test value 1","key2":"test value 2"}))
return("Hello World!")
Search
After running the test with the above code, you can search in Function-Log Query-Advanced Search using the following statement:
Search Results
After testing the log service, you can find the key1 field in the log query, as shown in the image below: