-
Notifications
You must be signed in to change notification settings - Fork 433
feat(logger): customizing logger output #140
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
133f260
feat(logger): readable log_dict seq
michaelbrewer f9f4bde
refactor: use None instead of
michaelbrewer a9cb740
Merge branch 'develop' into fix-log-key-order
michaelbrewer e148ffc
feat(logging): allow for custom json order
michaelbrewer 3e0116b
feat(logging): suppress some log keys
michaelbrewer e4c1e8e
refactor: make requested changes
michaelbrewer 42f6b22
chore: doc typo
michaelbrewer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -116,3 +116,57 @@ class Unserializable: | |||||
# THEN json_default should not be in the log message and the custom unserializable handler should be used | ||||||
assert log_dict["message"]["x"] == "<non-serializable: Unserializable>" | ||||||
assert "json_default" not in log_dict | ||||||
|
||||||
|
||||||
def test_log_dict_key_seq(stdout): | ||||||
# GIVEN the default logger configuration | ||||||
logger = Logger(stream=stdout) | ||||||
|
||||||
# WHEN logging a message | ||||||
logger.info("Message") | ||||||
|
||||||
log_dict: dict = json.loads(stdout.getvalue()) | ||||||
|
||||||
# THEN the beginning key sequence must be `level,location,message,timestamp` | ||||||
assert ",".join(list(log_dict.keys())[:4]) == "level,location,message,timestamp" | ||||||
|
||||||
|
||||||
def test_log_dict_key_custom_seq(stdout): | ||||||
# GIVEN a logger configuration with format_keys set to ["message"] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
logger = Logger(stream=stdout, log_record_order=["message"]) | ||||||
|
||||||
# WHEN logging a message | ||||||
logger.info("Message") | ||||||
|
||||||
log_dict: dict = json.loads(stdout.getvalue()) | ||||||
|
||||||
# THEN the first key should be "message" | ||||||
assert list(log_dict.keys())[0] == "message" | ||||||
|
||||||
|
||||||
def test_log_custom_formatting(stdout): | ||||||
# GIVEN a logger where we have a custom `location`, 'datefmt' format | ||||||
logger = Logger(stream=stdout, location="[%(funcName)s] %(module)s", datefmt="fake-datefmt") | ||||||
|
||||||
# WHEN logging a message | ||||||
logger.info("foo") | ||||||
|
||||||
log_dict: dict = json.loads(stdout.getvalue()) | ||||||
|
||||||
# THEN the `location` and "timestamp" should match the formatting | ||||||
assert log_dict["location"] == "[test_log_custom_formatting] test_aws_lambda_logging" | ||||||
assert log_dict["timestamp"] == "fake-datefmt" | ||||||
|
||||||
|
||||||
def test_log_dict_key_strip_nones(stdout): | ||||||
# GIVEN a logger confirmation where we set `location` and `timestamp` to None | ||||||
# Note: level, sampling_rate and service can not be suppressed | ||||||
logger = Logger(stream=stdout, level=None, location=None, timestamp=None, sampling_rate=None, service=None) | ||||||
|
||||||
# WHEN logging a message | ||||||
logger.info("foo") | ||||||
|
||||||
log_dict: dict = json.loads(stdout.getvalue()) | ||||||
|
||||||
# THEN the keys should only include `level`, `message`, `service`, `sampling_rate` | ||||||
assert sorted(log_dict.keys()) == ["level", "message", "sampling_rate", "service"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.