Skip to content

improv: logger autocomplete for PyCharm #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

## [1.1.1] - 2020-08-14
### Fixed
9 changes: 7 additions & 2 deletions aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
@@ -34,7 +34,11 @@ def _is_cold_start() -> bool:
return cold_start


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

def inject_lambda_context(self, lambda_handler: Callable[[Dict, Any], Any] = None, log_event: bool = False):
19 changes: 17 additions & 2 deletions tests/functional/test_logger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import io
import json
import logging
@@ -295,7 +296,7 @@ def test_logger_children_propagate_changes(stdout):
assert child.parent.name == "order"


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


def test_logger_level_case_insensitive(stdout):
def test_logger_level_case_insensitive():
# GIVEN a Loggers is initialized
# WHEN log level is set as "info" instead of "INFO"
logger = Logger(level="info")
@@ -344,3 +345,17 @@ def test_logger_level_env_var_as_int(monkeypatch):
monkeypatch.setenv("LOG_LEVEL", 50)
with pytest.raises(ValueError, match="Unknown level: '50'"):
Logger()


def test_logger_record_caller_location(stdout):
# GIVEN Logger is initialized
logger = Logger(stream=stdout)

# WHEN log statement is run
logger.info("log")

# THEN 'location' field should have
# the correct caller resolution
caller_fn_name = inspect.currentframe().f_code.co_name
log = capture_logging_output(stdout)
assert caller_fn_name in log["location"]