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
Extra parameter is available for all log levels, as implemented in the standard logging library. It accepts any dictionary, and it'll be added as part of the root structure of the logs.
<summary><strong>Excerpt output in CloudWatch Logs</strong></summary>
210
+
211
+
```json:title=cloudwatch_logs.json
212
+
{
213
+
"timestamp": "2021-01-12 14:08:12,357",
214
+
"level": "INFO",
215
+
"location": "collect.handler:1",
216
+
"service": "payment",
217
+
"sampling_rate": 0.0,
218
+
"request_id": "1123", // highlight-line
219
+
"message": "Collecting payment"
220
+
}
221
+
```
222
+
</details>
223
+
194
224
## Reusing Logger across your code
195
225
196
226
Logger supports inheritance via `child` parameter. This allows you to create multiple Loggers across your code base, and propagate changes such as new keys to all Loggers.
If you're migrating from other Loggers, there are few key points to be aware of: **Service parameter**, **Inheriting Loggers**, **Overriding Log records**, and **Logging exceptions**.
293
323
294
-
### The service parameter
324
+
### service parameter
295
325
296
326
Service is what defines what the function is responsible for, or part of (e.g payment service), and the name of the Logger.
297
327
298
328
For Logger, the `service` is the logging key customers can use to search log operations for one or more functions - For example, **search for all errors, or messages like X, where service is payment**.
299
329
300
-
### Inheriting Loggers
330
+
### inheriting Loggers
301
331
302
332
> Python Logging hierarchy happens via the dot notation: `service`, `service.child`, `service.child_2`.
303
333
@@ -325,7 +355,7 @@ In this case, Logger will register a Logger named `payment`, and a Logger named
325
355
326
356
This can be fixed by either ensuring both has the `service` value as `payment`, or simply use the environment variable `POWERTOOLS_SERVICE_NAME` to ensure service value will be the same across all Loggers when not explicitly set.
327
357
328
-
### Overriding Log records
358
+
### overriding Log records
329
359
330
360
You might want to continue to use the same date formatting style, or override `location` to display the `package.function_name:line_number` as you previously had.
Some keys cannot be supressed in the Log records: `sampling_rate` is part of the specification and cannot be supressed; `xray_trace_id` is supressed automatically if X-Ray is not enabled in the Lambda function, and added automatically if it is.
357
387
358
-
### Logging exceptions
388
+
### logging exceptions
359
389
360
390
When logging exceptions, Logger will add a new key named `exception`, and will serialize the full traceback as a string.
**What's the difference between `structure_log` and `extra`?**
469
+
470
+
Keys added with `structure_log` will persist across multiple log messages while keys added via `extra` will only be available in a given log message operation.
471
+
472
+
**Example - Persisting payment_id not request_id**
0 commit comments