All Products
Search
Document Center

Function Compute:Logging

Last Updated:Feb 26, 2024

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

Print logs

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 to print logs and collect the printed logs to the Simple Log Service Logstore that you specified when you created the service by using $GLOBALS['fcLogger']. If your function is created by using another method, you can use the corresponding logging method of 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 in descending order of severity.

Log level

Level

Interface

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)

200

$logger->info

Output details

DEBUG

100

$logger->debug

Debugging logs

Use the built-in logger 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 provides an example on 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 output two logs, and then the default log level is modified to output only one log.

After you run the preceding code, the following content is printed:

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 long printing 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

In the preceding example, the log printing function can also be replaced with other functions that output standard outputs and standard errors.

View logs

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