This topic describes how to print and view logs in the PHP runtime environment.

Log printing

Function Compute provides a built-in logger module. You can use $GLOBALS['fcLogger'] to collect and store printed logs in the Logstore that you specify when you create a service.

Log levels

You can call the setLevel method to specify the level of logs that you want to print. The following table describes the log levels by severity from high to low.
Log level Level Operation Description
EMERGENCY 600 $logger->emergency Emergency logs
ALERT 550 $logger->alert Alert logs
CRITICAL 500 $logger->critical Critical warnings
ERROR 400 $logger->error Error messages
WARNING 300 $logger->warning Warnings
NOTICE 250 $logger->notice Notifications and general logs
INFO (default severity) 200 $logger->info Output details
DEBUG 100 $logger->debug Debugging logs

Use the built-in log module to print logs

If you use this method to print logs, each log contains information such as the time, request ID, and log level. The following sample code shows how to print logs:


function handler($event, $context) {
  $logger = $GLOBALS['fcLogger'];
  $logger->info('hello world');
  $logger->critical('world hello');
  return 'hello world';

The following log is printed after you run the preceding code:

FunctionCompute php7.2 runtime inited.
FC Invoke Start RequestId: a7236357-619a-4578-8e49-xxxxxxxxxxxx
2022-07-04 12:24:05 a7236357-619a-4578-8e49-xxxxxxxxxxxx [INFO] hello world
2022-07-04 12:24:05 a7236357-619a-4578-8e49-xxxxxxxxxxxx [CRITICAL] world hello
\nFC Invoke End RequestId: a7236357-619a-4578-8e49-xxxxxxxxxxxx

View the logs

After the function is executed, you can view the logs on the Logs tab of the function details page. For more information, see View function invocation logs.