-
Notifications
You must be signed in to change notification settings - Fork 709
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
Setup formatted exception to use with async pickling #1046
Conversation
Hey @js-lowes, thanks for your work on improving Loguru.
Could you please elaborate on that? To be fair, I'm not so keen on the idea of extending If I'm not mistaken, you're trying to access the formatted traceback from your sink, right? What would you think of simply adding it to the from loguru import logger
import traceback
def save_formatted_exception(record):
exception = record["exception"]
if exception is not None:
formatted_exception = "".join(traceback.format_exception(*exception))
record["extra"]["formatted_exception"] = formatted_exception
def my_sink(message):
record = message.record
print("My custom traceback:", record["extra"]["formatted_exception"])
logger.configure(
patcher=save_formatted_exception,
handlers=[{"sink": my_sink, "enqueue": True}],
)
try:
1 / 0
except Exception:
logger.exception("Error") It looks it achieve the functionality you were looking for, without needing to change the Loguru API. |
We use Elastic Search and Kafka libraries, which throw exceptions that can not be pickled. When leveraging the
Yes I totally understand that you don't want the project to be bloated!
Thanks for the response! Yes, this is a lot better. I did not know about the Do you think this should be added to the documentation? |
Ok, thanks. That clarifies that there is no unexpected error during logging, but you indeed lose access to the value of the captured exception. I agree that accessing the formatted error could be useful. Instead of adding a 4th element to def sink(message):
record = message.record
if record["exception"]:
formatted_traceback = str(record["exception"]) The problem is that currently the To sum up, I don't have a definitive answer regarding this feature, but it's something I'm thinking about.
Sure, improving documentation is always welcome. I should indeed add a note about execution order of functions when |
Hi thank you for an awesome logging lib! It has made my life 1000% easier...
I am using
enqueue=True
and I ran into some pickling related issues when it came to 3rd party libraries and their exceptions. I still want to have a stacktrace, but ultimately unless we can pickle the exception, I can't get it downstream in thesink
. Ultimately I added an extra field to theRecordException
that formats the exception and saves it as string that can be safely pickled.Overall I think this would solve some of the issues, however it isn't customizable.
I setup this initial PR as a work around to it. I didn't want to sink to much time into until you give some feedback to see if this would be even a good idea and something that would want to be adopted. Would love to clean this up after given some feedback. Please rip into it and let me know!