Function Compute's PHP runtime supports two ways to write logs from your function code: the built-in logger module and standard PHP output functions. This topic explains how to use each approach and how to view logs after a function runs.
Print logs
Use the built-in logger
For functions using a built-in runtime, access the logger through the $GLOBALS['fcLogger'] global variable. Logs are automatically forwarded to the Simple Log Service (SLS) Logstore you specified when creating the service.
Each log entry is structured as:
YYYY-MM-DD HH:MM:SS <requestId> [LEVEL] messageExample:
2024-01-04 10:50:44 1-659xxxxx-16cxxxxx-39659xxxxxx [INFO] hello worldThe built-in logger automatically adds a timestamp, request ID, and log level to every entry. Standard PHP output functions (covered in the next section) do not include this metadata.
The following example prints two logs at the default INFO level, then raises the level to CRITICAL (500) so that subsequent INFO logs are suppressed:
<?php
function handler($event, $context) {
$logger = $GLOBALS['fcLogger'];
$logger->info('hello world');
$logger->critical('world hello');
$logger->setLevel(500);
$logger->info('hello world 2'); // suppressed -- below CRITICAL threshold
$logger->critical('world hello 2');
return 'hello world';
}Expected output:
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-39659xxxxxxhello world 2 does not appear because after setLevel(500), the logger only emits messages at CRITICAL (500) or above.
Log levels
Call $logger->setLevel(<numeric_level>) to control which messages are emitted. Messages below the active level are dropped. The default level is INFO (200).
| Log level | Numeric value | Interface | When to use |
|---|---|---|---|
| EMERGENCY | 600 | $logger->emergency | The system is unusable and requires immediate action |
| ALERT | 550 | $logger->alert | A critical component is down and action must be taken immediately |
| CRITICAL | 500 | $logger->critical | A serious failure has occurred that requires urgent attention |
| ERROR | 400 | $logger->error | An operation failed and could not complete |
| WARNING | 300 | $logger->warning | An unexpected situation occurred, but execution continued |
| NOTICE | 250 | $logger->notice | A normal but significant event worth tracking |
| INFO | 200 | $logger->info | General operational messages; default level |
| DEBUG | 100 | $logger->debug | Detailed diagnostic information for development and troubleshooting |
Use standard PHP output functions
For functions not using a built-in runtime, use standard PHP output functions. Unlike the built-in logger, this approach prints raw content only — no timestamp, request ID, or log level is added.
<?php
function handler($event, $context) {
var_dump("abcd");
echo "abcd 2\n";
fwrite(STDERR, "error\n");
return 'hello world';
}Expected 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-455a04xxxxxxAny function that writes to standard output or standard error works the same way.
View logs
After a function runs, open the function in the Function Compute console and go to the Logs tab on the Function Details page. To filter and query logs across invocations, see View invocation logs.