Introduction
After you complete the Python installation, unhandled exceptions are reported automatically. Calllerix.capture_exception() to report handled exceptions yourself with extra
context.
Automatic capture
lerix.init() installs two process-level hooks:
sys.excepthook. An exception that escapes the main thread is reported, given up toflush_timeoutseconds to leave, and then handed to the previous hook so the traceback is still printed and the interpreter exits as it normally would.KeyboardInterruptandSystemExitare not reported.threading.excepthook. An exception that kills athreading.Threadis reported with the thread name inmetadata.thread.
capture_unhandled=False to skip both.
asyncio
Exceptions inside tasks whose result is never awaited are only logged by asyncio. Callinstall_asyncio_handler after creating the loop to report
them too:
Web frameworks
Frameworks turn exceptions into a500 response before they reach the
process hooks, so each one has its own hook. They all attach the request
method, URL, route, status code and the X-Request-Id header when present,
under metadata.request, and then let the framework’s own error handling
run unchanged.
Change what is reported
Every hook accepts ashould_capture callable that receives the exception
and the HTTP status that will be sent (when known) and returns whether to
report it:
main.py
LerixFlask(app, should_capture=...). For Django set
it on the class in settings.py:
settings.py
Several clients
The hooks use the client created bylerix.init(). Pass client= to a hook
(or set LerixMiddleware.client for Django) when one process reports to
more than one project.
Report an error manually
orders.py
except block you can leave the exception out and
capture_exception() picks up the one being handled. It never raises and
never blocks: the report is sent from a background thread, and the returned
Future resolves once the report has been accepted or given up on. Use
capture_message for something that was not raised:
Parameters
BugType values
BugSeverity values
What is attached to a report
Flush before exit
Reports are sent in the background. Long-running servers do not need to do anything, and the SDK flushes on normal interpreter exit. In short-lived scripts, workers or serverless handlers, callflush() before returning:
Other functions
LerixClient can also be instantiated directly, with the same options as
init(), when you need more than one client in a process.
Disable in tests
Passenabled=False to turn every call into a no-op without changing your
code: