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

Invalid JSON #84

Open
ryan-nash opened this issue Oct 11, 2017 · 6 comments
Open

Invalid JSON #84

ryan-nash opened this issue Oct 11, 2017 · 6 comments
Labels

Comments

@ryan-nash
Copy link

The method described in the README to get pure JSON appears to output invalid JSON.

This is the example output from the docs:

{"datetime":"2015-04-16 10:28:41.186728","logLevel":"INFO","message":"Message content","context":"{"1":"foo","2":"bar"}"}

It doesn't pass a JSON validator and causes json_decode() to return null.

This is valid JSON, without quotes around the context value:

{"datetime":"2015-04-16 10:28:41.186728","logLevel":"INFO","message":"Message content","context":{"1":"foo","2":"bar"}}

At the moment I'm just using Regex to remove the quotes.

@katzgrau katzgrau added the Bug label Oct 12, 2017
@katzgrau
Copy link
Owner

Well then, guess we'll have to fix this one. Thanks for the report!

@flavienbwk
Copy link

It seems like there was no update since october 2017, but the problem is still here.

Thank you for solving this annoying problem !

@onno-vos-dev
Copy link
Contributor

@flavienbwk Sorry to sound negative here but perhaps you could consider fixing the issue? Any Open Source project stands and falls with the help of others creating a Pull Request to help resolve any open bugs or issues that are found.

The negative tone of "Thank you for solving this annoying problem !" isn't really necessary since you could easily try and resolve the issue and create a PR for this yourself ;)

@flavienbwk
Copy link

flavienbwk commented Jul 9, 2018

Oh, didn't want to sound negative, sorry for this!
(Actually just wanted to thank you for trying to solve this 😄 ).

@onno-vos-dev
Copy link
Contributor

Hint, I believe the problem exists in: https://github.com/katzgrau/KLogger/blob/master/src/Logger.php#L277 ;)

@gabema
Copy link

gabema commented Oct 23, 2018

Actually the sample code is incorrect. The following sample code will work to produce valid per-line JSON

        $contextValue = '{context}';
        $logFormat = json_encode([
            'datetime' => '{date}',
            'logLevel' => '{level}',
            'message' => '{message}',
            'context' => $contextValue,
        ]);
        $logFormat = str_replace("\"$contextValue\"", $contextValue, $logFormat);

The reason why this is needed is because the string '{context}' gets JSON encoded to double quotes. So after the logFormat has been serialized you should need to remove the double quotes that were added by the JSON serialization.

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

5 participants