Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more flexible normalizer #1865

Open
torreytsui opened this issue Jan 3, 2024 · 2 comments
Open

Support more flexible normalizer #1865

torreytsui opened this issue Jan 3, 2024 · 2 comments
Labels

Comments

@torreytsui
Copy link

Many times, I encountered use cases around custom normalization.

NormalizerFormatter::normalize / is_object($data)

To name a few:

  • Normalize a custom exception to include extra fields into the context
  • Normalize a custom object to include extra fields into the context
  • Redact fields from a custom object and selectively normalize non PII information into the context

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
Copy link
Contributor

Would this help me if I want to customize the file:line format?

$str .= '): ' . $e->getMessage() . ' at ' . $e->getFile() . ':' . $e->getLine() . ')';

@Seldaek
Copy link
Owner

Seldaek commented Apr 12, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants