Core\Log::Level
How loud a log record is. Every record your program writes carries one of five levels, and whoever runs the server decides how much of that they want to keep.
Core\Log::Level is declared in the class Core\Log. The method write takes it as its first argument.
The five, quietest first, are Debug, Info, Warn, Error and Critical. Debug is detail for whoever is watching this one run. Info is something the program did that an operator would want in the record. Warn is something recoverable that nobody chose — a retry, a fallback. Error means an operation failed. Critical is for a program that can no longer do its job at all.
The level a deployment keeps is a floor rather than a filter: set [log] level to Warn and everything louder than Warn is kept too. When nothing sets the level, production keeps Info and louder, so a Debug record is not written. Development keeps every record.
Good to know: the level decides how loud a record is, never who reads it. Where records go is a separate setting, and the same record goes to the same place whatever its level.
Examples
The five levels and what each is for
<?nvs// One record at each of the five levels, quietest first. In production, a// `Debug` record is not kept by default. This program sets the level to// `Debug` for this run, so all five are written to standard error.// Standard error is where records go when no `[log] target` is set.Core\Config::set('log.level', 'Debug');
Core\Log::write(Core\Log::Level::Debug, 'the cart held 3 items');Core\Log::write(Core\Log::Level::Info, 'order ORD-1042 was placed');Core\Log::write(Core\Log::Level::Warn, 'the address service was slow, used the address on file');Core\Log::write(Core\Log::Level::Error, 'the payment could not be taken');Core\Log::write(Core\Log::Level::Critical, 'the payments service cannot be reached at all');// proof: with-stderr{"level":"debug","msg":"the cart held 3 items","source":{"file":"docs/examples/types/Log-Level/01-the-five-levels-and-what-each-is-for.nvs","line":8}}{"level":"info","msg":"order ORD-1042 was placed","source":{"file":"docs/examples/types/Log-Level/01-the-five-levels-and-what-each-is-for.nvs","line":9}}{"level":"warn","msg":"the address service was slow, used the address on file","source":{"file":"docs/examples/types/Log-Level/01-the-five-levels-and-what-each-is-for.nvs","line":10}}{"level":"error","msg":"the payment could not be taken","source":{"file":"docs/examples/types/Log-Level/01-the-five-levels-and-what-each-is-for.nvs","line":11}}{"level":"critical","msg":"the payments service cannot be reached at all","source":{"file":"docs/examples/types/Log-Level/01-the-five-levels-and-what-each-is-for.nvs","line":12}}Picking the level for what happened
<?nvs// Which level something deserves is a decision, and it is worth making once.// Keeping it in a single place means two parts of your program are never loud// about the same thing in two different ways. The records go to standard error.
class Report{ public static function levelFor(string $outcome): Core\Log::Level { return match ($outcome) { 'placed' => Core\Log::Level::Info, 'retried' => Core\Log::Level::Warn, 'declined' => Core\Log::Level::Error, default => Core\Log::Level::Critical, }; }}
array<string> $events = [ 'placed' => 'order ORD-1042 was placed', 'retried' => 'the card was tried a second time', 'declined' => 'the card was declined', 'unreachable' => 'the payments service cannot be reached at all',];
foreach ($events as string $outcome => string $message) { Core\Log::write(Report::levelFor($outcome), $message);}// proof: with-stderr{"level":"info","msg":"order ORD-1042 was placed","source":{"file":"docs/examples/types/Log-Level/02-picking-the-level-for-what-happened.nvs","line":27}}{"level":"warn","msg":"the card was tried a second time","source":{"file":"docs/examples/types/Log-Level/02-picking-the-level-for-what-happened.nvs","line":27}}{"level":"error","msg":"the card was declined","source":{"file":"docs/examples/types/Log-Level/02-picking-the-level-for-what-happened.nvs","line":27}}{"level":"critical","msg":"the payments service cannot be reached at all","source":{"file":"docs/examples/types/Log-Level/02-picking-the-level-for-what-happened.nvs","line":27}}A payment somebody has to look into
<?nvs// The everyday job: one card, two tries, and a record the support desk can// find again tomorrow. The order number goes in the fields beside the message// instead of inside it, so a search for that order finds every line about it.// The records go to standard error.
string $order = 'ORD-1042';array<bool> $tries = [false, false];
int $number = 1;foreach ($tries as bool $wentThrough) { array<mixed> $where = ['order' => $order, 'attempt' => $number]; if ($wentThrough) { Core\Log::write(Core\Log::Level::Info, 'the payment went through', $where); } else { // A try that failed and will be made again is a `Warn`. Nobody chose // it, and nothing is lost yet. Core\Log::write(Core\Log::Level::Warn, 'the card was declined, trying again', $where); } $number = $number + 1;}
// Nothing is going to be tried again now, so this one goes up a level. An// `Error` is what somebody is expected to look into.array<mixed> $result = ['order' => $order, 'attempts' => 2];Core\Log::write(Core\Log::Level::Error, 'the order was not taken', $result);// proof: with-stderr{"level":"warn","msg":"the card was declined, trying again","source":{"file":"docs/examples/types/Log-Level/03-a-payment-somebody-has-to-look-into.nvs","line":18},"fields":{"order":"ORD-1042","attempt":1}}{"level":"warn","msg":"the card was declined, trying again","source":{"file":"docs/examples/types/Log-Level/03-a-payment-somebody-has-to-look-into.nvs","line":18},"fields":{"order":"ORD-1042","attempt":2}}{"level":"error","msg":"the order was not taken","source":{"file":"docs/examples/types/Log-Level/03-a-payment-somebody-has-to-look-into.nvs","line":26},"fields":{"order":"ORD-1042","attempts":2}}