@@ -29,8 +29,8 @@ def logger_setup(
29
29
service name
30
30
LOG_LEVEL: str
31
31
logging level (e.g. INFO, DEBUG)
32
- POWERTOOLS_LOGGER_SAMPLE_RATE: str
33
- samping rate ranging from 0 to 1, float precision
32
+ POWERTOOLS_LOGGER_SAMPLE_RATE: float
33
+ samping rate ranging from 0 to 1, 1 being 100% sampling
34
34
35
35
Parameters
36
36
----------
@@ -54,29 +54,26 @@ def logger_setup(
54
54
Setups structured logging in JSON for Lambda functions using env vars
55
55
56
56
$ export POWERTOOLS_SERVICE_NAME="payment"
57
+ $ export POWERTOOLS_LOGGER_SAMPLE_RATE=0.01 # 1% debug sampling
57
58
>>> from aws_lambda_powertools.logging import logger_setup
58
59
>>> logger = logger_setup()
59
60
>>>
60
61
>>> def handler(event, context):
61
62
logger.info("Hello")
62
- :param service:
63
- :param level:
64
- :param sampling_rate:
65
63
66
64
"""
67
65
service = os .getenv ("POWERTOOLS_SERVICE_NAME" ) or service
68
66
sampling_rate = os .getenv ("POWERTOOLS_LOGGER_SAMPLE_RATE" ) or sampling_rate
69
67
log_level = os .getenv ("LOG_LEVEL" ) or level
70
68
logger = logging .getLogger (name = service )
71
69
72
- # sampling a small percentage of requests with debug level, using a float value 0.1 = 10%~
73
-
74
70
try :
75
71
if sampling_rate and random .random () <= float (sampling_rate ):
76
72
log_level = logging .DEBUG
77
73
except ValueError :
78
- logger .debug (
79
- "POWERTOOLS_LOGGER_SAMPLE_RATE provided value {0} is not valid." .format (sampling_rate )
74
+ raise ValueError (
75
+ f"Expected a float value ranging 0 to 1, but received { sampling_rate } instead. Please review "
76
+ f"POWERTOOLS_LOGGER_SAMPLE_RATE environment variable."
80
77
)
81
78
82
79
logger .setLevel (log_level )
@@ -134,8 +131,6 @@ def logger_inject_lambda_context(
134
131
-------
135
132
decorate : Callable
136
133
Decorated lambda handler
137
- :param log_event:
138
- :param lambda_handler:
139
134
"""
140
135
141
136
# If handler is None we've been called with parameters
0 commit comments