Skip to content

Conversation

@Seldaek
Copy link
Contributor

@Seldaek Seldaek commented Nov 11, 2025

Fixes #10
Fixes #22

@Seldaek
Copy link
Contributor Author

Seldaek commented Nov 11, 2025

cc @Bilge @derrabus @MidnightDesign @sasezaki any thoughts here?

/**
* @param mixed $level
*/
private function normalizeLevel($level): int|string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be static.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marked static and protected as well because #10 shows this might come in handy

$message = $this->interpolate($message, $context);
}

$level = $this->normalizeLevel($level);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a breaking change though..?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I get it. The $recordsByLevel index could never have worked with anything else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I don't believe this carries much risk as it was broken already per #10

*
* @psalm-type log_record_array array{level: string, message: string|\Stringable, context: mixed[]}
* @psalm-type has_record_array array{level?: string, message?: string|\Stringable, context?: array<array-key, mixed>}
* @psalm-type log_record_array array{level: mixed, message: string|\Stringable, context: mixed[]}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're normalizing it to string|int, this can also be string|int, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, normalization means exactly "input may be everything (mixed) and output is string|int".

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm not understanding correctly, but what I'm saying is: the log_record_array is only used on $records and $recordsByLevel - and both of them can never have anything other than string|int in the level offset. At least not after this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for this type @MidnightDesign is correct IMO, I have changed it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $level of the query methods can be kept a bit narrower: string|int|Stringable|UnitEnum. They can't respond with true to anything else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Bilge
Copy link

Bilge commented Nov 11, 2025

Seems fine.

/**
* @param mixed $level
*/
protected static function normalizeLevel($level): int|string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that exposing this to extending classes is on purpose, so that they can implement custom normalization behavior, but is that really worth increasing the API surface?

Also, if this behavior should be allowed to be modified, wouldn't composition (optionally passing in some kind of normalizer) be better than the inheritance route?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

composition for a class that has so many public methods is a pain IMO if all you want to do is add normalization for a custom logger..

But maybe we're better off leaving this private until a real use case materializes, because enum+stringable support should already cover most use cases out there.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're still calling normalizeLevel() with $this-> in multiple places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right 🙈

Copy link
Contributor

@derrabus derrabus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@Seldaek
Copy link
Contributor Author

Seldaek commented Nov 11, 2025

Alright then, thanks everyone. I'll wrap up a release to get all this out.

@Seldaek Seldaek merged commit 89820fa into main Nov 11, 2025
@Seldaek Seldaek deleted the mixed-levels branch November 11, 2025 10:25
Comment on lines +430 to +433
/**
* @param mixed $level
*/
private static function normalizeLevel($level): int|string
Copy link

@theofidry theofidry Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* @param mixed $level
*/
private static function normalizeLevel($level): int|string
private static function normalizeLevel(mixed $level): int|string

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I thought this was from php8.1 somehow.. Not that huge a deal anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

level may be anything Cannot access offset of type Monolog\Level on array

7 participants