Skip to content

Commit

Permalink
feat: test non json logger
Browse files Browse the repository at this point in the history
  • Loading branch information
pquadri committed Mar 27, 2024
1 parent 5a8f127 commit 90e5767
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
6 changes: 3 additions & 3 deletions remote_log_formatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ def setup_logging(json: bool = True) -> None:
formatters={
"generic": {"class": "remote_log_formatter.JSONFormatter"},
"simple": {
"format": "%(asctime)s |%(levelname)s | %(name)s | %(message)s",
"format": "%(asctime)s | %(levelname)s | %(message)s | %(pathname)s:%(lineno)s ",
},
},
)

logging.config.dictConfig(LOG_CONFIG)


def get_logger() -> logging.Logger:
return logging.getLogger("remote")
def get_logger(name="remote") -> logging.Logger:
return logging.getLogger(name)
43 changes: 39 additions & 4 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@

@lru_cache()
@pytest.fixture()
def logger_json(setup_logging: None) -> None:
def logger_json() -> None:
_setup()
return _logger()


@lru_cache()
@pytest.fixture()
def logger_plain() -> None:
_setup(json=False)
return _logger()


@dataclass(frozen=True)
class Foo:
bar: str
Expand All @@ -34,7 +41,7 @@ class Foo:
pytest.param(Foo(bar=42), "Foo(bar=42)", id="dataclass"),
],
)
def test_logger(
def test_json_logger(
logger_json: logging.Logger,
caplog: pytest.LogCaptureFixture,
message: Any,
Expand All @@ -55,8 +62,36 @@ def test_logger(
"processname": "MainProcess",
"pathname": data["context"]["pathname"],
"module": "test_formatter",
"function": "test_logger",
"lineno": 48,
"function": "test_json_logger",
"lineno": data["context"]["lineno"],
},
"extra": {"type": "log"},
}


@lru_cache()
@pytest.mark.parametrize(
"message,expected",
[
pytest.param("foo", "foo", id="str"),
pytest.param(42, "42", id="integer"),
pytest.param(Decimal(42), "42", id="decimal"),
pytest.param(42.0, "42.0", id="float"),
pytest.param(Foo(bar=42), "Foo(bar=42)", id="dataclass"),
],
)
def test_plain_logger(
logger_plain: logging.Logger,
caplog: pytest.LogCaptureFixture,
message: Any,
expected: str,
) -> None:
logger_plain.info(message)
assert caplog.records
r = caplog.records[0]

data = logger_plain.handlers[0].format(r)

_ts, level, message, path = data.split("|")
assert message.strip() == expected
assert level.strip() == "INFO"

0 comments on commit 90e5767

Please sign in to comment.