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

GelfHandler "Message is invalid: additional key contains invalid characters" #1927

Closed
ybelenko opened this issue Nov 21, 2024 · 1 comment
Closed
Labels
Milestone

Comments

@ybelenko
Copy link

ybelenko commented Nov 21, 2024

Monolog version 1|2|3?

{
    "name": "monolog/monolog",
    "version": "3.8.0",
    "source": {
        "type": "git",
        "url": "https://github.com/Seldaek/monolog.git",
        "reference": "32e515fdc02cdafbe4593e30a9350d486b125b67"
    }
}

With the basic log call

$logger->info('Hello World', ['invalid context' => 'test']);
[2024-11-21T12:21:39.764122+00:00] Cron.ERROR: Uncaught Exception RuntimeException: "Message is invalid: additional key 'ctxt_invalid context' contains invalid characters" at /Users/ybelenko/Sites/bdo_pay/vendor/graylog2/gelf-php/src/Gelf/Publisher.php line 58 {"exception":"[object] (RuntimeException(code: 0): Message is invalid: additional key 'ctxt_invalid context' contains invalid characters at /Users/ybelenko/Sites/bdo_pay/vendor/graylog2/gelf-php/src/Gelf/Publisher.php:58)"} []

I guess Monolog should prepare $context and $extra for Publisher and strip spaces.
Valdiation part in vendor/graylog2/gelf-php/src/Gelf/MessageValidator.php:

foreach ($message->getAllAdditionals() as $key => $value) {
    if (!preg_match('#^[\w\.\-]*$#', $key)) {
        $reason = sprintf(
            "additional key '%s' contains invalid characters",
            $key
        );

        return false;
    }
}
@ybelenko ybelenko added the Bug label Nov 21, 2024
@Seldaek Seldaek closed this as completed in 5958473 Dec 5, 2024
@Seldaek Seldaek added this to the 3.x milestone Dec 5, 2024
@ybelenko
Copy link
Author

ybelenko commented Dec 11, 2024

Thanks, you're almost nailed it. But now I got TypeError

[2024-12-11T21:17:56.084934+01:00] Pay.ERROR: Uncaught Exception TypeError: "preg_replace(): Argument #3 ($subject) must be of type array|string, int given" at C:\WebServer\xampp\slick\main\vendor\monolog\monolog\src\Monolog\Formatter\GelfMessageFormatter.php line 130 {"exception":"[object] (TypeError(code: 0): preg_replace(): Argument #3 ($subject) must be of type array|string, int given at C:\\WebServer\\xampp\\slick\\main\\vendor\\monolog\\monolog\\src\\Monolog\\Formatter\\GelfMessageFormatter.php:130)"} []

because I use indexed array as context. Could you fix it with:

---            $key = (string) preg_replace('#[^\w\.\-]#', '-', $key);
+++            $key = (string) preg_replace('#[^\w\.\-]#', '-', (string) $key);

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

2 participants