This topic describes how to print and view logs in a Python runtime environment.
Log Printing
Log content that functions print to standard output (stdout) is collected in the Logstore specified when you create the service. You can print logs as follows.
Print Logs Using the Logging Module
Each log printed using this method includes information such as time, RequestId, and log level. RequestId helps locate problematic logs when errors occur. The following code provides an example.
import logging
def handler(event, context):
logger = logging.getLogger()
logger.info('hello world')
return 'done'Execute the preceding code. The output log content is as follows.
2017-07-05T05:13:35.920Z a72df088-f738-cee3-e0fe-323ad****e5 [INFO] hello worldPrint Logs Using Print
Using this method to print logs outputs the content as-is to the log. The following code provides an example.
def handler(event, context):
print ('hello world')
return 'done'The following log is printed after you run the preceding code:
hello worldUse context.getLogger to print logs
You can use context.getLogger to print logs to distinguish the logs of concurrent requests by RequestId. Example code:
def handler(event, context):
context.getLogger().info("hello world")
return 'done'The following log is printed after you run the preceding code:
2022-07-13 10:26:02 6785e433-497e-4c4a-a81a-2d4096d1**** [INFO] hello worldView Logs
After the function executes, you can view log information on the Invocation Logs tab of the function product page. For more information, see the referenced document.