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

The message in logs formatted by the vdk-logging-json now
have any newline characters replaced by 4 spaces. 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 committed Aug 24, 2021
1 parent 4048e25 commit b828044
Show file tree
Hide file tree
Showing 2 changed files with 9 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 replaced by 4 spaces.

# 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 @@ -14,6 +14,12 @@
)


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:
attempt_id = context.core_context.state.get(CommonStoreKeys.ATTEMPT_ID)
Expand All @@ -27,7 +33,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 b828044

Please sign in to comment.