Skip to content

Commit

Permalink
Improve handling of newlines in log messages (#596)
Browse files Browse the repository at this point in the history
* Avoid adding newlines to log messages

* Update CONTRIBUTORS.md

* cli: avoid skipping empty lines

* docs: update changelog

---------

Co-authored-by: Bogdan Popa <bogdan@defn.io>
  • Loading branch information
5tefan and Bogdanp authored Jan 13, 2024
1 parent 40ff80c commit 2808d85
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ of those changes to CLEARTYPE SRL.
| [@DiegoPomares](https://github.com/DiegoPomares/) | Diego Pomares |
| [@pahrohfit](https://github.com/pahrohfit/) | Robert Dailey |
| [@nhairs](https://github.com/nhairs) | Nicholas Hairs |
| [@5tefan](https://github.com/5tefan/) | Stefan Codrescu |
4 changes: 4 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ Fixed

* The ``CurrentMessage`` middleware now works under AsyncIO. (`#586`_,
`#593`_, `@pahrohfit`_)
* Improved logging behavior under different buffer modes. (`#596`_,
`@5tefan`_)

.. _#586: https://github.com/Bogdanp/dramatiq/issues/586
.. _#593: https://github.com/Bogdanp/dramatiq/pull/593
.. _#596: https://github.com/Bogdanp/dramatiq/pull/596
.. _@pahrohfit: https://github.com/pahrohfit
.. _@5tefan: https://github.com/5tefan

Added
^^^^^
Expand Down
11 changes: 2 additions & 9 deletions dramatiq/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,21 +338,14 @@ def watch_logs(log_filename, pipes, stop):
for event in events:
try:
while event.poll():
# StreamHandler writes newlines into the pipe separately
# from the actual log entry; to avoid back-to-back newlines
# in the events pipe (causing multiple entries on a single
# line), discard newline-only data from the pipe
try:
data = event.recv_bytes()
except EOFError:
event.close()
raise

data = data.decode("utf-8", errors="replace").rstrip("\n")
if not data:
continue

log_file.write(data + "\n")
data = data.decode("utf-8", errors="replace")
log_file.write(data)
log_file.flush()
except BrokenPipeError:
event.close()
Expand Down

0 comments on commit 2808d85

Please sign in to comment.