Skip to content

Support more flexible normalizer #1865

Open
@torreytsui

Description

@torreytsui

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions