All Products
Search
Document Center

Function Compute:Logging

Last Updated:Feb 20, 2024

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

Log printing

Function Compute provides a built-in logger module. If your function is created by using a built-in runtime, you can use the built-in logger module by using $GLOBALS['fcLogger'] to print logs and collect the printed logs to the Simple Log Service Logstore that you specified when you created the service. If your function is created by using another method, you can use the method provided by PHP to print logs.

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:

<?php

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

  $logger->setLevel(500);
  $logger->info('hello world 2');
  $logger->critical('world hello 2');
  return 'hello world';
}

In the preceding example, the default log level is used to print two logs, and then the default log level is modified to output one log.

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

FunctionCompute php7.2 runtime inited.
FC Invoke Start RequestId: 1-659xxxxx-16cxxxxx-39659xxxxxx
2024-01-04 10:50:44 1-659xxxxx-16cxxxxx-39659xxxxxx [INFO] hello world
2024-01-04 10:50:44 1-659xxxxx-16cxxxxx-39659xxxxxx [CRITICAL] world hello
2024-01-04 10:50:44 1-659xxxxx-16cxxxxx-39659xxxxxx [CRITICAL] world hello 2
\nFC Invoke End RequestId: 1-659xxxxx-16cxxxxx-39659xxxxxx

Use the log printing function to print logs

If you use the print command to print logs, the content of the log is printed. Sample code:

<?php

function handler($event, $context) {
  var_dump("abcd");
  echo "abcd 2\n";
  fwrite(STDERR, "error\n");
  return 'hello world';
}

Sample output:

FunctionCompute php7.2 runtime inited.
FC Invoke Start RequestId: 1-659xxxxx-165xxxxx-455a04xxxxxx
/code/index.php:4:
string(4) "abcd"
abcd 2
error
\nFC Invoke End RequestId: 1-659xxxxx-165xxxxx-455a04xxxxxx

You can replace the log printing function in the preceding example with other functions that output standard output and standard error messages.

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.