Seldaek / monolog

Sends your logs to files, sockets, inboxes, databases and various web services
https://seldaek.github.io/monolog/
MIT License
20.95k stars 1.9k forks source link

Support more flexible normalizer #1865

Open torreytsui opened 8 months ago

torreytsui commented 8 months ago

Many times, I encountered use cases around custom normalization.

NormalizerFormatter::normalize / is_object($data)

To name a few:

I understand that we can write our Processor or a Formatter or implement the JsonSerializable to achieve that.

But I can also see that the "nomalization" logic was the main customisation we really needed in those use cases.

The NormalizerFormatter class is great and I don't want to duplicate its logic.

What do you think if we make it more flexible and customizable?

Suggestion 1: introduce protected normalizeObject() method

// Monolog/Formatter/NormalizerFormatter.php

protected function normalize(mixed $data, int $depth = 0): mixed
{
    if (is_object($data)) {
        return $this->normalizeObject($data, $depth);
    }
}

Suggestion 2: introduce normalizer components

// Monolog/Formatter/NormalizerFormatter.php

public function __construct(..., $normalizers);

protected function normalize(mixed $data, int $depth = 0): mixed
{
    // Loop through each normalizer, check its support, and invoke $normalizer->normalise($data, $depth)
}

Or other suggestions? or not really worth it?

cesarreyes3 commented 5 months ago

Would this help me if I want to customize the file:line format? https://github.com/Seldaek/monolog/blob/479c936d2c230d8c467bdb3882afab45a6e6b8ad/src/Monolog/Formatter/LineFormatter.php#L261

Seldaek commented 4 months ago

IMO you can already extend and override normalize(), handling any special types you want to handle, then defer to the parent method for the rest. I don't really see what a more complicated system would bring there given this isn't a very common case I think.