Skip to content

Core\Test\LogRecord::fields

$logRecord->fields(): array<mixed>

fields() returns the extra fields of a log record, as an array. In a test, Core\Test::logs() returns one Core\Test\LogRecord for each record, and fields() is one of its methods.

The array contains the $fields that your program gave to Core\Log::write, in the same order. A string, a number, a bool and null keep their value. The array is empty when the record has no fields.

Good to know: the fields are returned the way the log wrote them. JSON has no type for some values, such as bytes. Such a value is the same as in a JSON log line. For bytes, that is an array with one key, $bytes, and the bytes as hex digits.

The examples below show a slow database query with its duration, the value of each type of field, and an upload request that logs the size of the file.

Return value

Returns array<mixed>. The $fields given to Core\Log::write, in the order given. A string, a number, a bool and null keep their value. JSON has no type for some values, such as bytes. Such a value is the same as in a JSON log line. For bytes, that is an array with one key, $bytes, and the bytes as hex digits. A very long or deeply nested value is cut down the same way the log line cuts it. The array is empty when the record has no fields.

Changelog

VersionChange
0.0.5Core\Test::logs returns the log records written in the current test. The list includes the records of each request and each run the test started. Each record has level(), message() and fields().

Examples

A slow query logs its duration

<?nvs
// Shows how to test the fields of a record about a slow database query.
// proof: test
use Core\Log;
use Core\Test;
class QueryTimer
{
// Writes a `warn` record when a query takes longer than 200 milliseconds.
public static function finished(string $table, int $milliseconds): void
{
if ($milliseconds > 200) {
Log::write(Log::Level::Warn, 'slow query', ['table' => $table, 'ms' => $milliseconds]);
}
}
}
class QueryTimerTest
{
#[Test]
public function aSlowQueryIsLoggedWithItsDuration(): void
{
QueryTimer::finished('orders', 40);
QueryTimer::finished('orders', 850);
// Only the slow query wrote a record. Its fields are in the order they were given.
Test::assertCount(Test::logs(), 1);
Test::assertEquals(Test::logs()[0]->fields(), ['table' => 'orders', 'ms' => 850]);
}
}
Output
QueryTimerTest
✓ aSlowQueryIsLoggedWithItsDuration
0 failed, 1 passed, 0 skipped, 0 flaky

The value of each type of field

<?nvs
// Shows the value that `fields()` returns for each type of field.
// proof: test
use Core\Log;
use Core\Test;
class FieldTypesTest
{
#[Test]
public function eachFieldKeepsItsValue(): void
{
Log::write(Log::Level::Info, 'file received', [
'name' => 'photo.png',
'size' => 2048,
'public' => false,
'album' => null,
'start' => 'PNG' as bytes,
]);
// A string, a number, a `bool` and `null` keep their value.
array<mixed> $fields = Test::logs()[0]->fields();
Test::assertSame($fields['name'], 'photo.png');
Test::assertSame($fields['size'], 2048);
Test::assertSame($fields['public'], false);
Test::assertNull($fields['album']);
// JSON has no type for `bytes`. The log line writes the bytes as hex digits under
// the key `$bytes`, and `fields()` returns the same thing.
Test::assertEquals($fields['start'], ['$bytes' => '504e47']);
}
}
Output
FieldTypesTest
✓ eachFieldKeepsItsValue
0 failed, 1 passed, 0 skipped, 0 flaky

An upload logs its size

<?nvs
// Shows how to test the fields of a record that an upload request wrote.
// proof: test
use Core\Http\Method;
use Core\Test;
class UploadTest
{
// The body has 11 bytes, so the `bytes` field of the record is `11`.
#[Test]
public function anUploadIsLoggedWithItsSize(): void
{
Test::request(Method::Put, '/files/notes.txt', {
headers: ['content-type' => 'text/plain'],
body: 'hello world',
});
Test::assertCount(Test::logs(), 1);
array<mixed> $fields = Test::logs()[0]->fields();
Test::assertSame($fields['file'], 'notes.txt');
Test::assertSame($fields['bytes'], 11);
}
}
// `Core\Test::request` runs this file again for the request.
// This part stores the file and writes its name and size to the log.
if (Core\Env::context() == Core\Env::Context::Request) {
tainted bytes $body = Core\Request::bytes();
Core\Log::write(Core\Log::Level::Info, 'file uploaded', [
'file' => 'notes.txt',
'bytes' => Core\Bytes::length($body),
]);
Core\Response::setStatus(201);
}
Output
UploadTest
✓ anUploadIsLoggedWithItsSize
0 failed, 1 passed, 0 skipped, 0 flaky