Skip to content

Commit ac8c16e

Browse files
authored
docs: new section "Migrating from other Loggers" (#148)
* chore: fix typo in test comment * chore: fix typo in metrics * improv: add section to answer common migration questions Signed-off-by: heitorlessa <[email protected]>
1 parent a7d7c86 commit ac8c16e

File tree

3 files changed

+103
-6
lines changed

3 files changed

+103
-6
lines changed

docs/content/core/logger.mdx

+101-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Key | Type | Example
9090
**function_request_id**| str | "899856cb-83d1-40d7-8611-9e78f15f32f4"
9191

9292
<details>
93-
<summary><strong>Exerpt output in CloudWatch Logs</strong></summary>
93+
<summary><strong>Excerpt output in CloudWatch Logs</strong></summary>
9494

9595
```json:title=cloudwatch_logs.json
9696
{
@@ -164,9 +164,9 @@ def handler(event, context):
164164
```
165165

166166
<details>
167-
<summary><strong>Exerpt output in CloudWatch Logs</strong></summary>
167+
<summary><strong>Excerpt output in CloudWatch Logs</strong></summary>
168168

169-
```json:title=cloudwatch_logs.jsonn
169+
```json:title=cloudwatch_logs.json
170170
{
171171
"timestamp": "2020-05-24 18:17:33,774",
172172
"level": "INFO",
@@ -242,7 +242,7 @@ def handler(event, context):
242242
```
243243

244244
<details>
245-
<summary><strong>Exerpt output in CloudWatch Logs</strong></summary>
245+
<summary><strong>Excerpt output in CloudWatch Logs</strong></summary>
246246

247247
```json:title=cloudwatch_logs.json
248248
{
@@ -260,3 +260,100 @@ def handler(event, context):
260260
}
261261
```
262262
</details>
263+
264+
265+
## Migrating from other Loggers
266+
267+
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**.
268+
269+
### The service parameter
270+
271+
Service is what defines what the function is responsible for, or part of (e.g payment service), and the name of the Logger.
272+
273+
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**.
274+
275+
### Inheriting Loggers
276+
277+
> Python Logging hierarchy happens via the dot notation: `service`, `service.child`, `service.child_2`.
278+
279+
For inheritance, Logger uses a `child=True` parameter along with `service` being the same value across Loggers.
280+
281+
For child Loggers, we introspect the name of your module where `Logger(child=True, service="name")` is called, and we name your Logger as **{service}.{filename}**.
282+
283+
A common issue when migrating from other Loggers is that `service` might be defined in the parent Logger (no child param), and not defined in the child Logger:
284+
285+
```python:title=incorrect_logger_inheritance.py
286+
# app.py
287+
import my_module
288+
from aws_lambda_powertools import Logger
289+
290+
logger = Logger(service="payment") # highlight-line
291+
...
292+
293+
# my_module.py
294+
from aws_lambda_powertools import Logger
295+
296+
logger = Logger(child=True) # highlight-line
297+
```
298+
299+
In this case, Logger will register a Logger named `payment`, and a Logger named `service_undefined`. The latter isn't inheriting from the parent, and will have no handler, thus no message being logged to standard output.
300+
301+
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.
302+
303+
### Overriding Log records
304+
305+
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.
306+
307+
Logger allows you to either change the format or suppress the following keys altogether at the initialization: `location`, `timestamp`, `level`, and `datefmt`
308+
309+
```python
310+
from aws_lambda_powertools import Logger
311+
312+
# override default values for location and timestamp format
313+
logger = Logger(stream=stdout, location="[%(funcName)s] %(module)s", datefmt="fake-datefmt") # highlight-line
314+
315+
# suppress location key
316+
logger = Logger(stream=stdout, location=None) # highlight-line
317+
```
318+
319+
Alternatively, you can also change the order of the following log record keys via the `log_record_order` parameter: `level`, location`, message`, and timestamp`
320+
321+
```python
322+
from aws_lambda_powertools import Logger
323+
324+
# make message as the first key
325+
logger = Logger(stream=stdout, log_record_order=["message"]) # highlight-line
326+
327+
# Default key sorting order
328+
logger = Logger(stream=stdout, log_record_order=["level","location","message","timestamp"]) # highlight-line
329+
```
330+
331+
### Logging exceptions
332+
333+
When logging exceptions, Logger will add a new key named `exception`, and will serialize the full traceback as a string.
334+
335+
```python:title=logging_an_exception.py
336+
from aws_lambda_powertools import Logger
337+
logger = Logger()
338+
339+
try:
340+
raise ValueError("something went wrong")
341+
except Exception:
342+
logger.exception("Received an exception") # highlight-line
343+
```
344+
345+
<details>
346+
<summary><strong>Excerpt output in CloudWatch Logs</strong></summary>
347+
348+
```json:title=cloudwatch_logs.json
349+
{
350+
"level": "ERROR",
351+
"location": "<module>:4",
352+
"message": "Received an exception",
353+
"timestamp": "2020-08-28 18:11:38,886",
354+
"service": "service_undefined",
355+
"sampling_rate": 0.0,
356+
"exception": "Traceback (most recent call last):\n File \"<input>\", line 2, in <module>\nValueError: something went wrong"
357+
}
358+
```
359+
</details>

docs/content/core/metrics.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ metrics.add_metadata(key="booking_id", value="booking_uuid") # highlight-line
113113
This will be available in CloudWatch Logs to ease operations on high cardinal data.
114114

115115
<details>
116-
<summary><strong>Exerpt output in CloudWatch Logs</strong></summary>
116+
<summary><strong>Excerpt output in CloudWatch Logs</strong></summary>
117117

118118
```json:title=cloudwatch_logs.json
119119
{

tests/functional/test_aws_lambda_logging.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def test_log_dict_key_seq(stdout):
132132

133133

134134
def test_log_dict_key_custom_seq(stdout):
135-
# GIVEN a logger configuration with format_keys set to ["message"]
135+
# GIVEN a logger configuration with log_record_order set to ["message"]
136136
logger = Logger(stream=stdout, log_record_order=["message"])
137137

138138
# WHEN logging a message

0 commit comments

Comments
 (0)