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
You can use context.getLogger to print logs to distinguish the logs of concurrent requests by RequestId. 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 the function is executed, you can view logs on the Logs tab of the function details page. For more information, see View invocation logs.