Log Development
You can use the following statements in the program to output a log:
print
logging module
For example, you can query the output content in the function log by running the following code:
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
Currently, all function logs are delivered to CLS. You can configure the function log delivery. For more information, see Log Delivery Configuration.
You can query function execution logs on the log query page of SCF or CLS. For more information on the log query method, see Log Search Guide. Note:
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
Currently, the string content output by simple print
or logger
in the function code will be recorded in the SCF_Message
field when it is delivered to CLS. For descriptions of CLS fields, see Log Delivery Configuration. At present, SCF supports adding custom fields to the content output to CLS. By doing so, you can output business fields and related data content to logs and use the search capability of CLS to query and track them in the execution process.
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 method
If a single-line log output by a function is in JSON format, the JSON content will be parsed into the format of field:value
when it is delivered to CLS. Only the first level of the JSON content can be parsed in this way, while other nested structures will be recorded as values.
You can run the following code to test:
import json
def main_handler(event, context):
print(json.dumps({"key1": "test value 1","key2": "test value 2"}))
return("Hello World!")
Search method
After using the above code to perform a test, you can run the following statement to search in Function Management > Log Query > Advanced Search:
Search result
After the test is written to CLS, you can find the key1
field in the log query as shown below:
Was this page helpful?