From 9bfd08003a2f34d6ea7d28b59cb95ea14be54ddd Mon Sep 17 00:00:00 2001 From: Morten Lied Johansen Date: Thu, 21 Apr 2022 20:37:10 +0200 Subject: [PATCH] Revert "Add extras to end of plain log format" This reverts commit 943fa790543ba5e0e259e61d730a5f7ece0d7bbd. Reverting this until cleaning up old dependencies. --- README.rst | 2 -- fiaas_logging/__init__.py | 22 +--------------------- tests/test_fiaas_logging.py | 31 ++----------------------------- 3 files changed, 3 insertions(+), 52 deletions(-) diff --git a/README.rst b/README.rst index b5e7b77..af1500c 100644 --- a/README.rst +++ b/README.rst @@ -53,8 +53,6 @@ debug `True`/`False` Enable debug logging ====== =============== ================================================= The plain format contains the fields timestamp, level name, message, logger name, and thread name. -If the extras feature is used, key-value pairs will be added to the end of the message. - In the json format, there are more fields, with more detail. The fields in the json output are: ============ ======================================================================= diff --git a/fiaas_logging/__init__.py b/fiaas_logging/__init__.py index 13aafd8..80aa26b 100644 --- a/fiaas_logging/__init__.py +++ b/fiaas_logging/__init__.py @@ -21,7 +21,6 @@ import logging import sys import threading -from logging import LogRecord LOG_EXTRAS = threading.local() @@ -78,25 +77,6 @@ def _build_location(fields): } -class PlainExtraFormatter(logging.Formatter): - def __init__(self): - super().__init__("[%(asctime)s|%(levelname)7s] %(message)s [%(name)s|%(threadName)s]%(extra_string)s") - - def format(self, record: LogRecord) -> str: - if hasattr(record, "extras") and record.extras: - record.extra_string = self._format_extras(record) - else: - record.extra_string = "" - return super().format(record) - - @staticmethod - def _format_extras(record): - pairs = [] - for k, v in record.extras.items(): - pairs.append(f"{k}={v}") - return " " + ", ".join(pairs) - - class ExtraFilter(logging.Filter): def filter(self, record): extras = {} @@ -130,5 +110,5 @@ def _create_default_handler(format): if format == "json": handler.setFormatter(FiaasFormatter()) elif format == "plain": - handler.setFormatter(PlainExtraFormatter()) + handler.setFormatter(logging.Formatter("[%(asctime)s|%(levelname)7s] %(message)s [%(name)s|%(threadName)s]")) return handler diff --git a/tests/test_fiaas_logging.py b/tests/test_fiaas_logging.py index 7c8882f..2a5630f 100644 --- a/tests/test_fiaas_logging.py +++ b/tests/test_fiaas_logging.py @@ -19,15 +19,13 @@ import json import logging import sys -from datetime import datetime import mock import pytest from callee import InstanceOf, Attrs, List from six import StringIO -from fiaas_logging import ExtraFilter, set_extras, init_logging, FiaasFormatter, _create_default_handler, \ - PlainExtraFormatter +from fiaas_logging import ExtraFilter, set_extras, init_logging, FiaasFormatter, _create_default_handler TEST_MESSAGE = "This is a test message" @@ -48,7 +46,7 @@ def debug(self, request): @pytest.fixture(params=("plain", "json")) def format(self, request): - yield request.param, FiaasFormatter if request.param == "json" else PlainExtraFormatter + yield request.param, FiaasFormatter if request.param == "json" else logging.Formatter @staticmethod def _describe_stream_handler(formatter): @@ -86,28 +84,3 @@ def test_json_log_has_extra(self): assert TEST_MESSAGE in log_entry["message"] assert log_entry["extras"]["one"] == "1" assert log_entry["extras"]["two"] == "2" - - -class TestPlainExtraFormatter(object): - @pytest.fixture - def formatter(self): - return PlainExtraFormatter() - - @pytest.fixture - def record(self): - record = logging.LogRecord("name", logging.INFO, "pathname", 42, "msg", None, None) - record.created = datetime(2000, 1, 1, 0, 0, 0).timestamp() - record.msecs = 0 - return record - - def test_no_extras(self, formatter, record): - actual = formatter.format(record) - assert actual == "[2000-01-01 00:00:00,000| INFO] msg [name|MainThread]" - - def test_with_extras(self, formatter, record): - record.extras = { - "key1": "value1", - "key2": "value2", - } - actual = formatter.format(record) - assert actual == "[2000-01-01 00:00:00,000| INFO] msg [name|MainThread] key1=value1, key2=value2"