1515
1616Install this package with `` pip install sentry-sdk `` . Then, in your code:
1717
18- import sentry_sdk
19- sentry_sdk.init(dsn="https://foo@sentry.io/123")
18+ ``` python
19+ import sentry_sdk
20+ sentry_sdk.init(dsn = " https://foo@sentry.io/123" )
21+ ```
2022
2123After initialization, you can capture exceptions like this:
2224
23- sentry_sdk.capture_exception(ValueError())
25+ ``` python
26+ sentry_sdk.capture_exception(ValueError ())
2427
25- try:
26- raise ValueError()
27- except Exception:
28- sentry_sdk.capture_exception()
28+ try :
29+ raise ValueError ()
30+ except Exception :
31+ sentry_sdk.capture_exception()
32+ ```
2933
3034...or send messages:
3135
32- sentry_sdk.capture_message("Hi Sentry!")
36+ ``` python
37+ sentry_sdk.capture_message(" Hi Sentry!" )
38+ ```
3339
3440## Scopes (contexts, tags)
3541
3642You can create a scope to attach data to all events happening inside of it:
3743
38- with sentry_sdk.Hub.current.push_scope():
39- with sentry_sdk.configure_scope() as scope:
40- scope.transaction = "my_view_name"
41- scope.set_tag("key", "value")
42- scope.user = {"id": 123}
43-
44- # ValueError event will have all that data attached
45- capture_exception(ValueError())
44+ ``` python
45+ with sentry_sdk.Hub.current.push_scope():
46+ with sentry_sdk.configure_scope() as scope:
47+ scope.transaction = " my_view_name"
48+ scope.set_tag(" key" , " value" )
49+ scope.user = {" id" : 123 }
4650
47- # This one not since it is outside of the context manager
51+ # ValueError event will have all that data attached
4852 capture_exception(ValueError ())
4953
54+ # This one not since it is outside of the context manager
55+ capture_exception(ValueError ())
56+ ```
57+
5058Scopes can be nested. If you call `` push_scope `` inside of the
5159`` with `` -statement again, that scope will be pushed onto a stack. It will also
5260inherit all data from the outer scope.
@@ -56,23 +64,29 @@ inherit all data from the outer scope.
5664If you never call `` init `` , no data will ever get sent to any server. In such
5765situations, code like this is essentially deadweight:
5866
59- with sentry_sdk.configure_scope() as scope:
60- scope.user = _get_user_data()
67+ ``` python
68+ with sentry_sdk.configure_scope() as scope:
69+ scope.user = _get_user_data()
70+ ```
6171
6272Sentry-Python supports an alternative syntax for configuring a scope that
6373solves this problem:
6474
65- @sentry_sdk.configure_scope
66- def _(scope):
67- scope.user = _get_user_data()
75+ ``` python
76+ @sentry_sdk.configure_scope
77+ def _ (scope ):
78+ scope.user = _get_user_data()
79+ ```
6880
6981Your function will just not be executed if there is no client configured.
7082
7183In your testing and development environment you still might want to run that
7284code without sending any events. In that case, simply call `` init `` without a
7385DSN:
7486
75- sentry_sdk.init()
87+ ``` python
88+ sentry_sdk.init()
89+ ```
7690
7791### Breadcrumbs
7892
@@ -81,23 +95,27 @@ anywhere in your system ends up as a breadcrumb, see [the logging
8195docs] ( ./docs/logging.md ) for more information. You can, however, also create
8296breadcrumbs manually:
8397
84- sentry_sdk.add_breadcrumb(
85- timestamp=datetime.datetime.now(),
86- type="log",
87- level="debug",
88- # message="hi",
89- # category="myapp.models",
90- })
98+ ``` python
99+ sentry_sdk.add_breadcrumb(
100+ timestamp = datetime.datetime.now(),
101+ type = " log" ,
102+ level = " debug" ,
103+ # message="hi",
104+ # category="myapp.models",
105+ })
106+ ```
91107
92108You can also pass a callback to ` add_breadcrumb ` like so:
93109
94- sentry_sdk.add_breadcrumb(lambda: {
95- "timestamp": datetime.datetime.now(),
96- "type": "log",
97- "level": "debug",
98- # "message": "hi",
99- # "category": "myapp.models",
100- })
110+ ``` python
111+ sentry_sdk.add_breadcrumb(lambda : {
112+ " timestamp" : datetime.datetime.now(),
113+ " type" : " log" ,
114+ " level" : " debug" ,
115+ # "message": "hi",
116+ # "category": "myapp.models",
117+ })
118+ ```
101119
102120The callback will only be called if a sentry client is configured.
103121
@@ -118,7 +136,9 @@ Currently Sentry-Python does not send any personally-identifiable user data
118136with events by default. You need to explicitly enable this behavior with the
119137`` send_default_pii `` option passed to `` init `` :
120138
121- init(..., send_default_pii=True)
139+ ``` python
140+ init(... , send_default_pii = True )
141+ ```
122142
123143## Integrations
124144
0 commit comments