Skip to content

Core\Log::write

Writes one log record with a level, a message, and the file and line that wrote it. Inside a web request, the record also has the time and the request id. fields adds extra values as their own object, so a search for an order number finds every record about that order.

[log] target sets where records go. With no target, they go to standard error. A server that runs as a service writes them to logs/nvs.log in its data folder. A record is never part of the program's output or of a web response. [log] format is "json" in production and "text" in development.

In JSON, a line break in the message is written as \n, so a record is always one line. A secret value as the message does not compile.

The examples below write records with and without fields, show each kind of field value, and log the rows an import cannot read.

Parameters

  • $levelCore\Log::LevelHow serious the record is. Each case of Core\Log::Level has the number of its syslog severity.
  • $messagestringWhat happened, as one plain sentence. A tainted value is allowed here, because a log often records input from outside. A secret value does not compile, as in every message a person reads.
  • $fieldsarray<mixed>default []Extra data about the event. It is written as a fields object next to the message. When it is empty, the record has no fields key. Nothing in fields makes a write fail. A value the log format cannot contain, such as bytes, a callable or a cycle, is written as text that describes it.

Return value

Returns void. Nothing. If a record cannot be written, it is lost and not written again. The log is not a place to store the program's data.

Examples

A record with and without fields

<?nvs
// Shows how to write a log record, first with a message only and then with fields.
// With no `[log] target` set, every record goes to standard error.
// This writes one line of JSON. It has the level, the message and the place in this file.
Core\Log::write(Core\Log::Level::Info, 'the server started');
// `fields` adds extra values next to the message.
// This line also has `"fields":{"user":42,"plan":"pro"}`.
Core\Log::write(Core\Log::Level::Info, 'a user signed in', ['user' => 42, 'plan' => 'pro']);
// proof: with-stderr
Output
{"level":"info","msg":"the server started","source":{"file":"docs/examples/core/Log/write/01-a-record-with-and-without-fields.nvs","line":6}}
{"level":"info","msg":"a user signed in","source":{"file":"docs/examples/core/Log/write/01-a-record-with-and-without-fields.nvs","line":10},"fields":{"user":42,"plan":"pro"}}

What a field can contain

<?nvs
// Shows the kinds of values a log field can contain, and how each one is written.
// With no `[log] target` set, every record goes to standard error.
// Numbers, `true`, `false` and `null` are written as JSON values.
// A nested array is written as a nested JSON object.
array<mixed> $order = [
'id' => 1042,
'total' => 19.99,
'paid' => true,
'coupon' => null,
'customer' => ['id' => 7, 'country' => 'AT'],
];
Core\Log::write(Core\Log::Level::Info, 'order placed', $order);
// A list has no keys of its own. The positions `0`, `1` and `2` are used as the keys.
array<mixed> $tags = ['new', 'gift', 'express'];
Core\Log::write(Core\Log::Level::Info, 'order tags', $tags);
// A line break in the message is written as `\n`. The record is still one line.
Core\Log::write(Core\Log::Level::Warn, "the address has two lines:\nMain Street 1\nVienna");
// proof: with-stderr
Output
{"level":"info","msg":"order placed","source":{"file":"docs/examples/core/Log/write/02-what-a-field-can-contain.nvs","line":14},"fields":{"id":1042,"total":19.99,"paid":true,"coupon":null,"customer":{"id":7,"country":"AT"}}}
{"level":"info","msg":"order tags","source":{"file":"docs/examples/core/Log/write/02-what-a-field-can-contain.nvs","line":18},"fields":{"0":"new","1":"gift","2":"express"}}
{"level":"warn","msg":"the address has two lines:\nMain Street 1\nVienna","source":{"file":"docs/examples/core/Log/write/02-what-a-field-can-contain.nvs","line":21}}

Rows an import cannot read

<?nvs
// Shows how an import job logs each row it cannot read, and one summary at the end.
// With no `[log] target` set, every record goes to standard error.
// Each row is an order number and an amount. Two of these rows are broken.
array<string> $rows = ['1042;19.99', '1043;abc', '1044;5.00', ';7.50'];
uint $saved = 0;
uint $skipped = 0;
uint $number = 1;
foreach ($rows as string $row) {
array<string> $parts = Core\Str::split($row, ';');
// `as ?float` converts the text to a number. For "abc" the result is `null`.
?float $amount = $parts[1] as ?float;
if ($parts[0] == '' || $amount == null) {
// The row number and the text go in the fields, so you can find the row in the file.
Core\Log::write(Core\Log::Level::Warn, 'a row was skipped', ['row' => $number, 'text' => $row]);
$skipped = $skipped + 1;
} else {
$saved = $saved + 1;
}
$number = $number + 1;
}
// One record says how the whole import went. It has `"saved":2,"skipped":2`.
Core\Log::write(Core\Log::Level::Info, 'the import finished', ['saved' => $saved, 'skipped' => $skipped]);
// proof: with-stderr
Output
{"level":"warn","msg":"a row was skipped","source":{"file":"docs/examples/core/Log/write/03-rows-an-import-cannot-read.nvs","line":17},"fields":{"row":2,"text":"1043;abc"}}
{"level":"warn","msg":"a row was skipped","source":{"file":"docs/examples/core/Log/write/03-rows-an-import-cannot-read.nvs","line":17},"fields":{"row":4,"text":";7.50"}}
{"level":"info","msg":"the import finished","source":{"file":"docs/examples/core/Log/write/03-rows-an-import-cannot-read.nvs","line":26},"fields":{"saved":2,"skipped":2}}