|
| 1 | +--- |
| 2 | +title: sys.exit |
| 3 | +description: Learn how to use Sentry to capture sys.exit calls |
| 4 | +--- |
| 5 | + |
| 6 | +The `SysExitIntegration` records `sys.exit` calls by capturing the `SystemExit` exception raised by `sys.exit`. |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +```bash |
| 11 | +pip install --upgrade "sentry-sdk" |
| 12 | +``` |
| 13 | + |
| 14 | +## Configure |
| 15 | + |
| 16 | +The `SysExitIntegration` is disabled by default, and the SDK only captures `SystemExit` exceptions automatically if this integration is manually enabled. |
| 17 | + |
| 18 | +To enable the `SysExitIntegration` and configure it to only capture unsuccessful `sys.exit` calls, use the following: |
| 19 | + |
| 20 | +<SignInNote /> |
| 21 | + |
| 22 | +```python |
| 23 | +import sentry_sdk |
| 24 | +from sentry_sdk.integrations.sys_exit import SysExitIntegration |
| 25 | + |
| 26 | +sentry_sdk.init( |
| 27 | + dsn="___PUBLIC_DSN___", |
| 28 | + integrations=[SysExitIntegration()], |
| 29 | +) |
| 30 | +``` |
| 31 | + |
| 32 | +If you wish to capture successful and unsuccessful `sys.exit` calls, use the following, instead: |
| 33 | + |
| 34 | +```python |
| 35 | +import sentry_sdk |
| 36 | +from sentry_sdk.integrations.sys_exit import SysExitIntegration |
| 37 | + |
| 38 | +sentry_sdk.init( |
| 39 | + dsn="___PUBLIC_DSN___", |
| 40 | + integrations=[SysExitIntegration(capture_successful_exits=True)], |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +A `sys.exit` call is considered "successful" when the function is passed a value of `0` or `None`. Passing any other value results in an "unsuccessful" exit. |
| 45 | + |
| 46 | + |
| 47 | +## Verify |
| 48 | + |
| 49 | +Running the following snippet should result in a `SystemExit` exception being reported to Sentry. |
| 50 | + |
| 51 | +```python |
| 52 | +import sys |
| 53 | + |
| 54 | +import sentry_sdk |
| 55 | +from sentry_sdk.integrations.sys_exit import SysExitIntegration |
| 56 | + |
| 57 | +sentry_sdk.init( |
| 58 | + dsn="___PUBLIC_DSN___", |
| 59 | + integrations=[SysExitIntegration()], |
| 60 | +) |
| 61 | + |
| 62 | +sys.exit(1) |
| 63 | +``` |
| 64 | + |
| 65 | +If you use `capture_successful_exits=True`, calling `sys.exit(0)` should also report a `SystemExit` exception to Sentry. |
| 66 | + |
| 67 | + |
| 68 | +<Alert level="info" title="Manually-raised SystemExit"> |
| 69 | + |
| 70 | +Please note that the `SysExitIntegration` only captures `SystemExit` exceptions which are raised by calling `sys.exit`. If your code raises `SystemExit` without calling `sys.exit`, the exception will not be reported to Sentry. |
| 71 | + |
| 72 | +</Alert> |
0 commit comments