This topic describes how to print and view logs in a Python runtime.
Print logs
The log content that is printed by a function to the standard output (stdout) is stored in the specified Logstore when you create the service. You can use one of the following methods to print logs.
Use the logging module to print logs
If you use the logging module to print logs, each log contains information such as the time, request ID, and log level. The request ID helps you locate logs when errors occur. Sample code:
import logging
def handler(event, context):
logger = logging.getLogger()
logger.info('hello world')
return 'done'
The following log is printed after you run the preceding code:
2017-07-05T05:13:35.920Z a72df088-f738-cee3-e0fe-323ad****e5 [INFO] hello world
Use the print command to print logs
If you use the print command to print logs, the content of the log is printed. Sample code:
def handler(event, context):
print ('hello world')
return 'done'
The following log is printed after you run the preceding code:
hello world
Use context.getLogger to print logs
If the instance concurrency of the function is greater than 1, one function instance can concurrently process multiple requests. In this case, we recommend that you use context.getLogger to print logs. This way, the logs of each concurrent request can be distinguished by using the request IDs. Sample 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 world
View logs
After a function is executed, you can view logs on the Logs tab of the function details page. For more information, see View function invocation logs.