Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ PHP NEWS
target, such as [$a, $b] = [$b, $a], no longer builds the array when the
result is unused (GH-23048). (Artem Ukrainskiy)

- CLI:
. Added --loglevel for the built-in development server to select process,
error, or request logs (GH-16855). (mvanhorn)

- Curl:
. curl_multi_exec() now throws a TypeError when still_running is not an int.
(arshidkv12)
Expand Down
5 changes: 5 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ PHP 8.7 UPGRADE NOTES
3. Changes in SAPI modules
========================================

- CLI:
. The built-in development server accepts --loglevel <n>, where 1 logs
process messages only, 2 also logs errors, and 3 (the default) also logs
each request.

========================================
4. Deprecated Functionality
========================================
Expand Down
8 changes: 7 additions & 1 deletion sapi/cli/php_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const opt_struct OPTIONS[] = {
/* Internal testing option -- may be changed or removed without notice,
* including in patch releases. */
{16, 1, "repeat"},
{17, 1, "loglevel"},
{'-', 0, NULL} /* end of args */
};

Expand Down Expand Up @@ -459,7 +460,7 @@ static void php_cli_usage(char *argv0)
" %s [options] -r <code> [--] [args...]\n"
" %s [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]\n"
" %s [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]\n"
" %s [options] -S <addr>:<port> [-t docroot] [router]\n"
" %s [options] -S <addr>:<port> [-t docroot] [--loglevel <n>] [router]\n"
" %s [options] -- [args...]\n"
" %s [options] -a\n"
"\n"
Expand Down Expand Up @@ -490,6 +491,8 @@ static void php_cli_usage(char *argv0)
"\n"
" --ini Show configuration file names\n"
" --ini=diff Show INI entries that differ from the built-in default\n"
" --loglevel <n> Built-in server log level: 1=process, 2=error,\n"
" 3=message (default).\n"
"\n"
" --rf <name> Show information about function <name>.\n"
" --rc <name> Show information about class <name>.\n"
Expand Down Expand Up @@ -825,6 +828,9 @@ static int do_cli(int argc, char **argv) /* {{{ */
case 16:
num_repeats = atoi(php_optarg);
break;
case 17:
param_error = "--loglevel is only valid with the built-in server (-S).\n";
break;
default:
break;
}
Expand Down
13 changes: 13 additions & 0 deletions sapi/cli/php_cli_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ static void sapi_cli_server_log_write(int type, const char *msg) /* {{{ */
#else
fprintf(stderr, "[%s] %s\n", buf, msg);
#endif
fflush(stderr);
} /* }}} */

static void sapi_cli_server_log_message(const char *msg, int syslog_type_int) /* {{{ */
Expand Down Expand Up @@ -2908,6 +2909,18 @@ int do_cli_server(int argc, char **argv) /* {{{ */
php_cli_server_log_level--;
}
break;
case 17: {
char *end = NULL;
long level = strtol(php_optarg, &end, 10);
if (end == php_optarg || *end != '\0' || level < 1 || level > 3) {
fprintf(stderr,
"Invalid --loglevel \"%s\". Use 1 (process), 2 (error), or 3 (message).\n",
php_optarg);
return 1;
}
php_cli_server_log_level = (int) level;
break;
}
}
}

Expand Down
45 changes: 45 additions & 0 deletions sapi/cli/tests/gh16855.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
GH-16855: --loglevel CLI option for the built-in server
--SKIPIF--
<?php
include "skipif.inc";
?>
--FILE--
<?php
include "php_cli_server.inc";

$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');

echo "=== help ===\n";
$help = shell_exec("$php -n -h");
echo str_contains($help, '--loglevel') ? "help lists --loglevel\n" : "missing --loglevel\n";

echo "=== invalid value ===\n";
passthru("$php -n -S localhost:0 --loglevel 9 2>&1", $status);
echo "exit=$status\n";

echo "=== without -S ===\n";
passthru("$php -n --loglevel 1 -r 'echo 1;' 2>&1", $status);
echo "exit=$status\n";

echo "=== loglevel 1 omits request logs ===\n";
$quiet = php_cli_server_start('echo "ok";', 'index.php', ['--loglevel', '1']);
file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS);
$out = file_get_contents($quiet->outputFile);
echo str_contains($out, 'Development Server') ? "startup logged\n" : "startup missing\n";
echo (str_contains($out, 'Accepted') || str_contains($out, ' GET ') || str_contains($out, 'Closing'))
? "request logs present\n"
: "request logs omitted\n";
?>
--EXPECT--
=== help ===
help lists --loglevel
=== invalid value ===
Invalid --loglevel "9". Use 1 (process), 2 (error), or 3 (message).
exit=1
=== without -S ===
--loglevel is only valid with the built-in server (-S).
exit=1
=== loglevel 1 omits request logs ===
startup logged
request logs omitted
18 changes: 18 additions & 0 deletions sapi/cli/tests/gh16855_default.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
GH-16855: built-in server default log level records requests
--SKIPIF--
<?php
include "skipif.inc";
?>
--FILE--
<?php
include "php_cli_server.inc";
$info = php_cli_server_start('echo "ok";');
file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS);
$out = file_get_contents($info->outputFile);
echo (str_contains($out, 'Accepted') || str_contains($out, ' GET '))
? "request logs present\n"
: "request logs missing\n";
?>
--EXPECT--
request logs present
Loading