Missing Columns on Custom Formatter #305
-
Hey there. I am trying to create a custom log formatter but I am hitting a wall that I cannot overcome and I need help. Regex link: https://regexr.com/7omeu This is my custom log: class LogViewerCustom extends Log
{
public static string $name = 'Custom';
public static string $regex = '/^\[(?P<datetime>[\d\-+ :]+)\] \[(?P<env>\w+)\.(?P<level>\w+)\] \[(?P<code>.{5})\] (?P<message>.+)\s(?P<payload>\{.*\})?$/';
/** @var array|string[][] */
public static array $columns = [
['label' => 'Level', 'data_path' => 'level'],
['label' => 'Date/Time', 'data_path' => 'datetime'],
['label' => 'Env', 'data_path' => 'env'],
['label' => 'Code', 'data_path' => 'code'],
['label' => 'Message', 'data_path' => 'message'],
['label' => 'Payload', 'data_path' => 'payload'],
];
} and this is the laravel.log:
This is the result: No data on Env nor Code. And the Payload column is missing. But the log row is read correctly: But if I do a dd on the fillMatches method protected function fillMatches(array $matches = []): void
{
parent::fillMatches($matches);
dd($matches);
} I get (on the Dev Tools response): array:11 [ // app\Logging\LogViewerCustom.php:27
0 => "[2023-12-07 04:24:02] [local.DEBUG] [DBG02] payload {"foo":"bar"}"
"datetime" => "2023-12-07 04:24:02"
1 => "2023-12-07 04:24:02"
"env" => "local"
2 => "local"
"level" => "DEBUG"
3 => "DEBUG"
"code" => "DBG02"
4 => "DBG02"
"message" => "payload {"foo":"bar"}"
5 => "payload {"foo":"bar"}"
] So shouldn't the data_path for env and code be there? What am I missing? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hey @jfvoliveira , glad you're experimenting with custom logs! So, I'm not sure if I missed it in the docs or not, but it's not enough to just provide extra regex named groups. You should also update the protected function fillMatches(array $matches = []): void
{
// the parent method only parses datetime, level, and message
parent::fillMatches($matches);
// for other properties, fill them in $this->context or $this->extra
$this->context = [
'env' => $matches['env'] ?? '',
'code' => $matches['code'] ?? '',
'payload' => json_decode($matches['payload'] ?? 'null'),
];
// DO NOT assign properties outside of 'context' or 'extra' arrays, as it will not get returned to the frontend.
$this->env = $matches['env']; // Do not do this
} If you have any other custom properties/matches to define on the log, do it on the So, now that you have the properties clearly defined inside the context or extra arrays, it's time to update the column definitions: /** @var array|string[][] */
public static array $columns = [
['label' => 'Level', 'data_path' => 'level'],
['label' => 'Date/Time', 'data_path' => 'datetime'],
['label' => 'Env', 'data_path' => 'context.env'],
['label' => 'Code', 'data_path' => 'context.code'],
['label' => 'Message', 'data_path' => 'message'],
['label' => 'Payload', 'data_path' => 'context.payload'],
]; Let me know if this helps, or if you have any more questions :) |
Beta Was this translation helpful? Give feedback.
Hey @jfvoliveira , glad you're experimenting with custom logs!
So, I'm not sure if I missed it in the docs or not, but it's not enough to just provide extra regex named groups. You should also update the
fillMatches()
method that you mentioned above, to something like this: