Skip to content
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

vdk-logging-json: Escape newlines in log messages #121

Merged
merged 2 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions projects/vdk-core/plugins/vdk-logging-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The label names follow the labelling recommendations found at http://ltsv.org/.
The reason we chose the LTSV naming standard is due to the fact that this plugin was based on a previous LTSV-formatting plugin,
as well as the fact that there is no single JSON naming standard.

Additionally, newline characters within the message are escaped.

# Usage

Switching vdk logging can be done by simply installing the plugin:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
'"lineno":"%(lineno)s","funcname":"%(funcName)s","attemptid":"{attempt_id}","message":"%(message)s"'
)

# this class serves to escape newline characters in the log message
# as according to https://stackoverflow.com/questions/42068/how-do-i-handle-newlines-in-json
# it is currently experimental and might be removed
class RemoveNewlinesFormatter(logging.Formatter):
def format(self, record):
record.msg = record.msg.replace("\n", "\\n")
return super().format(record)


@hookimpl(trylast=True)
def initialize_job(context: JobContext) -> None:
Expand All @@ -27,7 +35,7 @@ def initialize_job(context: JobContext) -> None:
) # formatting a string containing curly braces

for handler in logging.getLogger().handlers:
formatter = logging.Formatter(fmt=detailed_format)
formatter = RemoveNewlinesFormatter(fmt=detailed_format)
formatter.default_time_format = "%Y-%m-%dT%H:%M:%S"
formatter.default_msec_format = "%s.%03dZ"

Expand Down