Skip to content

Commit

Permalink
vdk-logging-json: Replace newlines in log messages processed by vdk-l…
Browse files Browse the repository at this point in the history
…ogging-json (#121)

The message in logs formatted by the vdk-logging-json now
have any newline characters escaped. This change
is made due to how logs are currently parsed by separating
at newline characters.

Signed-off-by: gageorgiev <gageorgiev@vmware.com>
  • Loading branch information
gageorgiev authored and Plamen Kostov committed Aug 29, 2021
1 parent abd3a5d commit 5e3127e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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

0 comments on commit 5e3127e

Please sign in to comment.