Core\Test\LogRecord::level
$logRecord->level(): stringlevel() returns the level of a log record, in lower case. In a test, Core\Test::logs() returns one Core\Test\LogRecord for each record, and level() is one of its methods.
The result is one of five strings: "debug", "info", "warn", "error" or "critical". It is the same text as the level in a JSON log line. It matches the Core\Log::Level case that your program gave to Core\Log::write.
Use level() to check that a problem is logged at the right level. A failed payment should be an error, not an info.
Good to know: a record below the setting [log] level is not kept. The default is info, so a test never sees a debug record unless the setting is lower.
The examples below show a failed payment, a disk that fills up step by step, and a request with a wrong API key.
Return value
Returns string. "debug", "info", "warn", "error" or "critical". This is the level a JSON log line has.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\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 failed payment is an error
<?nvs// Shows how to test that a failed payment is logged as an error.// proof: test
use Core\Log;use Core\Test;
class Payment{ // Charges the card. A declined card writes an `error` record and returns `false`. public static function charge(string $card, int $cents): bool { if ($card == '4000-0000-0000-0002') { Log::write(Log::Level::Error, 'payment declined', ['cents' => $cents]); return false; } return true; }}
class PaymentTest{ #[Test] public function aDeclinedCardIsLoggedAsAnError(): void { Test::assertSame(Payment::charge('4000-0000-0000-0002', 2500), false);
// `level()` returns the level in lower case, so the test compares it with `'error'`. Test::assertCount(Test::logs(), 1); Test::assertSame(Test::logs()[0]->level(), 'error'); }} PaymentTest ✓ aDeclinedCardIsLoggedAsAnError
0 failed, 1 passed, 0 skipped, 0 flakyA disk that fills up
<?nvs// Shows how to test that the level of a record grows with the size of a problem.// proof: test
use Core\Log;use Core\Test;use Core\Test\LogRecord;
class DiskCheck{ // Writes a `warn` record above 80 percent and a `critical` record above 95 percent. public static function check(int $percentUsed): void { if ($percentUsed > 95) { Log::write(Log::Level::Critical, 'disk almost full', ['used' => $percentUsed]); } elseif ($percentUsed > 80) { Log::write(Log::Level::Warn, 'disk filling up', ['used' => $percentUsed]); } else { Log::write(Log::Level::Info, 'disk checked', ['used' => $percentUsed]); } }}
class DiskCheckTest{ #[Test] public function eachCheckHasTheRightLevel(): void { DiskCheck::check(50); DiskCheck::check(85); DiskCheck::check(99);
// The three records are in the order they were written. array<string> $levels = []; foreach (Test::logs() as LogRecord $record) { $levels[] = $record->level(); } Test::assertEquals($levels, ['info', 'warn', 'critical']); }} DiskCheckTest ✓ eachCheckHasTheRightLevel
0 failed, 1 passed, 0 skipped, 0 flakyA wrong api key is a warning
<?nvs// Shows how to test the level of a record that a request wrote.// proof: test
use Core\Http\Method;use Core\Test;
class ApiKeyTest{ // A wrong key is a warning. The test checks the level and the status of the response. #[Test] public function aWrongKeyIsLoggedAsAWarning(): void { var $response = Test::request(Method::Get, '/reports', { headers: ['x-api-key' => 'not-a-real-key'], });
Test::assertSame($response->status(), 401); Test::assertCount(Test::logs(), 1); Test::assertSame(Test::logs()[0]->level(), 'warn'); }}
// `Core\Test::request` runs this file again for the request.// This part checks the key and writes a `warn` record when the key is wrong.if (Core\Env::context() == Core\Env::Context::Request) { if (Core\Request::header('x-api-key') != 'key-for-reports') { Core\Log::write(Core\Log::Level::Warn, 'wrong API key'); Core\Response::setStatus(401); } else { echo 'report'; }} ApiKeyTest ✓ aWrongKeyIsLoggedAsAWarning
0 failed, 1 passed, 0 skipped, 0 flaky