15
15
16
16
Install this package with `` pip install sentry-sdk `` . Then, in your code:
17
17
18
- import sentry_sdk
19
- sentry_sdk.init(dsn="https://[email protected] /123")
18
+ ``` python
19
+ import sentry_sdk
20
+ sentry_sdk.init(
dsn = " https://[email protected] /123" )
21
+ ```
20
22
21
23
After initialization, you can capture exceptions like this:
22
24
23
- sentry_sdk.capture_exception(ValueError())
25
+ ``` python
26
+ sentry_sdk.capture_exception(ValueError ())
24
27
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
+ ```
29
33
30
34
...or send messages:
31
35
32
- sentry_sdk.capture_message("Hi Sentry!")
36
+ ``` python
37
+ sentry_sdk.capture_message(" Hi Sentry!" )
38
+ ```
33
39
34
40
## Scopes (contexts, tags)
35
41
36
42
You can create a scope to attach data to all events happening inside of it:
37
43
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 }
46
50
47
- # This one not since it is outside of the context manager
51
+ # ValueError event will have all that data attached
48
52
capture_exception(ValueError ())
49
53
54
+ # This one not since it is outside of the context manager
55
+ capture_exception(ValueError ())
56
+ ```
57
+
50
58
Scopes can be nested. If you call `` push_scope `` inside of the
51
59
`` with `` -statement again, that scope will be pushed onto a stack. It will also
52
60
inherit all data from the outer scope.
@@ -56,23 +64,29 @@ inherit all data from the outer scope.
56
64
If you never call `` init `` , no data will ever get sent to any server. In such
57
65
situations, code like this is essentially deadweight:
58
66
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
+ ```
61
71
62
72
Sentry-Python supports an alternative syntax for configuring a scope that
63
73
solves this problem:
64
74
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
+ ```
68
80
69
81
Your function will just not be executed if there is no client configured.
70
82
71
83
In your testing and development environment you still might want to run that
72
84
code without sending any events. In that case, simply call `` init `` without a
73
85
DSN:
74
86
75
- sentry_sdk.init()
87
+ ``` python
88
+ sentry_sdk.init()
89
+ ```
76
90
77
91
### Breadcrumbs
78
92
@@ -81,23 +95,27 @@ anywhere in your system ends up as a breadcrumb, see [the logging
81
95
docs] ( ./docs/logging.md ) for more information. You can, however, also create
82
96
breadcrumbs manually:
83
97
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
+ ```
91
107
92
108
You can also pass a callback to ` add_breadcrumb ` like so:
93
109
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
+ ```
101
119
102
120
The callback will only be called if a sentry client is configured.
103
121
@@ -118,7 +136,9 @@ Currently Sentry-Python does not send any personally-identifiable user data
118
136
with events by default. You need to explicitly enable this behavior with the
119
137
`` send_default_pii `` option passed to `` init `` :
120
138
121
- init(..., send_default_pii=True)
139
+ ``` python
140
+ init(... , send_default_pii = True )
141
+ ```
122
142
123
143
## Integrations
124
144
0 commit comments