From 6ae730e8aa9fa4276b3448f47a4e78c4bf1791cd Mon Sep 17 00:00:00 2001 From: Grant Orndorff Date: Wed, 21 Feb 2024 15:42:25 -0500 Subject: [PATCH] Revert "logs: use less heavy structure for journald logging" This reverts commit 3bbbe5b98d398ea627a9229f9b5637ed5797b00f. --- features/unattached_commands.feature | 2 +- uaclient/log.py | 51 ++++------------------------ 2 files changed, 8 insertions(+), 45 deletions(-) diff --git a/features/unattached_commands.feature b/features/unattached_commands.feature index e8c64eb8fb..397ae48d7f 100644 --- a/features/unattached_commands.feature +++ b/features/unattached_commands.feature @@ -346,7 +346,7 @@ Feature: Command behaviour when unattached """ Then stdout matches regexp: """ - WARNING|ubuntupro.apt|fail|\d+|Failed to fetch ESM Apt Cache item: https://esm.ubuntu.com/apps/ubuntu/dists/-apps-security/InRelease|{} + "WARNING", "ubuntupro.apt", "fail", \d+, "Failed to fetch ESM Apt Cache item: https://esm.ubuntu.com/apps/ubuntu/dists/-apps-security/InRelease", {}] """ When I run `ls /var/crash/` with sudo Then stdout does not contain substring: diff --git a/uaclient/log.py b/uaclient/log.py index a398ce6fc0..ee185f3340 100644 --- a/uaclient/log.py +++ b/uaclient/log.py @@ -1,10 +1,9 @@ -import abc import json import logging import os import pathlib from collections import OrderedDict -from typing import Any, Dict, List, Tuple, Union # noqa: F401 +from typing import Any, Dict, List, Union # noqa: F401 from uaclient import defaults, system, util from uaclient.config import UAConfig @@ -18,10 +17,9 @@ def filter(self, record: logging.LogRecord): return True -class BaseProFormatter(logging.Formatter, metaclass=abc.ABCMeta): - """Formatter class that collects all the fields we care about in the - correct order and calls a subclass defined formatter function to turn - it into a string. +class JsonArrayFormatter(logging.Formatter): + """Json Array Formatter for our logging mechanism + Custom made for Pro logging needs """ default_time_format = "%Y-%m-%dT%H:%M:%S" @@ -33,7 +31,7 @@ class BaseProFormatter(logging.Formatter, metaclass=abc.ABCMeta): "funcName", "lineno", "message", - ) # type: Tuple[str, ...] + ) def format(self, record: logging.LogRecord) -> str: record.message = record.getMessage() @@ -62,42 +60,7 @@ def format(self, record: logging.LogRecord) -> str: local_log_record[field] = value local_log_record["extra"] = extra_message_dict - return self.subformatter(local_log_record) - - @abc.abstractmethod - def subformatter(self, d: Dict[str, Any]) -> str: - pass - - -class JsonArrayFormatter(BaseProFormatter): - def subformatter(self, d: Dict[str, Any]) -> str: - return json.dumps(list(d.values())) - - -class JournaldFormatter(BaseProFormatter): - """Logging to journald in json format is considered unideal - This formatter includes all the same fields, separated by `|` - characters to maintain structure - """ - - required_fields = ( - # no need for asctime because journald already has the time - "levelname", - "name", - "funcName", - "lineno", - "message", - ) - - def subformatter(self, d: Dict[str, Any]) -> str: - values = [] - for v in d.values(): - if isinstance(v, str): - values.append(v) - else: - # json format only for non string types - values.append(json.dumps(v)) - return "|".join(values) + return json.dumps(list(local_log_record.values())) def get_user_or_root_log_file_path() -> str: @@ -141,7 +104,7 @@ def setup_journald_logging(): logger.setLevel(logging.INFO) logger.addFilter(RedactionFilter()) console_handler = logging.StreamHandler() - console_handler.setFormatter(JournaldFormatter()) + console_handler.setFormatter(JsonArrayFormatter()) console_handler.setLevel(logging.INFO) logger.addHandler(console_handler)