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

Allow custom serializers #372

Closed
XayOn opened this issue Nov 30, 2020 · 5 comments
Closed

Allow custom serializers #372

XayOn opened this issue Nov 30, 2020 · 5 comments
Labels
enhancement Improvement to an already existing feature wontfix This will not be worked on

Comments

@XayOn
Copy link

XayOn commented Nov 30, 2020

The hability to specify a custom serializer for a file could be useful to save files in human-readable serializable formats such as the one followed by splunk. This would also open a door to save pickled, xml or any other log format the user wants (for whatever reason... I cant think of a good one to save pickled data or xml in logs tbh).

Should be simple enough. I'm proposing a backwards-compatible implementation where serializer is either a boolean, or a custom serializer directly.

return json.dumps(serializable, default=str) + "\n"

@XayOn
Copy link
Author

XayOn commented Nov 30, 2020

I'm leaving this idea here, but will come back tomorrow with a pull request for this proposal 😃

@XayOn
Copy link
Author

XayOn commented Dec 1, 2020

I was thinking on using a custom sink with serialize=True, that would serve the same purpose except I would have to de-serialize each time and the overhead would be terrific 😞

@Delgan
Copy link
Owner

Delgan commented Dec 1, 2020

Hi! Thanks for your interest on loguru and thanks for being willing to contribute.

This feature has been requested several times but so far I have always been reluctant to add it.
The most recent discussion on this topic: #339 (comment)

I don't know if you saw it but there exists a snippet in the documentation: Serializing log messages using a custom function

I think using a custom formatter is not too cumbersome and should be the way to go. I prefer suggesting this solution over complicating the serialize parameter.

def formatter(record):
    record["extra"]["serialized"] = orjson.dumps(record, default=str)
    return "{extra[serialized]}\n"

logger.add("file.log", format=formatter)

In this way, the user has the possibility to customize the serialization process entirely.

@XayOn
Copy link
Author

XayOn commented Dec 1, 2020

The patch() function is actually better for what I was looking for!
I thought I read enough on the documentation but I missed that part.

Thanks and thanks for this great library!

@XayOn XayOn closed this as completed Dec 1, 2020
@Delgan
Copy link
Owner

Delgan commented Dec 2, 2020

Thanks for your understanding, @XayOn!

@Delgan Delgan added enhancement Improvement to an already existing feature wontfix This will not be worked on labels Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement to an already existing feature wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants