Skip to content

Commit 6215fd6

Browse files
authored
Merge pull request #110 from heitorlessa/improv/logger-auto-complete
improv: logger autocomplete for PyCharm
2 parents 32549c8 + 75b2c16 commit 6215fd6

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Fixed
1010
- **Docs**: Clarify confusion on Tracer reuse and `auto_patch=False` statement
11+
- **Logger**: Autocomplete for log statements in PyCharm
1112

1213
## [1.1.1] - 2020-08-14
1314
### Fixed

aws_lambda_powertools/logging/logger.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ def _is_cold_start() -> bool:
3434
return cold_start
3535

3636

37-
class Logger:
37+
# PyCharm does not support autocomplete via getattr
38+
# so we need to return to subclassing removed in #97
39+
# All methods/properties continue to be proxied to inner logger
40+
# https://github.com/awslabs/aws-lambda-powertools-python/issues/107
41+
class Logger(logging.Logger): # lgtm [py/missing-call-to-init]
3842
"""Creates and setups a logger to format statements in JSON.
3943
4044
Includes service name and any additional key=value into logs
@@ -187,7 +191,8 @@ def _configure_sampling(self):
187191
self.log_level = logging.DEBUG
188192
except ValueError:
189193
raise InvalidLoggerSamplingRateError(
190-
f"Expected a float value ranging 0 to 1, but received {self.sampling_rate} instead. Please review POWERTOOLS_LOGGER_SAMPLE_RATE environment variable." # noqa E501
194+
f"Expected a float value ranging 0 to 1, but received {self.sampling_rate} instead."
195+
f"Please review POWERTOOLS_LOGGER_SAMPLE_RATE environment variable."
191196
)
192197

193198
def inject_lambda_context(self, lambda_handler: Callable[[Dict, Any], Any] = None, log_event: bool = False):

tests/functional/test_logger.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import inspect
12
import io
23
import json
34
import logging
@@ -295,7 +296,7 @@ def test_logger_children_propagate_changes(stdout):
295296
assert child.parent.name == "order"
296297

297298

298-
def test_logger_child_not_set_returns_same_logger(stdout):
299+
def test_logger_child_not_set_returns_same_logger():
299300
# GIVEN two Loggers are initialized with the same service name
300301
# WHEN child param isn't set
301302
logger_one = Logger(service="something")
@@ -308,7 +309,7 @@ def test_logger_child_not_set_returns_same_logger(stdout):
308309
assert logger_one.name is logger_two.name
309310

310311

311-
def test_logger_level_case_insensitive(stdout):
312+
def test_logger_level_case_insensitive():
312313
# GIVEN a Loggers is initialized
313314
# WHEN log level is set as "info" instead of "INFO"
314315
logger = Logger(level="info")
@@ -344,3 +345,17 @@ def test_logger_level_env_var_as_int(monkeypatch):
344345
monkeypatch.setenv("LOG_LEVEL", 50)
345346
with pytest.raises(ValueError, match="Unknown level: '50'"):
346347
Logger()
348+
349+
350+
def test_logger_record_caller_location(stdout):
351+
# GIVEN Logger is initialized
352+
logger = Logger(stream=stdout)
353+
354+
# WHEN log statement is run
355+
logger.info("log")
356+
357+
# THEN 'location' field should have
358+
# the correct caller resolution
359+
caller_fn_name = inspect.currentframe().f_code.co_name
360+
log = capture_logging_output(stdout)
361+
assert caller_fn_name in log["location"]

0 commit comments

Comments
 (0)