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-structlog: LTSV formatting #2887

Merged
merged 8 commits into from
Nov 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ def add_fields(
log_record["level"] = record.levelname


class LtsvFormatter(Formatter):
"""
Formats logs in the LTSV format.

"""

def format(self, record: LogRecord) -> LogRecord:
record.message = record.getMessage()
record.timestamp = self.formatTime(record, self.datefmt)
record.line_number = record.lineno
if ("level" not in record.__dict__) or (not record.__dict__["level"]):
record.level = record.levelname

s = self.formatMessage(record)
return s


class ConsoleFormatter(Formatter):
"""
Console formatter. Basically the same as logging.Formatter.
Expand Down Expand Up @@ -105,6 +122,17 @@ def build_console_format(self) -> str:
out.append("- %(message)s")
return " ".join(out)

def build_ltsv_format(self) -> str:
out = []
for key in self._metadata_keys:
if key in STRUCTLOG_LOGGING_METADATA_ALL:
out.append(key + ":" + STRUCTLOG_LOGGING_METADATA_ALL[key])
else:
out.append(key + ":" + f"%({key})s")
out.append("message:%(message)s")
ltsv_format = "\t".join(out)
return ltsv_format


def create_formatter(logging_format: str, metadata_keys: str) -> [Formatter, Filter]:
"""
Expand All @@ -121,6 +149,11 @@ def create_formatter(logging_format: str, metadata_keys: str) -> [Formatter, Fil
StructlogMetadataBuilder(metadata_keys).build_json_format()
)
custom_key_filter = JsonMetadataFilter(key_set)
elif logging_format == "ltsv":
formatter = LtsvFormatter(
fmt=StructlogMetadataBuilder(metadata_keys).build_ltsv_format()
)
custom_key_filter = ConsoleMetadataFilter(key_set)
else:
formatter = ConsoleFormatter(
fmt=StructlogMetadataBuilder(metadata_keys).build_console_format()
Expand Down
14 changes: 10 additions & 4 deletions projects/vdk-plugins/vdk-structlog/tests/test_structlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
"line_number": ":[0-9]+",
"vdk_job_name": JOB_NAME,
},
"ltsv": {
"level": r"level:INFO",
"file_name": r"file_name:10_dummy\.py",
"line_number": "line_number:[0-9]+",
"vdk_job_name": f"job_name:{JOB_NAME}",
},
"json": {
"level": '"level": "INFO"',
"file_name": '"filename:":"10_dummy.py"',
Expand All @@ -41,8 +47,8 @@


@pytest.mark.parametrize(
"log_format", ["console"]
) # TODO: replace with ["console","json"] once the issue where fields can't be excluded in JSON is fixed
"log_format", ["console, ltsv"]
) # TODO: replace with ["console","ltsv", "json"] once the issue where fields can't be excluded in JSON is fixed
def test_structlog(log_format):
with mock.patch.dict(
os.environ,
Expand Down Expand Up @@ -71,8 +77,8 @@ def test_structlog(log_format):


@pytest.mark.parametrize(
"log_format", ["console"]
) # TODO: replace with ["console", "json"] once the issue where fields can't be excluded in JSON is fixed
"log_format", ["console", "ltsv"]
) # TODO: replace with ["console", "ltsv", "json"] once the issue where fields can't be excluded in JSON is fixed
def test_stock_fields_removal(log_format):
stock_field_reps = STOCK_FIELD_REPRESENTATIONS[log_format]

Expand Down