Skip to content

Commit

Permalink
test(TestFormatters): update logging configuration and replace Ignore…
Browse files Browse the repository at this point in the history
…NANTraceFormatter with BasicFormatter
  • Loading branch information
Msameim181 committed Dec 13, 2024
1 parent fc2885c commit ee11d70
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions test/logger_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import logging
import os
import re
from io import StringIO

import pytest
Expand All @@ -13,7 +14,7 @@
trace_id_ctx,
tracer,
)
from chromatrace.logging_settings import IgnoreNANTraceFormatter
from chromatrace.logging_settings import ApplicationLevelFilter, BasicFormatter
from chromatrace.tracer import RequestIdFilter
from fastapi import FastAPI
from fastapi.testclient import TestClient
Expand Down Expand Up @@ -104,33 +105,38 @@ async def test_async_tracer(self):

class TestFormatters:
@pytest.fixture(scope="class")
def settings(self):
return LoggingSettings(
application_level="TestFormatters",
enable_console_logging=True,
enable_tracing=True,
ignore_nan_trace=True,
def config(self):
return LoggingConfig(
LoggingSettings(
application_level="TestFormatters",
enable_console_logging=False,
enable_tracing=True,
ignore_nan_trace=True,
)
)

@pytest.fixture
def stream(self):
return StringIO()

@pytest.fixture
def logger(self, settings, stream):
def logger(self, config, stream):
# Clear all existing loggers
config = LoggingConfig(settings)
logger = config.get_logger("formatter-test")

# Add StreamHandler directly to logger
handler = logging.StreamHandler(stream)
handler.setFormatter(
IgnoreNANTraceFormatter(
fmt=settings.log_format,
datefmt=settings.date_format,
style=settings.style,
BasicFormatter(
fmt=config.settings.log_format,
datefmt=config.settings.date_format,
style=config.settings.style,
colored=True,
remove_nan_trace=True,
)
)
handler.addFilter(RequestIdFilter())
handler.addFilter(ApplicationLevelFilter(config.settings.application_level))
logger.addHandler(handler)
return logger

Expand All @@ -144,10 +150,10 @@ def test_colored_output(self, logger, stream):
assert "test-id" in output

def test_ignore_nan_trace(self, logger, stream):
trace_id_ctx.set("NAN")
logger.info("Test message with no trace...")
assert "NAN" not in logger.handlers[0].format(
logging.LogRecord("", 0, "", 0, "msg", (), None)
)
output = stream.getvalue()
assert "NAN" not in output, "NAN trace should be removed"


class TestContextManagement:
Expand Down

0 comments on commit ee11d70

Please sign in to comment.