Skip to content

Commit 091a32c

Browse files
committed
refactor(logger): localize powertools_dev logic and warning to ease reusing
1 parent c746636 commit 091a32c

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

aws_lambda_powertools/logging/formatter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
1010

1111
from ..shared import constants
12-
from ..shared.functions import strtobool
12+
from ..shared.functions import powertools_dev_is_set
1313

1414
RESERVED_LOG_ATTRS = (
1515
"name",
@@ -116,7 +116,7 @@ def __init__(
116116
self.json_deserializer = json_deserializer or json.loads
117117
self.json_default = json_default or str
118118
self.json_indent = (
119-
constants.PRETTY_INDENT if strtobool(os.getenv("POWERTOOLS_DEV", "0")) else constants.COMPACT_INDENT
119+
constants.PRETTY_INDENT if powertools_dev_is_set() else constants.COMPACT_INDENT
120120
) # indented json serialization when in AWS SAM Local
121121
self.json_serializer = json_serializer or partial(
122122
json.dumps, default=self.json_default, separators=(",", ":"), indent=self.json_indent

aws_lambda_powertools/shared/constants.py

+1
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@
3636
# JSON indentation level
3737
PRETTY_INDENT: int = 4
3838
COMPACT_INDENT = None
39+
POWERTOOLS_DEV_ENV: str = "POWERTOOLS_DEV"

aws_lambda_powertools/shared/functions.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import base64
22
import logging
3+
import os
4+
import warnings
35
from binascii import Error as BinAsciiError
46
from typing import Optional, Union
57

@@ -78,3 +80,12 @@ def bytes_to_string(value: bytes) -> str:
7880
return value.decode("utf-8")
7981
except (BinAsciiError, TypeError):
8082
raise ValueError("base64 UTF-8 decode failed")
83+
84+
85+
def powertools_dev_is_set() -> bool:
86+
is_on = strtobool(os.getenv("POWERTOOLS_DEV", "0"))
87+
if is_on:
88+
warnings.warn("POWERTOOLS_DEV environment variable is enabled. Increasing verbosity across utilities.")
89+
return True
90+
91+
return False

0 commit comments

Comments
 (0)