From ee11d70048e7c55c0072ccbe89ee7c6b61dfa9f3 Mon Sep 17 00:00:00 2001 From: Mohammad Mahdi Samei <9259samei@gmail.com> Date: Fri, 13 Dec 2024 21:41:59 +0330 Subject: [PATCH] test(TestFormatters): update logging configuration and replace IgnoreNANTraceFormatter with BasicFormatter --- test/logger_test.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/test/logger_test.py b/test/logger_test.py index b988321..e2c2c0b 100644 --- a/test/logger_test.py +++ b/test/logger_test.py @@ -1,6 +1,7 @@ import asyncio import logging import os +import re from io import StringIO import pytest @@ -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 @@ -104,12 +105,14 @@ 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 @@ -117,20 +120,23 @@ 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 @@ -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: