Skip to content

Commit

Permalink
Merge pull request #2444 from PrefectHQ/log-json
Browse files Browse the repository at this point in the history
Pop args out of log messages to prevent shipping non-JSON objects
  • Loading branch information
joshmeek committed Apr 29, 2020
2 parents eb59918 + 302e99b commit b923e6d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ These changes are available in the [master branch](https://github.com/PrefectHQ/
### Fixes

- Fix `server start` CLI command not respecting `version` kwarg on tagged releases - [#2435](https://github.com/PrefectHQ/prefect/pull/2435)
- Fix issue with non-JSON serializable args being used to format log messages preventing them from shipping to Cloud - [#2407](https://github.com/PrefectHQ/prefect/issues/2407)

### Deprecations

Expand Down
3 changes: 3 additions & 0 deletions src/prefect/utilities/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def emit(self, record) -> None: # type: ignore
assert isinstance(self.client, Client) # mypy assert

record_dict = record.__dict__.copy()
## remove potentially non-json serializable formatting args
record_dict.pop("args", None)

log = dict()
log["flow_run_id"] = prefect.context.get("flow_run_id", None)
log["task_run_id"] = prefect.context.get("task_run_id", None)
Expand Down
26 changes: 26 additions & 0 deletions tests/utilities/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ def test_remote_handler_captures_tracebacks(caplog, monkeypatch):
logger.handlers = []


def test_cloud_handler_formats_messages_and_removes_args(caplog, monkeypatch):
monkeypatch.setattr("prefect.client.Client", MagicMock)
client = MagicMock()
try:
with utilities.configuration.set_temporary_config(
{"logging.log_to_cloud": True}
):
logger = utilities.logging.configure_logging(testing=True)
assert hasattr(logger.handlers[-1], "client")
logger.handlers[-1].client = client

child_logger = logger.getChild("sub-test")
child_logger.info("Here's a number: %d", 42)

time.sleep(0.75)

cloud_logs = client.write_run_logs.call_args[0][0]
assert len(cloud_logs) == 1
assert cloud_logs[0]["message"] == "Here's a number: 42"
assert "args" not in cloud_logs[0]["info"]
finally:
# reset root_logger
logger = utilities.logging.configure_logging(testing=True)
logger.handlers = []


def test_remote_handler_ships_json_payloads(caplog, monkeypatch):
monkeypatch.setattr("prefect.client.Client", MagicMock)
client = MagicMock()
Expand Down

0 comments on commit b923e6d

Please sign in to comment.