-
Notifications
You must be signed in to change notification settings - Fork 715
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
Make default file sink encoding utf8 #339
Comments
Well, I tried to copy the default behavior of There actually exists a big discussion about the question of default locale encoding for Python files: PEP 597: Use UTF-8 for default text file encoding. It seems reasonable to make As you mentioned JSON serialization, I also think we should set Line 258 in 93f1990
Otherwise, non-ascii characters are escaped despite the file being opened with |
I think you kinda have made the decision already tho by including emoji-icon things that don't render on my windows machine. It also creates technically invalid JSON (tho it works with the stdlib json parser/serializer). I have several scripts that use loguru but monkey patch in a new The PEP discussion you link to is pretty interesting, but I don't think the reasons they mention for not making the default encoding utf8 (that some windows users just want to do their job and not delve into the oddities of file encodings) apply to the average lo-guru. Unrelated: I was thinking of opening a pull request if/when I find the time to allow the serialize option for a sink to be either a bool or a callable that accepts |
Fair point. 😄
For my information, can you please explain me how this can lead to the creation of an invalid JSON?
Yes, you're right. Actually it will be much better to default to We just need to make sure to bump the Loguru version to
This is a suggestion that came up several times (#278, #203). def formatter(record):
record["extra"]["serialized"] = orjson.dumps(record, default=str)
return "{extra[serialized]}\n"
logger.add("file.log", format=formatter) There exists an entry about custom serialization in the documentation: Serializing log messages using a custom function. |
@Delgan OH NO! I thought I had responded and I did not! Sorry for the delay! :/ You are correct that the 'invalid json' is really me stringifying/making JSON on a windows machine and parsing it on a linux machine. So I was wrong Per a serialization handler, I looked at the 2 issues you mentioned and my monkey-patching is pretty similar.
I quite like the #278 version as it also uses orjson!
The serialization you mention WOULD work for me, but (imo) the examples you give don't totally fit the awesome functional-ness of the current version of loguru; I think being able to give a serialize-method to the as for the timedelta problem, stdlib json, orjson and python-rapidjson all support a default argument which is quite nice. FOOD FOR THOUGHT: Would you consider making orjson (and/or python-rapidjson would be another good option)an optional requirement of loguru and have loguru use an orjson-specific serializer? I see a few benefits:
(You could even use the library (i wrote) that swaps out json-libs if you wanna checkit out => jsonbourne) (PS: I am pretty sure jsonbourne supports timedelta) Additional question: You mention on the loguru README that you were at one point aiming to make loguru faster than the builtin std-lib logging module/pkg (possibly using a c/cpp extension), what happened to that? did you ever make any benchmarks? |
@Delgan Don't mean to bother you, but I am wondering if you ever read me reply above. |
Hey @jessekrubin. I had read your comment but I postponed my answer because I was a little short of time. Also, I gave priority to support requests rather than to the discussion here as it seemed less urgent. Because this is a subject worthy of further consideration, it takes me some time to formulate a proper response. :) Anyway, thanks for the clarification about invalid JSON (de)serialization. So at least it's not a bug in About making the I see If I give special treatment to It is also necessary to think about the type of object that would be passed to such function. What I mean is that the Basically, I have quite high threshold for adding new features. There already exists different way to customize the serialization of logged message (through Following the same reasoning, if someone prefers to use something different than the standard As we are discussing speed: I still plan to re-implement some part of That's it. I hope this answers some of your questions. 😉 |
Fixed on |
Great improvement |
Making the default file encoding utf-8 would be quite nice (in my opinion and would be better for JSON serialization).
The text was updated successfully, but these errors were encountered: