Skip to content

Commit

Permalink
Revert "Add extras to end of plain log format"
Browse files Browse the repository at this point in the history
This reverts commit 943fa79.

Reverting this until cleaning up old dependencies.
  • Loading branch information
mortenlj committed Apr 21, 2022
1 parent 943fa79 commit 9bfd080
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 52 deletions.
2 changes: 0 additions & 2 deletions README.rst
Expand Up @@ -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:

============ =======================================================================
Expand Down
22 changes: 1 addition & 21 deletions fiaas_logging/__init__.py
Expand Up @@ -21,7 +21,6 @@
import logging
import sys
import threading
from logging import LogRecord

LOG_EXTRAS = threading.local()

Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
31 changes: 2 additions & 29 deletions tests/test_fiaas_logging.py
Expand Up @@ -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"

Expand All @@ -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):
Expand Down Expand Up @@ -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"

0 comments on commit 9bfd080

Please sign in to comment.