-
Notifications
You must be signed in to change notification settings - Fork 704
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
Add an optional hook upon record creation #221
Comments
Hi @ClementMaliet. So, I've been thinking about your problem. I don't believe adding a new hook or changing the existing one would be of much help. The handling of messages and color is rather complex. I do agree this led to an undesirable and surprising behavior: one can modify the Anyway. I think another solution is possible. def custom_format(record):
format_string = "[{time:YYYY-MM-DD HH:mm:ss.SSSS zz}]" \
"[<b><y>{module}:{line}</y></b>] - " \
"[<b>{level}</b>]: " \
"<level>%s</level>\n{exception}"
message_token = "{message}"
if not record["extra"].get("formatted", False):
if "\n" in record["message"]:
length = len("[{time:YYYY-MM-DD HH:mm:ss.SSSS zz}]"
"[{module}:{line}] - "
"[{level}]:".format(**record))
lines = record["message"].splitlines()
message_token = "\n".join("<n><w>" + "." * (length + 1 if i > 0 else 0) + ":</w></n> <level>{extra[lines][%d]}</level>" % i for i in range(len(lines)))
record["extra"]["lines"] = lines
record["extra"]["formatted"] = True
return format_string % message_token This is a little bit hackish but this is an acceptable workaround in my opinion. It seems a little bit complex, though, compared to simply starting the multi-lines message on a new line. 😄 |
Hi @Delgan, This is indeed a reasonable workaround, the idea was hackish from the start and it does seem understandable not to change the API to support such a niche use case, all the more so if it may introduce confusion as to what information Though I do agreed that it might indeed be useful to put some emphasis in the documentation on the fact that Thank you for the workaround which I will use to update |
I was quite worried about this very surprising behavior. As a result, I have slightly modified the way messages are handled. Basically, if |
Hello,
I have a rather log-heavy application which regularly outputs long multi-line messages. Up until version 0.4.0, I used a (admittedly hackish) custom format function to modify the record message in-place to replace it with an appropriately coloured and dot-padded version.
My actual formatter code look something like that..
However since the 0.4.1 release, whenever using colours, the message which get printed is a colour-parsed message computed long before the any hook function kicks in, be it patch or format.
I do know that modifying the record dictionary is explicitly discouraged in the documentation because of potential consistency issues such as mine. However, a hook entry point activated just after the record creation (or having the patch call there to avoid addition the API) would do the trick with no consistency issue as nothing would have ever happened with the record yet.
Now, there might be implementation details I did not considered as I am rather unfamiliar with the full loguru codebase but I think one of loguru's strength is its flexibility and I believe this would enable useful customization to the way logs are emitted by applications.
The text was updated successfully, but these errors were encountered: