You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow to override the log level while logging exceptions (#79)
While logging information during an exception `except` block, instead of
overwriting the log level with `CRITICAL`, the log level used by the
caller will be set instead.
This allows the caller to use:
```python
try:
raise ValueError()
except Exception as exc:
logger.info(f"I was expecting this error: {exc}")
```
The log message will be logged as `INFO` level, whereas it was logged as
`CRITICAL` before.
If an `exception` attribute is still passed to the logger, as in
`logger.info("something", exception=exc)`, the log level will be `INFO`
as expected but the error will also still be correctly reported (in
Error Reporting + the bubble in Cloud Logging)
Copy file name to clipboardExpand all lines: README.md
+62-3Lines changed: 62 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,6 +58,11 @@ except:
58
58
59
59
ifnot converted:
60
60
logger.critical("This is not supposed to happen", converted=converted)
61
+
62
+
try:
63
+
1/0
64
+
exceptZeroDivisionErroras exc:
65
+
logger.info("This was known to happen! {exc}")
61
66
```
62
67
63
68
The `structlog_gcp.build_processors()` function constructs structlog processors to:
@@ -70,12 +75,66 @@ For more advanced usage, see [Advanced Configuration](#advanced-configuration)
70
75
71
76
### Errors
72
77
73
-
Errors are automatically reported to the [Google Error Reporting service](https://cloud.google.com/error-reporting/).
78
+
79
+
Errors are automatically reported to the [Google Error Reporting service](https://cloud.google.com/error-reporting/), most of the time.
80
+
81
+
#### Using `logger.exception`
82
+
83
+
Using:
84
+
85
+
```python
86
+
try:
87
+
1/0
88
+
except:
89
+
logger.exception("oh no")
90
+
```
91
+
92
+
Will give you:
93
+
94
+
* The current exception can automatically added into the log event
95
+
* The log level will be `ERROR`
96
+
* The exception will be reported in Error Reporting
97
+
98
+
##### Using `logger.$LEVEL(..., exception=exc)`
99
+
100
+
Using:
101
+
102
+
```python
103
+
try:
104
+
1/0
105
+
exceptExceptionas exc
106
+
logger.info("oh no", exception=exc)
107
+
```
108
+
Will give you:
109
+
110
+
* The specified exception will be part of the log event
111
+
* The log level will be `INFO`, or whichever log level you used
112
+
* The exception will be reported in Error Reporting
113
+
114
+
115
+
##### Using `logger.$LEVEL(...)`
116
+
117
+
Not passing any `exception` argument to the logger, as in:
118
+
119
+
```python
120
+
try:
121
+
1/0
122
+
exceptExceptionas exc
123
+
logger.warning(f"oh no: {exc}")
124
+
```
125
+
126
+
Will give you:
127
+
128
+
* The exception will **not** be part of the log event.
129
+
* The log level will be `WARNING` (or whichever log level you used)
130
+
* AND the exception will **not** be reported in Error Reporting
131
+
132
+
### Configuration
74
133
75
134
You can configure the service name and the version used during the report with 2 different ways:
76
135
77
-
* By default, the library assumes to run with Cloud Function environment
78
-
variables configured, in particular [the `K_SERVICE` and `K_REVISION` variables](https://cloud.google.com/functions/docs/configuring/env-var#runtime_environment_variables_set_automatically).
136
+
* By default, the library assumes to run with Cloud Run environment
137
+
variables configured, in particular [the `K_SERVICE` and `K_REVISION` variables](https://cloud.google.com/run/docs/configuring/services/overview-environment-variables#reserved_environment_variables_for_functions).
79
138
* You can also pass the service name and revision at configuration time with:
0 commit comments