Skip to content

The debug viewer

nvs debug serves the debug viewer, a page in your browser that shows what your server recorded.

Turn on [debug] store = true in nvs.toml. Then open the link that nvs debug prints. The link contains a token, a long random text. The viewer opens only in a browser that opened this link. The first page lists the newest requests. Open a request to see its timeline, its queries, its dumps and its log lines. An exception shows its stack. Other pages show the runs and their tests, the calls to other servers, the scheduled jobs and queues, and the server's own log lines.

A file:line in the viewer opens the file in your editor. Press ? to see every key.

Good to know: r sends a recorded request to your server again. The handler runs again, so a mail is sent again and a row is written again.

The examples below show three routes. One makes a dump. One runs the same query once for each order in a loop. One throws an error that the Errors page counts.

Examples

A route that dumps a value

<?nvs
// Shows a route that dumps a value, so you can read the value later in the debug viewer.
class Carts
{
#[Core\Route(path: '/cart', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function show(): string
{
array<int> $prices = ['coffee' => 450, 'cake' => 380];
int $total = 0;
foreach ($prices as int $price) {
$total += $price;
}
// `Core\Debug::dump` writes the array to standard error. With `[debug] store = true`,
// the dump is also saved with the request. The request's page in the viewer lists it.
Core\Debug::dump($prices);
return 'total: ' . $total;
}
}
// The server calls `show` for each `GET /cart` request. Here the program calls it once itself.
// This prints `total: 830`.
echo (new Carts())->show(), "\n";
Output
total: 830

Queries that repeat

<?nvs
// Shows a route that runs one query for each order, and how the debug viewer lists those queries.
var $db = Core\Db::open({ driver: Core\Db\Driver::Sqlite, path: ':memory:' });
$db->execute('create table customers (id integer primary key, name text not null)', []);
$db->execute('create table orders (id integer primary key, customer integer not null)', []);
$db->execute("insert into customers (id, name) values (1, 'Ada'), (2, 'Linus')", []);
$db->execute('insert into orders (id, customer) values (101, 1), (102, 2), (103, 1)', []);
class Orders
{
#[Core\Route(path: '/orders', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function list(Core\Db\Connection $db): void
{
foreach ($db->query('select id, customer from orders order by id', []) as Core\Db\Row $order) {
// This query runs once for each order. The request's page in the viewer lists
// every query with its time, so you see the same query three times.
foreach ($db->query('select name from customers where id = ?', [$order->int('customer') ?? 0]) as Core\Db\Row $customer) {
echo $order->int('id') ?? 0, ' ', $customer->string('name') ?? '', "\n";
}
}
}
}
// The server calls `list` for each `GET /orders` request. Here the program calls it once itself.
// This prints one line for each order. A join would give the same lines with one query.
(new Orders())->list($db);
$db->close();
Output
101 Ada
102 Linus
103 Ada

A handler that throws

<?nvs
// Shows a route that throws an error, which the debug viewer counts on its Errors page.
// proof: exit 1
class Invoices
{
#[Core\Route(path: '/invoices/{id}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function show(uint $id): string
{
array<string> $invoices = [7 => 'paid', 8 => 'open'];
?string $status = $invoices[$id] ?? null;
if ($status == null) {
// The viewer's Errors page has one row for `RuntimeError` at this line.
// Each time the line throws, the count in that row is one more.
throw new RuntimeError('no invoice ' . $id);
}
return 'invoice ' . $id . ': ' . $status;
}
}
// The server calls `show` for each request. Here the program calls it twice itself.
// The first call prints `invoice 7: paid`.
echo (new Invoices())->show(7), "\n";
// Invoice 9 does not exist, so this call throws. The error ends the program with exit status 1.
echo (new Invoices())->show(9), "\n";
Output
invoice 7: paid

Changelog

VersionChange
0.0.6The debug viewer has three new pages. Outbound lists the calls your code made to other servers, and Code shows which lines ran and which lines copied a value, over all requests. Schedule shows each [[schedule]] job with its next run, and each queue with its pending, failed and dead-letter jobs.
0.0.6The Debug page of the debug viewer lists traces beside dumps and profiles. Each profile and trace shows the file and line that started it, and By site groups every kind by its line. The number beside Debug counts every record since you opened the viewer.
0.0.6The debug viewer shows the server's own log lines, such as its start, a reload or a compile error, on the Logs page. The Server filter shows only these lines. You do not need nvs debug --tail for them.
0.0.6When a request does not pass one of the server's checks, the debug viewer shows the cause. An example is a body that is too large. The Refused filter of the request list and the search term refused:<cause> find these requests.
0.0.6A run's page in the debug viewer has a Tests tab with each test of nvs test and its result. Its Output tab shows what the run printed. It also lists the run's profiles, and has a Coverage tab.
0.0.6The debug viewer's timeline shows calls to LDAP, IMAP, file transfer servers and Redis, database transactions and schema applies. Each call shows the server it reached, and a failed call shows its error's class. Search finds a call with kind: and target:.
0.0.6A request that opened a WebSocket or an event stream shows the connection in the debug viewer. You see how long it was open, the frames each way and why it closed. In development, the first 32 frames each way are shown.
0.0.6The Errors page of the debug viewer has a Faults tab. It lists each request or run that a panic or a limit stopped, and each TLS connection that failed, with its cause and its message.
0.0.6A production server records no dumped value, only its file and line and the type and size of each value. In development, a recorded JSON or form field named password, secret, token or api_key is replaced with [redacted]. The Settings page of the debug viewer lists the newest 50 sign-ins.
0.0.6When a browser opens the http:// address of the debug viewer (nvs debug), the viewer returns a short text that says to open the https:// address. When a browser does not trust its certificate, the viewer writes a line that names [debug] tls_cert_file. At start, it warns about a certificate that has expired, expires within 14 days or is not for its address.
0.0.6The debug viewer has a Debug page that lists every Core\Debug::dump value and every profile, newest first. You can filter the list, and its By site tab groups the dumps by the line that made them. The Stream page, which lists every record, also shows the value of each dump.
0.0.6The debug viewer has a Mail page. It lists the messages a capture = true block kept. A message shows its HTML in a safe frame, its plain text, its headers, its attachments and which CSS rules stayed.
0.0.6A request's page in the debug viewer shows its handler, its memory peak and limit, its response size, and the stack of an exception. Each cache, HTTP, file and process event shows its result. Sent mail and jobs pushed to a queue are events too.
0.0.6The debug viewer has a Runs page. It lists scheduled jobs, queue jobs and runs of nvs run and nvs test, each with its outcome, memory, queries and dumps. A run's page has the tabs of a request's page, except Request, Response and Coverage.
0.0.6The debug viewer has a new design, with a dark and a light theme. The request list has filters, summary tiles and the time split of each request, and a request page has tabs. Two new pages show totals for each route and for each query.